Class: Twocheckout::HashObject
- Inherits:
-
Object
- Object
- Twocheckout::HashObject
- Defined in:
- lib/twocheckout/hash_object.rb
Instance Method Summary collapse
-
#initialize(hash) ⇒ HashObject
constructor
A new instance of HashObject.
- #inspect ⇒ Object
- #method_missing(name) ⇒ Object
Constructor Details
#initialize(hash) ⇒ HashObject
Returns a new instance of HashObject.
6 7 8 |
# File 'lib/twocheckout/hash_object.rb', line 6 def initialize(hash) @hash = hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/twocheckout/hash_object.rb', line 10 def method_missing(name) name = name.to_s if @hash.key? name result = @hash[name] if result.is_a?(Hash) result = HashObject.new(result) @hash[name] = result end if result.is_a?(Array) new_array = [] result.each do |item| new_item = item.is_a?(Hash) ? HashObject.new(item) : item new_array << new_item end new_array.freeze result = new_array @hash[name] = result end return result end super.method_missing name end |
Instance Method Details
#inspect ⇒ Object
33 34 35 |
# File 'lib/twocheckout/hash_object.rb', line 33 def inspect "#<#{self.class.name}:#{self._key}> #{pp @hash}" end |