Sha256: a84c2b6ff75f27620e3a1bfd17d89f6197b31b99e54521131e8e63586981decc

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module ApiClient

  module Resource

    class Base < ApiClient::Base

      class << self
        extend ApiClient::Mixins::Delegation
        extend ApiClient::Mixins::Configuration

        delegate :find_all, :find, :create, :update, :destroy, :path, :to => :scope

        dsl_accessor :prefix

        def inherited(subclass)
          super
          small_name = subclass.name.split('::').last.downcase
          subclass.namespace small_name
          subclass.prefix    self.prefix
          subclass.always do
            name    = subclass.name.split('::').last.downcase
            pre_fix = prefix
            path ["", prefix, "#{name}s"].compact.join('/')
          end
        end

        def scope(options = {})
          scope_in_thread || ApiClient::Resource::Scope.new(self).params(options)
        end

      end

      def persisted?
        !!self.id
      end

      def save
        self.persisted? ? remote_update : remote_create
      end

      def destroy
        self.class.destroy(self.id)
      end

      def payload
        hash = self.to_hash
        hash.delete('id') # This key is never required
        hash
      end

      def remote_update
        self.class.update(self.id, payload)
      end

      def remote_create
        self.class.create(payload)
      end

    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
api_client-0.1.8 lib/api_client/resource/base.rb
api_client-0.1.7 lib/api_client/resource/base.rb
api_client-0.1.6 lib/api_client/resource/base.rb
api_client-0.1.5 lib/api_client/resource/base.rb
api_client-0.1.4 lib/api_client/resource/base.rb
api_client-0.1.3 lib/api_client/resource/base.rb
api_client-0.1.2 lib/api_client/resource/base.rb
api_client-0.1.1 lib/api_client/resource/base.rb
api_client-0.1.0 lib/api_client/resource/base.rb