Class: Twocheckout::HashObject

Inherits:
Object
  • Object
show all
Defined in:
lib/twocheckout/hash_object.rb

Direct Known Subclasses

Checkout, Company, Invoice, LineItem, Product, Sale

Instance Method Summary collapse

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

#inspectObject



33
34
35
# File 'lib/twocheckout/hash_object.rb', line 33

def inspect
  "#<#{self.class.name}:#{self._key}> #{pp @hash}"
end