Module: Harpy::Resource
- Extended by:
- ActiveSupport::Concern
- Included in:
- Collection
- Defined in:
- lib/harpy/resource.rb
Defined Under Namespace
Modules: ClassMethods
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) ⇒ Object
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/harpy/resource.rb', line 238
def method_missing(method, *args)
key = method.to_s
if persisted? && !@attrs.has_key?(key)
super
elsif key=~/[\]=]\z/
super
else
@attrs[key]
end
end
|
Class Method Details
.from_url(hash) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/harpy/resource.rb', line 21
def self.from_url(hash)
results = {}
hash.each do |klass, urls|
results[klass] = Harpy.client.get [*urls]
end
Harpy.client.run results.values.flatten
results.each do |klass, requests|
requests.collect! do |request|
klass.send :from_url_handler, request.response
end
end
results
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
199
200
201
|
# File 'lib/harpy/resource.rb', line 199
def ==(other)
other.equal?(self) || (urn && other.instance_of?(self.class) && other.urn == urn)
end
|
#as_json(*args) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/harpy/resource.rb', line 140
def as_json(*args)
hash = @attrs.dup
hash.delete "link"
hash.delete "urn"
hash
end
|
#attributes=(attrs) ⇒ Object
130
131
132
133
134
135
136
137
138
|
# File 'lib/harpy/resource.rb', line 130
def attributes=(attrs)
attrs.each do |key, value|
if respond_to? "#{key}="
send "#{key}=", value
else
@attrs[key] = value
end
end
end
|
#destroy ⇒ Object
159
160
161
162
163
164
|
# File 'lib/harpy/resource.rb', line 159
def destroy
raise Harpy::UrlRequired unless url
run_callbacks :destroy do
process_response Harpy.client.delete(url), :destroy
end
end
|
#has_key?(key) ⇒ Boolean
191
192
193
|
# File 'lib/harpy/resource.rb', line 191
def has_key?(key)
@attrs.has_key? key.to_s
end
|
#hash ⇒ Object
195
196
197
|
# File 'lib/harpy/resource.rb', line 195
def hash
urn.hash
end
|
#id ⇒ Object
179
180
181
|
# File 'lib/harpy/resource.rb', line 179
def id
@attrs["urn"].split(":").last if @attrs["urn"]
end
|
#initialize(attrs = nil) ⇒ Object
125
126
127
128
|
# File 'lib/harpy/resource.rb', line 125
def initialize(attrs = nil)
@attrs = {}
self.attributes = attrs || {}
end
|
#inspect ⇒ Object
187
188
189
|
# File 'lib/harpy/resource.rb', line 187
def inspect
"<#{self.class.name} @attrs:#{@attrs.inspect} @errors:#{errors.full_messages.inspect} persisted:#{persisted?}>"
end
|
#link(rel) ⇒ Object
166
167
168
169
|
# File 'lib/harpy/resource.rb', line 166
def link(rel)
link = (@attrs["link"]||[]).detect{|l| l["rel"]==rel.to_s}
link["href"] if link
end
|
#persisted? ⇒ Boolean
183
184
185
|
# File 'lib/harpy/resource.rb', line 183
def persisted?
@attrs["urn"].present?
end
|
#save ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/harpy/resource.rb', line 147
def save
if valid?
run_callbacks :save do
json = Yajl::Encoder.encode as_json
raise BodyToBig, "Size: #{json.bytesize} bytes (max 1MB)" if json.bytesize > 1.megabyte
persisted? ? update(json) : create(json)
end
else
false
end
end
|
#url ⇒ Object
171
172
173
|
# File 'lib/harpy/resource.rb', line 171
def url
link "self"
end
|
#url_collection ⇒ Object
175
176
177
|
# File 'lib/harpy/resource.rb', line 175
def url_collection
Harpy.entry_point.resource_url self.class.resource_name
end
|