Sha256: 174048ca6d2da2da61f771102f99e5840a9492119aebe50a272d618ff272bb8a

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 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 = NameResolver.resolve(subclass.name)
          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
        get_scope.destroy(self.id)
      end

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

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

      def remote_create
        get_scope.create(payload)
      end

      def get_scope
        original_scope || self.class
      end

    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
api_client-0.5.1 lib/api_client/resource/base.rb
api_client-0.5.0 lib/api_client/resource/base.rb
api_client-0.4.3 lib/api_client/resource/base.rb
api_client-0.4.2 lib/api_client/resource/base.rb
api_client-0.4.1 lib/api_client/resource/base.rb
api_client-0.3.3 lib/api_client/resource/base.rb