Module: LazyResource::Resource
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary
Attributes included from Mapping
#fetched, #other_attributes, #persisted
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Attributes
#primary_key
Methods included from Mapping
#fetched?, #load, root_node_name
#collection_path, #collection_url, #element_path, #element_url, #new_element_path, #split_options
Class Method Details
26
27
28
|
# File 'lib/lazy_resource/resource.rb', line 26
def self.
@default_headers || {}
end
|
22
23
24
|
# File 'lib/lazy_resource/resource.rb', line 22
def self.()
@default_headers =
end
|
.root_node_name=(node_name) ⇒ Object
.site ⇒ Object
18
19
20
|
# File 'lib/lazy_resource/resource.rb', line 18
def self.site
@site
end
|
.site=(site) ⇒ Object
14
15
16
|
# File 'lib/lazy_resource/resource.rb', line 14
def self.site=(site)
@site = site
end
|
Instance Method Details
#==(other) ⇒ Object
Tests for equality. Returns true iff other
is the same object or other is an instance of the same class and has the same attributes.
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/lazy_resource/resource.rb', line 125
def ==(other)
return true if other.equal?(self)
return false unless other.instance_of?(self.class)
self.class.attributes.inject(true) do |memo, attribute|
attribute_name = attribute.first
attribute_type = attribute.last[:type]
if attribute_type.include?(LazyResource::Resource) || (attribute_type.is_a?(::Array) && attribute_type.first.include?(LazyResource::Resource))
memo
else
memo && self.send(:"#{attribute_name}") == other.send(:"#{attribute_name}")
end
end
end
|
#as_json(options = {}) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/lazy_resource/resource.rb', line 205
def as_json(options={})
self.class.attributes.inject({}) do |hash, (attribute_name, attribute_options)|
attribute_type = attribute_options[:type]
unless self.instance_variable_get("@#{attribute_name}").nil?
value = self.send(:"#{attribute_name}")
if (attribute_type.is_a?(::Array) && attribute_type.first.include?(LazyResource::Resource))
value = value.map { |v| v.as_json }
elsif attribute_type.include?(LazyResource::Resource)
value = value.as_json
elsif attribute_type == DateTime
if options[:include_time_ago_in_words] && defined?(TwitterCldr)
hash[:"#{attribute_name}_in_words"] = (DateTime.now - (DateTime.now - value).to_f).localize.ago.to_s
end
if options[:strftime]
value = self.send(attribute_name).strftime(options[:strftime])
end
if options[attribute_name.to_sym] && options[attribute_name.to_sym][:strftime]
value = self.send(attribute_name).strftime(options[attribute_name.to_sym][:strftime])
end
value = value.to_s unless value.is_a?(String)
end
hash[attribute_name.to_sym] = value
end
hash
end
end
|
#attribute_params ⇒ Object
197
198
199
200
201
202
203
|
# File 'lib/lazy_resource/resource.rb', line 197
def attribute_params
{ self.class.element_name.to_sym => changed_attributes.inject({}) do |hash, changed_attribute|
hash.tap do |hash|
hash[changed_attribute.first] = self.send(changed_attribute.first)
end
end }
end
|
#create ⇒ Object
164
165
166
167
168
169
170
171
|
# File 'lib/lazy_resource/resource.rb', line 164
def create
run_callbacks :create do
request = Request.new(self.collection_url, self, { :method => :post, :params => attribute_params })
self.class.request_queue.queue(request)
self.class.fetch_all
self.changed_attributes.clear
end
end
|
#destroy ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/lazy_resource/resource.rb', line 182
def destroy
run_callbacks :destroy do
request = Request.new(self.element_url, self, { :method => :delete })
self.class.request_queue.queue(request)
self.class.fetch_all
end
end
|
#eql?(other) ⇒ Boolean
142
143
144
|
# File 'lib/lazy_resource/resource.rb', line 142
def eql?(other)
self == other
end
|
#initialize(attributes = {}) ⇒ Object
117
118
119
120
121
|
# File 'lib/lazy_resource/resource.rb', line 117
def initialize(attributes={})
self.tap do |resource|
resource.load(attributes, false)
end
end
|
#new_record? ⇒ Boolean
Also known as:
new?
150
151
152
|
# File 'lib/lazy_resource/resource.rb', line 150
def new_record?
!persisted?
end
|
146
147
148
|
# File 'lib/lazy_resource/resource.rb', line 146
def persisted?
@persisted
end
|
#save ⇒ Object
156
157
158
159
160
161
162
|
# File 'lib/lazy_resource/resource.rb', line 156
def save
return true if !changed?
run_callbacks :save do
new_record? ? create : update
self.persisted = true
end
end
|
#update ⇒ Object
173
174
175
176
177
178
179
180
|
# File 'lib/lazy_resource/resource.rb', line 173
def update
run_callbacks :update do
request = Request.new(self.element_url, self, { :method => :put, :params => attribute_params })
self.class.request_queue.queue(request)
self.class.fetch_all
self.changed_attributes.clear
end
end
|
#update_attributes(attributes = {}) ⇒ Object
190
191
192
193
194
195
|
# File 'lib/lazy_resource/resource.rb', line 190
def update_attributes(attributes={})
attributes.each do |name, value|
self.send("#{name}=", value)
end
self.update
end
|