Class: Tickethub::Resource
- Inherits:
-
Object
- Object
- Tickethub::Resource
show all
- Defined in:
- lib/tickethub/resource.rb
Direct Known Subclasses
Address, App, App::Category, App::Subscription, Contact, Supplier, Supplier::Answer, Supplier::App, Supplier::Bill, Supplier::Booking, Supplier::Card, Supplier::Charge, Supplier::Coupon, Supplier::Customer, Supplier::Fee, Supplier::Invoice, Supplier::Message, Supplier::Option, Supplier::Order, Supplier::Payment, Supplier::Product, Supplier::Question, Supplier::Rate, Supplier::Receipt, Supplier::Reseller, Supplier::Scan, Supplier::Season, Supplier::Subscription, Supplier::Tax, Supplier::Ticket, Supplier::Tier, Supplier::User, Supplier::Variant, Supplier::Voucher, Token, User
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.all(params = {}) ⇒ Object
-
.association(key, klass, path: key) ⇒ Object
-
.attribute(key, options) ⇒ Object
-
.attributes ⇒ Object
-
.collection(key, klass, path: key, &block) ⇒ Object
-
.descendants ⇒ Object
-
.dump_value(key, value) ⇒ Object
-
.endpoint ⇒ Object
-
.inherited(descendant) ⇒ Object
-
.load(endpoint, attributes = nil) ⇒ Object
-
.load_value(key, value, object) ⇒ Object
-
.path(value, options = {}) ⇒ Object
-
.polymorphic(type, attribute = :type) ⇒ Object
-
.register_type(type, klass, attribute = :type) ⇒ Object
-
.registered_types ⇒ Object
-
.scope(key, proc = -> (params = {}) { self.scope key, params }) ⇒ Object
-
.scopes ⇒ Object
-
.serialize(attributes) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(endpoint, attributes = nil) ⇒ Resource
Returns a new instance of Resource.
193
194
195
196
197
198
|
# File 'lib/tickethub/resource.rb', line 193
def initialize(endpoint, attributes = nil)
@endpoint = endpoint
attributes ||= endpoint.get
self.load attributes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/tickethub/resource.rb', line 274
def method_missing(method, *arguments)
if match = method.to_s.match(/^(.+)(=|\?)$/)
key = match[1].to_sym
case match[2]
when '='
@attributes[key] = self.class
.load_value(key, arguments.first, self)
when "?"
!! @attributes[key]
end
else
@attributes.key?(method) ? @attributes[method] : super
end
end
|
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
191
192
193
|
# File 'lib/tickethub/resource.rb', line 191
def endpoint
@endpoint
end
|
Class Method Details
.all(params = {}) ⇒ Object
46
47
48
|
# File 'lib/tickethub/resource.rb', line 46
def self.all(params = {})
Tickethub::Collection.new self.endpoint, self, params
end
|
.association(key, klass, path: key) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/tickethub/resource.rb', line 162
def self.association(key, klass, path: key)
define_method key do
instance_variable_defined?("@#{key}") ? instance_variable_get("@#{key}") :
instance_variable_set("@#{key}", klass.load(@endpoint[path]))
end
define_method "#{key}=" do |value|
instance_variable_set "@#{key}", ((value.nil? || value.is_a?(klass)) ? value :
klass.load(@endpoint[path], value))
end
end
|
.attribute(key, options) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/tickethub/resource.rb', line 58
def self.attribute(key, options)
self.attributes[key] = options
self.descendants.each do |descendant|
descendant.attributes[key] = options
end
end
|
.attributes ⇒ Object
31
32
33
|
# File 'lib/tickethub/resource.rb', line 31
def attributes
@attributes ||= {}
end
|
.collection(key, klass, path: key, &block) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/tickethub/resource.rb', line 174
def self.collection(key, klass, path: key, &block)
define_method key do |params = {}|
(instance_variable_defined?("@#{key}") ? instance_variable_get("@#{key}") :
(sinleton? || id?) ? instance_variable_set("@#{key}",
Tickethub::Collection.new(@endpoint[path], klass, params)) : []).tap do |collection|
collection.instance_eval &block if block
end
end
define_method "#{key}=" do |values|
instance_variable_set "@#{key}", id??
(Tickethub::Collection.new(@endpoint[path], klass).tap do |collection|
collection.cache = values
end) : values.collect { |value| klass.load @endpoint, value }
end
end
|
.descendants ⇒ Object
35
36
37
|
# File 'lib/tickethub/resource.rb', line 35
def descendants
@descendants ||= []
end
|
.dump_value(key, value) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/tickethub/resource.rb', line 125
def self.dump_value(key, value)
return value unless self.attributes.key? key
case self.attributes[key][:type]
when :date then value.iso8601
when :datetime then value.iso8601
when :time then value.iso8601[10..-1]
when :duration then value.iso8601
when :money then value.fractional
when :timezone then value.zone
when :country then value.alpha2
when :currency then value.iso_code
else value
end
end
|
.endpoint ⇒ Object
18
19
20
21
|
# File 'lib/tickethub/resource.rb', line 18
def endpoint
return self.superclass.endpoint unless @path
Tickethub.endpoint[@path, @options]
end
|
.inherited(descendant) ⇒ Object
40
41
42
43
44
|
# File 'lib/tickethub/resource.rb', line 40
def self.inherited(descendant)
if descendant.ancestors.member? Tickethub::Resource
self.descendants.push descendant
end
end
|
.load(endpoint, attributes = nil) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/tickethub/resource.rb', line 146
def self.load(endpoint, attributes = nil)
attributes ||= endpoint.get
klass = registered_types.find do |type, options|
attributes[options[:attribute].to_s] == type
end
klass = klass ? klass[1][:klass] : self
if id = attributes['id']
endpoint = klass.endpoint[id, endpoint.options]
end
klass.new endpoint, attributes
end
|
.load_value(key, value, object) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
|
# File 'lib/tickethub/resource.rb', line 70
def self.load_value(key, value, object)
return nil if value.nil? || (value.is_a?(String) && value.empty?)
return value unless self.attributes.key? key
case self.attributes[key][:type]
when :date
case value
when String then ISO8601::Date.new(value)
else raise ArgumentError, 'invalid date value: ' + value
end
when :datetime
case value
when String then ISO8601::DateTime.new(value)
else raise ArgumentError, 'invalid datetime value: ' + value
end
when :time
case value
when String then ISO8601::Time.new(value)
else raise ArgumentError, 'invalid time value: ' + value
end
when :duration
case value
when String then ISO8601::Duration.new(value)
else raise ArgumentError, 'invalid time value: ' + value
end
when :money
currency = Money::Currency.new(object.currency)
raise ArgumentError, 'invalid money currency' if currency.blank?
case value
when Fixnum then Money.new(value, currency)
when Numeric, String
currency = Money::Currency.wrap(currency)
Money.new(value.to_d * currency.subunit_to_unit, currency)
else raise ArgumentError, 'invalid money value: ' + value
end
when :currency
case value
when String then Money::Currency.new(value)
else raise ArgumentError, 'invalid currency value: ' + value
end
when :timezone
case value
when String then Timezone::Zone.new(zone: value)
else raise ArgumentError, 'invalid timezone value: ' + value
end
when :country
case value
when String then Country.new(value)
else raise ArgumentError, 'invalid country value: ' + value
end
else value
end
end
|
.path(value, options = {}) ⇒ Object
13
14
15
16
|
# File 'lib/tickethub/resource.rb', line 13
def path(value, options = {})
@path = value
@options = options
end
|
.polymorphic(type, attribute = :type) ⇒ Object
50
51
52
|
# File 'lib/tickethub/resource.rb', line 50
def self.polymorphic(type, attribute = :type)
self.superclass.register_type type, self, attribute
end
|
.register_type(type, klass, attribute = :type) ⇒ Object
54
55
56
|
# File 'lib/tickethub/resource.rb', line 54
def self.register_type(type, klass, attribute = :type) self.registered_types[type] = { klass: klass, attribute: attribute }
end
|
.registered_types ⇒ Object
23
24
25
|
# File 'lib/tickethub/resource.rb', line 23
def registered_types
@registered_types ||= {}
end
|
.scope(key, proc = -> (params = {}) { self.scope key, params }) ⇒ Object
66
67
68
|
# File 'lib/tickethub/resource.rb', line 66
def self.scope(key, proc = -> (params = {}) { self.scope key, params })
self.scopes[key] = proc
end
|
.scopes ⇒ Object
27
28
29
|
# File 'lib/tickethub/resource.rb', line 27
def scopes
@scopes ||= {}
end
|
.serialize(attributes) ⇒ Object
140
141
142
143
144
|
# File 'lib/tickethub/resource.rb', line 140
def self.serialize(attributes)
attributes.collect do |key, value|
[key, dump_value(key, value)]
end.to_h
end
|
Instance Method Details
#==(other) ⇒ Object
240
241
242
|
# File 'lib/tickethub/resource.rb', line 240
def ==(other)
self.hash == other.hash
end
|
#[](key) ⇒ Object
232
233
234
|
# File 'lib/tickethub/resource.rb', line 232
def [](key)
send key
end
|
#[]=(key, value) ⇒ Object
236
237
238
|
# File 'lib/tickethub/resource.rb', line 236
def []=(key, value)
send "#{key}=", value
end
|
#destroy ⇒ Object
212
213
214
|
# File 'lib/tickethub/resource.rb', line 212
def destroy
self.load @endpoint.delete.decoded
end
|
#eql?(other) ⇒ Boolean
244
245
246
|
# File 'lib/tickethub/resource.rb', line 244
def eql?(other)
self == other
end
|
#errors ⇒ Object
256
257
258
|
# File 'lib/tickethub/resource.rb', line 256
def errors
@errors ||= Tickethub::Errors.new @attributes[:errors]
end
|
#hash ⇒ Object
248
249
250
|
# File 'lib/tickethub/resource.rb', line 248
def hash
id?? id.hash : super
end
|
#inspect ⇒ Object
268
269
270
|
# File 'lib/tickethub/resource.rb', line 268
def inspect
"#<#{self.class.name} #{to_h}>"
end
|
#load(attributes) ⇒ Object
224
225
226
227
228
229
230
|
# File 'lib/tickethub/resource.rb', line 224
def load(attributes)
@attributes = {}
attributes.each do |key, value|
send "#{key}=", value
end
return self
end
|
#reload! ⇒ Object
220
221
222
|
# File 'lib/tickethub/resource.rb', line 220
def reload!
self.load @endpoint.get
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
216
217
218
|
# File 'lib/tickethub/resource.rb', line 216
def respond_to?(method, include_priv = false)
@attributes.key?(method.to_s.remove(/[=\?]\Z/).to_sym) || super
end
|
#to_h ⇒ Object
260
261
262
|
# File 'lib/tickethub/resource.rb', line 260
def to_h
@attributes
end
|
#to_param ⇒ Object
252
253
254
|
# File 'lib/tickethub/resource.rb', line 252
def to_param
self.id
end
|
#to_s ⇒ Object
264
265
266
|
# File 'lib/tickethub/resource.rb', line 264
def to_s
self.id?? id : super
end
|
#update(attributes) ⇒ Object
204
205
206
207
208
209
210
|
# File 'lib/tickethub/resource.rb', line 204
def update(attributes)
self.load @endpoint.patch(attributes).decoded
return true
rescue Tickethub::ResourceInvalid => err
self.load Tickethub::Response.new(err.response).decoded
return false
end
|
#valid? ⇒ Boolean
200
201
202
|
# File 'lib/tickethub/resource.rb', line 200
def valid?
errors.nil? || errors.valid?
end
|