lib/hash.rb in hash_dot-2.1.0 vs lib/hash.rb in hash_dot-2.2.0
- old
+ new
@@ -1,14 +1,16 @@
class Hash
class << self
attr_accessor :use_dot_syntax
+ attr_accessor :hash_dot_use_default
end
attr_accessor :use_dot_syntax
+ attr_accessor :hash_dot_use_default
- def to_dot
- dotify_hash(self)
+ def to_dot(use_default: false)
+ dotify_hash(self, use_default: use_default)
self
end
def method_missing(method, *args)
@@ -16,13 +18,14 @@
prop = create_prop(method)
if setter?(method)
self[prop] = args.first
- else
- super(method, args) && return unless key?(prop)
+ elsif key?(prop) || use_default?
self[prop]
+ else
+ super(method, args)
end
end
def respond_to?(method, include_all=false)
return super(method, include_all) unless to_dot?
@@ -31,17 +34,22 @@
super(method, include_all)
end
private
- def dotify_hash(hash)
+ def dotify_hash(hash, use_default: false)
hash.use_dot_syntax = true
+ hash.hash_dot_use_default = use_default
- hash.keys.each { |key| dotify_hash(hash[key]) if hash[key].is_a?(Hash) }
+ hash.keys.each { |key| dotify_hash(hash[key], use_default: use_default) if hash[key].is_a?(Hash) }
end
def to_dot?
use_dot_syntax || self.class.use_dot_syntax
+ end
+
+ def use_default?
+ hash_dot_use_default || self.class.hash_dot_use_default
end
def setter?(method)
method.last == "="
end