Sha256: 82a3076dd8a331cf0973fddeb4b4e79aad0c0108514a4d23aa9e3d95cd2bd066

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

module RemoteResource
  module Querying
    module PersistenceMethods
      extend ActiveSupport::Concern

      module ClassMethods

        def create(attributes = {}, connection_options = {})
          resource = new attributes
          response = RemoteResource::Request.new(self, :post, attributes, connection_options).perform
          resource.handle_response response
        end

        def destroy(id, connection_options = {})
          resource = new
          response = RemoteResource::Request.new(self, :delete, { id: id }, connection_options).perform
          resource.handle_response(response)
        end
      end

      def update_attributes(attributes = {}, connection_options = {})
        rebuild_resource attributes
        attributes.reverse_merge! id: id
        create_or_update attributes, connection_options
        success? ? self : false
      end

      def save(connection_options = {})
        create_or_update self.attributes, connection_options
        success? ? self : false
      end

      def create_or_update(attributes = {}, connection_options = {})
        if attributes.has_key? :id
          response = RemoteResource::Request.new(self, :patch, attributes, connection_options).perform
        else
          response = RemoteResource::Request.new(self, :post, attributes, connection_options).perform
        end
        handle_response response
      end

      def destroy(connection_options = {})
        id.present? || raise(RemoteResource::IdMissingError.new("`id` is missing from resource"))
        response = RemoteResource::Request.new(self, :delete, { id: id }, connection_options).perform
        handle_response(response)
        success? ? self : false
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddy_remote_resource-0.4.9 lib/remote_resource/querying/persistence_methods.rb
ddy_remote_resource-0.4.8 lib/remote_resource/querying/persistence_methods.rb