Sha256: ea62a489853a2e4b6d3a0af7a9bdb0c8eb50de32a1b0f4e02052b95dd2d9cf6f
Contents?: true
Size: 1.77 KB
Versions: 47
Compression:
Stored size: 1.77 KB
Contents
module SparkApi module Models module Concerns module Savable def save(arguments = {}) self.errors = [] # clear the errors hash begin return save!(arguments) rescue BadResourceRequest => e self.errors << {:code => e.code, :message => e.message} SparkApi.logger.warn("Failed to save resource #{self}: #{e.message}") rescue NotFound => e SparkApi.logger.error("Failed to save resource #{self}: #{e.message}") end false end def save!(arguments = {}) persisted? ? update!(arguments) : create!(arguments) end def create!(arguments = {}) results = connection.post self.path, post_data.merge(params_for_save), arguments update_attributes_after_create(results.first) reset_dirty params_for_save.clear true end def update_attributes(attrs = {}, options = {}) load(attrs, options) save! end def update!(arguments = {}) return true unless changed? && persisted? connection.put resource_uri, dirty_attributes, arguments reset_dirty params_for_save.clear true end # extra params to be passed when saving def params_for_save @params_for_save ||= {} end # update/create hash (can be overridden) def post_data { resource_pluralized => [ attributes ] } end private def update_attributes_after_create(result) attributes['Id'] = result['Id'] ? result['Id'] : parse_id(result['ResourceUri']) result.delete('Id') attributes.merge! result end end end end end
Version data entries
47 entries across 47 versions & 1 rubygems