Class: Shipay::ShipayObject
Constant Summary
collapse
- RESOURCES =
Dir[File.expand_path('../resources/*.rb', __FILE__)].map do |path|
File.basename(path, '.rb').to_sym
end
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(response = {}) ⇒ ShipayObject
Returns a new instance of ShipayObject.
9
10
11
12
13
14
15
16
17
|
# File 'lib/shipay/object.rb', line 9
def initialize(response = {})
@attributes = Hash.new
@unsaved_attributes = Set.new
@client_key = response.dig(:client_key) || Shipay.default_client_key update response
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/shipay/object.rb', line 88
def method_missing(name, *args, &block)
name = name.to_s
unless block_given?
if name.end_with?('=') && args.size == 1
attribute_name = name[0...-1]
return self[attribute_name] = args[0]
end
if args.size == 0
return self[name] || self[name.to_sym]
end
end
if attributes.respond_to? name
return attributes.public_send name, *args, &block
end
super name, *args, &block
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/shipay/object.rb', line 3
def attributes
@attributes
end
|
Class Method Details
.convert(response, resource_name = nil, client_key = nil) ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/shipay/object.rb', line 111
def convert(response, resource_name = nil, client_key=nil)
case response
when Array
response.map{ |i| convert i, resource_name, client_key }
when Hash
resource_class_for(resource_name).new(response.merge({client_key: client_key}))
else
response
end
end
|
Instance Method Details
#==(other) ⇒ Object
28
29
30
|
# File 'lib/shipay/object.rb', line 28
def ==(other)
self.class == other.class && id == other.id
end
|
#[]=(key, value) ⇒ Object
19
20
21
22
|
# File 'lib/shipay/object.rb', line 19
def []=(key,value)
@attributes[key] = value
@unsaved_attributes.add key
end
|
#empty? ⇒ Boolean
24
25
26
|
# File 'lib/shipay/object.rb', line 24
def empty?
@attributes.empty?
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
44
45
46
47
48
|
# File 'lib/shipay/object.rb', line 44
def respond_to?(name, include_all = false)
return true if name.to_s.end_with? '='
@attributes.has_key?(name.to_s) || super
end
|
38
39
40
41
42
|
# File 'lib/shipay/object.rb', line 38
def to_hash
Hash[@attributes.map do |key, value|
[ key, to_hash_value(value, :to_hash) ]
end]
end
|
#unsaved_attributes ⇒ Object
32
33
34
35
36
|
# File 'lib/shipay/object.rb', line 32
def unsaved_attributes
Hash[@unsaved_attributes.map do |key|
[ key, to_hash_value(self[key], :unsaved_attributes) ]
end]
end
|