Sha256: ff7ff759599efa610e5a141653d6c9018fbbca14ae96089673953edb4ef7264b

Contents?: true

Size: 1.87 KB

Versions: 15

Compression:

Stored size: 1.87 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(results.first)
          reset_dirty
          params_for_save.clear
          true
        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(result)
          attributes['Id'] = result['Id'] ? result['Id'] : parse_id(result['ResourceUri'])
          result.delete('Id')
          attributes.merge! result
        end

        # can be overridden
        def resource_pluralized
          resource = self.class.name.split('::').last
          unless resource.split('').last == "s"
            resource = resource + "s"
          end
          resource
        end

      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spark_api-1.3.24 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.23 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.21 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.20 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.19 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.18 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.17 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.16 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.15 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.14 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.13 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.10 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.9 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.6 lib/spark_api/models/concerns/savable.rb
spark_api-1.3.3 lib/spark_api/models/concerns/savable.rb