Sha256: 2e22463fc6c6e9fc8a46f6270219d91db927b1c1c2306f1169a81818b99ba662

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 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
        scope = original_scope || self.class
        scope.update(self.id, payload)
      end

      def remote_create
        self.class.create(payload)
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
api_client-0.3.0 lib/api_client/resource/base.rb
api_client-0.2.0 lib/api_client/resource/base.rb
api_client-0.1.9 lib/api_client/resource/base.rb