Class: CallableHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/rack-payment/callable_hash.rb

Overview

A Hash that you can call, like an OpenStruct, but you can still call [] on it.

>> hash = CallableHash.new :foo => 'bar'

>> hash.foo
=> 'bar'

>> hash[:foo]
=> 'bar'

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CallableHash

Returns a new instance of CallableHash.



13
14
15
# File 'lib/rack-payment/callable_hash.rb', line 13

def initialize hash
  hash.each {|key, value| self[key] = value }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rack-payment/callable_hash.rb', line 17

def method_missing name, *args
  if self[name]
    self[name]
  else
    super
  end
end

Instance Method Details

#typeObject

Override type so, if there’s a :type or ‘type’ key in this Hash, we return that value. Else we return the actual object type.



27
28
29
30
31
# File 'lib/rack-payment/callable_hash.rb', line 27

def type
  return self[:type]  if self[:type]
  return self['type'] if self['type']
  super
end

#zipObject

Override zip so, if there’s a :zip or ‘zip’ key in this Hash, we return that value. Else we return the actual object zip.



35
36
37
38
39
# File 'lib/rack-payment/callable_hash.rb', line 35

def zip
  return self[:zip]  if self[:zip]
  return self['zip'] if self['zip']
  super
end