Module: Harpy::Resource::InstanceMethods

Defined in:
lib/harpy/resource.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



253
254
255
256
257
258
259
260
261
262
# File 'lib/harpy/resource.rb', line 253

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



210
211
212
# File 'lib/harpy/resource.rb', line 210

def ==(other)
  other.equal?(self) || (urn && other.instance_of?(self.class) && other.urn == urn)
end

#as_json(*args) ⇒ Object



151
152
153
154
155
156
# File 'lib/harpy/resource.rb', line 151

def as_json(*args)
  hash = @attrs.dup
  hash.delete "link"
  hash.delete "urn"
  hash
end

#attributes=(attrs) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/harpy/resource.rb', line 141

def attributes=(attrs)
  attrs.each do |key, value|
    if respond_to? "#{key}="
      send "#{key}=", value
    else
      @attrs[key] = value
    end
  end
end

#destroyObject

Raises:



170
171
172
173
174
175
# File 'lib/harpy/resource.rb', line 170

def destroy
  raise Harpy::UrlRequired unless url
  run_callbacks :destroy do
    process_response Harpy.client.delete(url), :destroy
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/harpy/resource.rb', line 202

def has_key?(key)
  @attrs.has_key? key.to_s
end

#hashObject



206
207
208
# File 'lib/harpy/resource.rb', line 206

def hash
  urn.hash
end

#idObject



190
191
192
# File 'lib/harpy/resource.rb', line 190

def id
  @attrs["urn"].split(":").last if @attrs["urn"]
end

#initialize(attrs = nil) ⇒ Object



136
137
138
139
# File 'lib/harpy/resource.rb', line 136

def initialize(attrs = nil)
  @attrs = {}
  self.attributes = attrs || {}
end

#inspectObject



198
199
200
# File 'lib/harpy/resource.rb', line 198

def inspect
  "<#{self.class.name} @attrs:#{@attrs.inspect} @errors:#{errors.full_messages.inspect} persisted:#{persisted?}>"
end


177
178
179
180
# File 'lib/harpy/resource.rb', line 177

def link(rel)
  link = (@attrs["link"]||[]).detect{|l| l["rel"]==rel.to_s}
  link["href"] if link
end

#persisted?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/harpy/resource.rb', line 194

def persisted?
  @attrs["urn"].present?
end

#saveObject



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/harpy/resource.rb', line 158

def save
  if valid?
    run_callbacks :save do
      json = JSON.generate 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

#urlObject



182
183
184
# File 'lib/harpy/resource.rb', line 182

def url
  link "self"
end

#url_collectionObject



186
187
188
# File 'lib/harpy/resource.rb', line 186

def url_collection
  Harpy.entry_point.resource_url self.class.resource_name
end