Class: Caesars::Hash
Overview
A subclass of ::Hash that provides method names for hash parameters. It’s like a lightweight OpenStruct.
ch = Caesars::Hash[:tabasco => :lots!]
puts ch.tabasco # => lots!
Instance Method Summary collapse
- #method_missing(meth) ⇒ Object
-
#to_hash(hash = self) ⇒ Object
Returns a clone of itself and all children cast as ::Hash objects.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth) ⇒ Object
68 69 70 |
# File 'lib/caesars.rb', line 68 def method_missing(meth) self[meth] if self.has_key?(meth) end |
Instance Method Details
#to_hash(hash = self) ⇒ Object
Returns a clone of itself and all children cast as ::Hash objects
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/caesars.rb', line 73 def to_hash(hash=self) return hash unless hash.is_a?(Caesars::Hash) # nothing to do target = (Caesars::HASH_TYPE)[dup] hash.keys.each do |key| if hash[key].is_a? Caesars::Hash target[key] = hash[key].to_hash next elsif hash[key].is_a? Array target[key] = hash[key].collect { |h| to_hash(h) } next end target[key] = hash[key] end target end |