Class: Caesars::Hash
Overview
Caesars::Hash
A subclass of ::Hash (1.9) or Caesars::OrderedHash (1.8) 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
- #__class__ ⇒ Object
- #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
Instance Method Details
#__class__ ⇒ Object
35 36 37 |
# File 'lib/caesars/hash.rb', line 35 def __class__ HASH_TYPE end |
#to_hash(hash = self) ⇒ Object
Returns a clone of itself and all children cast as ::Hash objects
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/caesars/hash.rb', line 19 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 |