class Hashr
Constants
- VERSION
Attributes
Public Class Methods
Source
# File lib/hashr.rb, line 23 def const_missing(name) Kernel.const_get(name) end
Source
# File lib/hashr.rb, line 18 def default(defaults) @defaults = (self.defaults || {}).deep_merge(defaults || {}) end
Also aliased as: define
Source
# File lib/hashr.rb, line 10 def inherited(other) other.default(defaults) end
Source
# File lib/hashr.rb, line 14 def new(*args) super(self, *args) end
Calls superclass method
Source
# File lib/hashr.rb, line 30 def initialize(klass, data = nil, defaults = nil, &block) ::Kernel.fail ::ArgumentError.new("Invalid input #{data.inspect}") unless data.nil? || data.is_a?(::Hash) data = (data || {}).deep_symbolize_keys defaults = (defaults || klass.defaults || {}).deep_symbolize_keys @class = klass @data = defaults.deep_merge(data).inject({}) do |result, (key, value)| result.merge(key => value.is_a?(::Hash) ? ::Hashr.new(value, {}) : value) end end
Public Instance Methods
Source
# File lib/hashr.rb, line 84 def ==(other) to_h == other.to_h if other.respond_to?(:to_h) end
Source
# File lib/hashr.rb, line 50 def []=(key, value) @data.store(to_key(key), value.is_a?(::Hash) ? ::Hashr.new(value, {}) : value) end
Source
# File lib/hashr.rb, line 99 def inspect "<#{self.class.name} #{@data.inspect}>" end
Source
# File lib/hashr.rb, line 88 def instance_of?(const) self.class == const end
Source
# File lib/hashr.rb, line 92 def is_a?(const) consts = [self.class] consts << consts.last.superclass while consts.last.superclass consts.include?(const) end
Also aliased as: kind_of?
Source
# File lib/hashr.rb, line 62 def method_missing(name, *args, &block) case name.to_s[-1, 1] when '?' !!self[name.to_s[0..-2]] when '=' self[name.to_s[0..-2]] = args.first else self[name] end end
Source
# File lib/hashr.rb, line 77 def to_h @data.inject({}) do |hash, (key, value)| hash.merge(key => value.is_a?(Hashr) || value.is_a?(Hash) ? value.to_h : value) end end
Also aliased as: to_hash
Source
# File lib/hashr.rb, line 73 def try(key) defined?(key) ? self[key] : nil # TODO needs to look for to_h etc end
Source
# File lib/hashr.rb, line 54 def values_at(*keys) keys.map { |key| self[key] } end
Private Instance Methods
Source
# File lib/hashr.rb, line 105 def to_key(key) key.respond_to?(:to_sym) ? key.to_sym : key end