class Hashr
Constants
- VERSION
Attributes
defaults[R]
class[R]
Public Class Methods
const_missing(name)
click to toggle source
# File lib/hashr.rb, line 23 def const_missing(name) Kernel.const_get(name) end
default(defaults)
click to toggle source
# File lib/hashr.rb, line 18 def default(defaults) @defaults = (self.defaults || {}).deep_merge(defaults || {}) end
Also aliased as: define
inherited(other)
click to toggle source
# File lib/hashr.rb, line 10 def inherited(other) other.default(defaults) end
new(*args)
click to toggle source
Calls superclass method
# File lib/hashr.rb, line 14 def new(*args) super(self, *args) end
new(klass, data = nil, defaults = nil, &block)
click to toggle 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
==(other)
click to toggle source
# File lib/hashr.rb, line 84 def ==(other) to_h == other.to_h if other.respond_to?(:to_h) end
[](key)
click to toggle source
# File lib/hashr.rb, line 46 def [](key) @data[to_key(key)] end
[]=(key, value)
click to toggle source
# File lib/hashr.rb, line 50 def []=(key, value) @data.store(to_key(key), value.is_a?(::Hash) ? ::Hashr.new(value, {}) : value) end
defined?(key)
click to toggle source
# File lib/hashr.rb, line 42 def defined?(key) @data.key?(to_key(key)) end
inspect()
click to toggle source
# File lib/hashr.rb, line 99 def inspect "<#{self.class.name} #{@data.inspect}>" end
instance_of?(const)
click to toggle source
# File lib/hashr.rb, line 88 def instance_of?(const) self.class == const end
is_a?(const)
click to toggle 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?
method_missing(name, *args, &block)
click to toggle 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
respond_to?(*args)
click to toggle source
# File lib/hashr.rb, line 58 def respond_to?(*args) true end
to_h()
click to toggle 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
try(key)
click to toggle source
# File lib/hashr.rb, line 73 def try(key) defined?(key) ? self[key] : nil # TODO needs to look for to_h etc end
values_at(*keys)
click to toggle source
# File lib/hashr.rb, line 54 def values_at(*keys) keys.map { |key| self[key] } end
Private Instance Methods
to_key(key)
click to toggle source
# File lib/hashr.rb, line 105 def to_key(key) key.respond_to?(:to_sym) ? key.to_sym : key end