module ObjectJSONMapper::Persistence
Public Class Methods
included(base)
click to toggle source
# File lib/object_json_mapper/persistence.rb, line 3 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
destroy()
click to toggle source
@result [TrueClass,FalseClass]
# File lib/object_json_mapper/persistence.rb, line 56 def destroy client.delete true rescue RestClient::ExceptionWithResponse false end
Also aliased as: delete
reload()
click to toggle source
@return [ObjectJSONMapper::Base]
# File lib/object_json_mapper/persistence.rb, line 66 def reload tap do |base| base.attributes = HTTP.parse_json(client.get.body) if reloadable? end end
save(*)
click to toggle source
@return [ObjectJSONMapper::Base,FalseClass]
# File lib/object_json_mapper/persistence.rb, line 8 def save(*) return update(attributes) if persisted? response = self.class.client.post(attributes) result = if response.headers[:location] RestClient.get(response.headers[:location], ObjectJSONMapper.headers) else response.body end persist errors.clear attributes.merge!(HTTP.parse_json(result)) self rescue RestClient::ExceptionWithResponse => e raise e unless e.response.code == 422 load_errors(HTTP.parse_json(e.response.body)) false ensure validate end
Also aliased as: save!
update(params = {})
click to toggle source
@param params [Hash] @return [ObjectJSONMapper::Base,FalseClass]
# File lib/object_json_mapper/persistence.rb, line 37 def update(params = {}) return false if new_record? client.patch(params) reload errors.clear self rescue RestClient::ExceptionWithResponse => e raise e unless e.response.code == 422 load_errors(HTTP.parse_json(e.response.body)) false end
Also aliased as: update_attributes