Module: Balanced::Resource
- Included in:
- Account, ApiKey, BankAccount, Callback, Card, Credit, Customer, Debit, Event, EventCallback, Hold, Invoice, Log, Marketplace, Merchant, Refund, Reversal, Transaction, Verification
- 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/balanced/resources/resource.rb', line 91
def method_missing(method, *args, &block)
case method
when /(.+)\=$/
attr = method.to_s.chop
@attributes[attr] = args[0]
else
if @attributes.has_key? "#{method}_uri"
value = @attributes["#{method}_uri"]
begin
values_class = Balanced.from_uri(value)
rescue NameError
super
end
if Balanced.is_collection(value)
= Balanced::Pager.new value, {}
return .to_a
else
return values_class.find(value)
end
else
super
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6
7
8
|
# File 'lib/balanced/resources/resource.rb', line 6
def attributes
@attributes
end
|
Class Method Details
.included(base) ⇒ Object
136
137
138
|
# File 'lib/balanced/resources/resource.rb', line 136
def self.included(base)
base.extend ClassMethods
end
|
Instance Method Details
#copy_from(other) ⇒ Object
85
86
87
88
89
|
# File 'lib/balanced/resources/resource.rb', line 85
def copy_from other
other.instance_variables.each do |ivar|
instance_variable_set ivar, other.instance_variable_get(ivar)
end
end
|
#destroy ⇒ Object
66
67
68
|
# File 'lib/balanced/resources/resource.rb', line 66
def destroy
Balanced.delete @attributes[:uri]
end
|
#find(*arguments) ⇒ Object
delegate the query to the pager module
14
15
16
|
# File 'lib/balanced/resources/resource.rb', line 14
def find *arguments
self.class.find *arguments
end
|
#initialize(attributes = {}) ⇒ Object
8
9
10
|
# File 'lib/balanced/resources/resource.rb', line 8
def initialize attributes = {}
@attributes = attributes
end
|
#reload(the_response = nil) ⇒ Object
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/balanced/resources/resource.rb', line 74
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[:uri])
end
fresh and copy_from fresh
self
end
|
#sanitize ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/balanced/resources/resource.rb', line 31
def sanitize
to_submit = {}
@attributes.each do |key, value|
if not value.is_a? Balanced::Resource
to_submit[key] = value
end
end
to_submit
end
|
#save ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/balanced/resources/resource.rb', line 18
def save
uri = @attributes.delete('uri')
method = :post
if uri.nil?
uri = self.class.collection_path
elsif !Balanced.is_collection(uri)
method = :put
end
attributes_to_submit = self.sanitize
@response = Balanced.send(method, uri, attributes_to_submit)
reload @response
end
|
#unstore ⇒ Object
70
71
72
|
# File 'lib/balanced/resources/resource.rb', line 70
def unstore
destroy
end
|
#warn_on_positional(args) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/balanced/resources/resource.rb', line 41
def warn_on_positional args
msg = <<-WARNING
Called from: #{caller[1]}
#############################################################
# WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! #
#############################################################
Using positional arguments is **DEPRECATED**. Please use the
keyword options pattern instead. Version __0.7.0__ of the
Ruby client will not support positional arguments.
If you need help, please hop on irc.freenode.net #balanced
or contact [email protected]
WARNING
unless (args.size == 1 and args.last.is_a? Hash) or (args.size == 0)
warn msg
end
end
|