Class: Invoiced::Object
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/invoiced/object.rb
Direct Known Subclasses
Attachment, CatalogItem, Contact, Coupon, CreditNote, Customer, Email, Estimate, Event, File, Invoice, Letter, LineItem, Note, PaymentPlan, PaymentSource, PaymentSourceObject, Plan, Subscription, Task, TaxRate, TextMessage, Transaction
Constant Summary
collapse
- @@permanent_attributes =
Set.new([:id])
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, id = nil, values = {}) ⇒ Object
Returns a new instance of Object.
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/invoiced/object.rb', line 9
def initialize(client, id=nil, values={})
@client = client
@endpoint_base = ''
@endpoint = build_endpoint
@id = id
@values = {}
if !id.nil?
@endpoint += "/#{id}"
@unsaved = Set.new
refresh_from(values.dup.merge({:id => id}))
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/invoiced/object.rb', line 149
def method_missing(name, *args)
if name.to_s.end_with?('=')
attr = name.to_s[0...-1].to_sym
add_accessors([attr])
begin
mth = method(name)
rescue NameError
raise NoMethodError.new("Cannot set #{attr} on this object. HINT: you can't set: #{@@permanent_attributes.to_a.join(', ')}")
end
return mth.call(args[0])
else
return @values[name] if @values.has_key?(name)
end
begin
super
rescue NoMethodError => e
raise e
end
end
|
Instance Attribute Details
Returns the value of attribute client.
5
6
7
|
# File 'lib/invoiced/object.rb', line 5
def client
@client
end
|
Instance Method Details
83
84
85
|
# File 'lib/invoiced/object.rb', line 83
def [](k)
@values[k.to_sym]
end
|
#[]=(k, v) ⇒ Object
87
88
89
|
# File 'lib/invoiced/object.rb', line 87
def []=(k, v)
send(:"#{k}=", v)
end
|
#add_accessors(keys) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/invoiced/object.rb', line 129
def add_accessors(keys)
metaclass.instance_eval do
keys.each do |k|
next if @@permanent_attributes.include?(k)
k_eq = :"#{k}="
define_method(k) { @values[k] }
define_method(k_eq) do |v|
if v == ""
raise ArgumentError.new(
"You cannot set #{k} to an empty string." \
"We interpret empty strings as nil in requests." \
"You may set #{self}.#{k} = nil to delete the property.")
end
@values[k] = v
@unsaved.add(k)
end
end
end
end
|
#build_endpoint ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/invoiced/object.rb', line 36
def build_endpoint
if self.class.const_defined? "OBJECT_NAME"
'/' + self.class::OBJECT_NAME + 's'
else
'/objects'
end
end
|
#each(&blk) ⇒ Object
110
111
112
|
# File 'lib/invoiced/object.rb', line 110
def each(&blk)
@values.each(&blk)
end
|
32
33
34
|
# File 'lib/invoiced/object.rb', line 32
def endpoint()
@endpoint_base + @endpoint
end
|
#endpoint_base ⇒ Object
28
29
30
|
# File 'lib/invoiced/object.rb', line 28
def endpoint_base()
@endpoint_base
end
|
58
59
60
61
|
# File 'lib/invoiced/object.rb', line 58
def inspect
id_string = (!@id.nil?) ? " id=#{@id}" : ""
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
end
|
91
92
93
|
# File 'lib/invoiced/object.rb', line 91
def keys
@values.keys
end
|
114
115
116
|
# File 'lib/invoiced/object.rb', line 114
def metaclass
class << self; self; end
end
|
#refresh_from(values) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/invoiced/object.rb', line 63
def refresh_from(values)
removed = Set.new(@values.keys - values.keys)
added = Set.new(values.keys - @values.keys)
instance_eval do
remove_accessors(removed)
add_accessors(added)
end
removed.each do |k|
@values.delete(k)
@unsaved.delete(k)
end
values.each do |k, v|
@values[k] = v
@unsaved.delete(k)
end
return self
end
|
#remove_accessors(keys) ⇒ Object
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/invoiced/object.rb', line 118
def remove_accessors(keys)
metaclass.instance_eval do
keys.each do |k|
next if @@permanent_attributes.include?(k)
k_eq = :"#{k}="
remove_method(k) if method_defined?(k)
remove_method(k_eq) if method_defined?(k_eq)
end
end
end
|
#retrieve(id, params = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/invoiced/object.rb', line 44
def retrieve(id, params={})
if !id
raise ArgumentError.new("Missing ID.")
end
response = @client.request(:get, "#{endpoint()}/#{id}", params)
Util.convert_to_object(self, response[:body])
end
|
#set_endpoint_base(base) ⇒ Object
23
24
25
26
|
# File 'lib/invoiced/object.rb', line 23
def set_endpoint_base(base)
@endpoint_base = base
self
end
|
103
104
105
106
107
108
|
# File 'lib/invoiced/object.rb', line 103
def to_hash
@values.inject({}) do |acc, (key, value)|
acc[key] = value.respond_to?(:to_hash) ? value.to_hash : value
acc
end
end
|
#to_json(*a) ⇒ Object
99
100
101
|
# File 'lib/invoiced/object.rb', line 99
def to_json(*a)
JSON.generate(@values)
end
|
#to_s(*args) ⇒ Object
54
55
56
|
# File 'lib/invoiced/object.rb', line 54
def to_s(*args)
JSON.pretty_generate(@values)
end
|
95
96
97
|
# File 'lib/invoiced/object.rb', line 95
def values
@values.values
end
|