class Hash

Attributes

hash_dot_use_default[RW]
use_dot_syntax[RW]
hash_dot_use_default[RW]
use_dot_syntax[RW]

Public Instance Methods

method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/hash.rb, line 18
def method_missing(method, *args)
  return super(method, *args) unless to_dot?

  method, chain = method.to_s.split(".", 2).map(&:to_sym)

  return send(method).send(chain, *args) if chain

  prop = create_prop(method)

  if setter?(method)
    self[prop] = args.first
  elsif key?(prop) || use_default?
    self[prop]
  else
    super(method, args)
  end
end
respond_to?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/hash.rb, line 36
def respond_to?(method, include_all = false)
  return super(method, include_all) unless to_dot?

  prop = create_prop(method)
  return true if key?(prop)

  super(method, include_all)
end
to_dot(use_default: false) click to toggle source
# File lib/hash.rb, line 12
def to_dot(use_default: false)
  dotify_hash(self, use_default: use_default)

  self
end

Private Instance Methods

basic_prop_from_method(method) click to toggle source
# File lib/hash.rb, line 86
def basic_prop_from_method(method)
  setter?(method) ? method.chop : method
end
create_prop(method) click to toggle source
# File lib/hash.rb, line 80
def create_prop(method)
  prop = basic_prop_from_method(method)

  key?(prop.to_s) ? prop.to_s : prop
end
dotify_hash(hash, use_default: false) click to toggle source
# File lib/hash.rb, line 59
def dotify_hash(hash, use_default: false)
  hash.use_dot_syntax = true
  hash.hash_dot_use_default = use_default

  hash.each_value do |val|
    dotify_obj(val, use_default: use_default)
  end
end
dotify_obj(obj, use_default: false) click to toggle source
# File lib/hash.rb, line 47
def dotify_obj(obj, use_default: false)
  case obj
  when Array
    obj.each do |el|
      dotify_obj(el, use_default: use_default)
    end
  when Hash
    dotify_hash(obj, use_default: use_default)
    # else no-op
  end
end
setter?(method) click to toggle source
# File lib/hash.rb, line 76
def setter?(method)
  method.last == "="
end
to_dot?() click to toggle source
# File lib/hash.rb, line 68
def to_dot?
  use_dot_syntax || self.class.use_dot_syntax
end
use_default?() click to toggle source
# File lib/hash.rb, line 72
def use_default?
  hash_dot_use_default || self.class.hash_dot_use_default
end