Sha256: 6ed0fe43a2db048c517dd072eee979616326ecda728a6cbe261c4fc92cde6c96

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 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 = {})
          attrs.each{|k,v| public_send(:"#{k}=", v)}
          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

1 entries across 1 versions & 1 rubygems

Version Path
spark_api-1.3.25 lib/spark_api/models/concerns/savable.rb