Module: Balanced::Resource
- Included in:
- ApiKey, BankAccount, BankAccountVerification, Callback, Card, CardHold, Credit, Customer, Debit, Dispute, Event, EventCallback, Log, Marketplace, Order, Refund, Reversal, Transfer
- Defined in:
- lib/balanced/resources/resource.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/balanced/resources/resource.rb', line 151
def method_missing(method, *args, &block)
if @attributes.has_key?(method.to_s)
return @attributes[method.to_s]
end
case method.to_s
when /(.+)=$/
attr = method.to_s.chop
@attributes[attr] = args[0]
else
if @hyperlinks.has_key? "#{method}"
value = @hyperlinks["#{method}"]
result = value.call
return result
else
super
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
11
12
13
|
# File 'lib/balanced/resources/resource.rb', line 11
def attributes
@attributes
end
|
#href ⇒ Object
Returns the value of attribute href.
15
16
17
|
# File 'lib/balanced/resources/resource.rb', line 15
def href
@href
end
|
#hyperlinks ⇒ Object
Returns the value of attribute hyperlinks.
12
13
14
|
# File 'lib/balanced/resources/resource.rb', line 12
def hyperlinks
@hyperlinks
end
|
#id ⇒ Object
Returns the value of attribute id.
14
15
16
|
# File 'lib/balanced/resources/resource.rb', line 14
def id
@id
end
|
#links ⇒ Object
Returns the value of attribute links.
16
17
18
|
# File 'lib/balanced/resources/resource.rb', line 16
def links
@links
end
|
Class Method Details
.included(base) ⇒ Object
197
198
199
|
# File 'lib/balanced/resources/resource.rb', line 197
def self.included(base)
base.extend ClassMethods
end
|
Instance Method Details
#copy_from(other) ⇒ Object
145
146
147
148
149
|
# File 'lib/balanced/resources/resource.rb', line 145
def copy_from(other)
other.instance_variables.each do |ivar|
instance_variable_set ivar, other.instance_variable_get(ivar)
end
end
|
#does_resource_respond_to?(method_name) ⇒ Boolean
193
194
195
|
# File 'lib/balanced/resources/resource.rb', line 193
def does_resource_respond_to?(method_name)
@attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
end
|
#fetch(*arguments) ⇒ Object
Also known as:
find
delegate the query to the pager module
85
86
87
|
# File 'lib/balanced/resources/resource.rb', line 85
def fetch(*arguments)
self.class.find *arguments
end
|
#hydrate(links, meta) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/balanced/resources/resource.rb', line 43
def hydrate(links, meta)
links.each do |key, uri|
property = key.sub(/.*?\./, '')
if self.links.include? property
link_value = self.links[property]
template = Addressable::Template.new(uri)
uri = template.expand("#{key}" => link_value).to_s
@hyperlinks[property] = Balanced::Utils.callable(
link_value.nil? ? link_value : lambda { self.class.find(uri) }
)
else
unless uri.nil? || uri.is_a?(Hash)
begin
match_data = /\{(#{self.class.collection_name}\.(\w+))\}/.match(uri)
rescue TypeError => ex
puts 'what the fuck'
raise ex
end
unless match_data.nil?
attribute_path = match_data[1]
attribute_name = match_data[2]
template = Addressable::Template.new(uri)
uri = template.expand("#{attribute_path}" => @attributes[attribute_name]).to_s
end
end
@hyperlinks[property] = Balanced::Utils.callable(Balanced::Pager.new(uri, {}))
end
end
end
|
#initialize(attributes = {}) ⇒ Object
18
19
20
21
|
# File 'lib/balanced/resources/resource.rb', line 18
def initialize(attributes = {})
@attributes = Utils.indifferent_read_access attributes
@hyperlinks = {}
end
|
#reload(the_response = nil) ⇒ Object
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/balanced/resources/resource.rb', line 134
def reload(the_response = nil)
if the_response
return if the_response.body.to_s.length.zero?
fresh = self.class.construct_from_response the_response.body
else
fresh = self.find(@attributes[:href])
end
fresh and copy_from fresh
self
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
184
185
186
|
# File 'lib/balanced/resources/resource.rb', line 184
def respond_to?(method_name, include_private=false)
does_resource_respond_to?(method_name) || super
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
188
189
190
|
# File 'lib/balanced/resources/resource.rb', line 188
def respond_to_missing?(method_name, include_private=false)
does_resource_respond_to?(method_name) || super
end
|
#sanitize ⇒ Object
113
114
115
116
117
118
119
120
121
|
# File 'lib/balanced/resources/resource.rb', line 113
def sanitize
to_submit = {}
@attributes.each do |key, value|
unless value.is_a? Balanced::Resource
to_submit[key] = value
end
end
to_submit
end
|
#save ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/balanced/resources/resource.rb', line 91
def save
href = @attributes.delete('href')
method = :post
if href.nil?
href = self.class.collection_path
elsif !Balanced.is_collection(href)
method = :put
end
attributes_to_submit = self.sanitize
begin
@response = Balanced.send(method, href, attributes_to_submit)
rescue Balanced::Error
@attributes['href'] = href
raise
end
reload @response
end
|
#unstore ⇒ Object
Also known as:
destroy
129
130
131
|
# File 'lib/balanced/resources/resource.rb', line 129
def unstore
Balanced.unstore @attributes[:href]
end
|