Class: Caesars::Hash

Inherits:
HASH_TYPE
  • Object
show all
Defined in:
lib/caesars/hash.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object



10
11
12
13
# File 'lib/caesars/hash.rb', line 10

def method_missing(meth)
  STDERR.puts "Caesars::Hash.method_missing: #{meth}" if Caesars.debug?
  self[meth] || self[meth.to_s]
end

Instance Method Details

#__class__Object



32
33
34
# File 'lib/caesars/hash.rb', line 32

def __class__
  HASH_TYPE
end

#to_hash(hash = self) ⇒ Object

Returns a clone of itself and all children cast as ::Hash objects



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/caesars/hash.rb', line 16

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