Class: Hyperwallet::HyperwalletObject

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperwallet/hyperwallet_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ HyperwalletObject

Returns a new instance of HyperwalletObject.



5
6
7
8
9
10
11
# File 'lib/hyperwallet/hyperwallet_object.rb', line 5

def initialize(values)
  @values = {}
  (self.class.attributes + values.keys).each do |k|
    attr = Util.symbolize_attribute(k)
    @values[attr] = HyperwalletObject.convert_to_hyperwallet_object(values[k], self.class, attr)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/hyperwallet/hyperwallet_object.rb', line 37

def method_missing(name, *args, &block)
  if @values.has_key?(name)
    @values[name]
  else
    super
  end
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



3
4
5
# File 'lib/hyperwallet/hyperwallet_object.rb', line 3

def values
  @values
end

Class Method Details

.attributesObject



25
26
27
# File 'lib/hyperwallet/hyperwallet_object.rb', line 25

def self.attributes
  []
end

.construct_from(values) ⇒ Object



33
34
35
# File 'lib/hyperwallet/hyperwallet_object.rb', line 33

def self.construct_from(values)
  object = self.new(values)
end

.convert_to_hyperwallet_object(resp, type = nil, relation = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hyperwallet/hyperwallet_object.rb', line 13

def self.convert_to_hyperwallet_object(resp, type = nil, relation = nil)
  case resp
  when Array
    resp.map { |r| convert_to_hyperwallet_object(r, type, relation) }
  when Hash
    object_class = (relation.nil? ? type : type.relations[relation]) || HyperwalletObject
    object_class.construct_from(resp)
  else
    resp
  end
end

.relationsObject



29
30
31
# File 'lib/hyperwallet/hyperwallet_object.rb', line 29

def self.relations
  {}
end

Instance Method Details

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/hyperwallet/hyperwallet_object.rb', line 45

def respond_to_missing?(name, *args)
  @values.has_key?(name) || super
end