Sha256: c8335efd085e4181fc93fe097b73f72430066627c4e2efe82489a3e27b5258a9

Contents?: true

Size: 803 Bytes

Versions: 25

Compression:

Stored size: 803 Bytes

Contents

module JsonApiClient
  class Scope
    attr_reader :klass, :params

    def initialize(klass)
      @klass = klass
      @params = {}
    end

    def where(conditions = {})
      @params.merge!(conditions)
      self
    end
    alias paginate where

    def order(conditions)
      where(order: conditions)
    end

    def includes(*tables)
      @params[:includes] ||= []
      @params[:includes] += tables.flatten
      self
    end

    def page(number)
      where(page: number)
    end

    def first
      paginate(page: 1, per_page: 1).to_a.first
    end

    def build
      klass.new(params)
    end

    def to_a
      @to_a ||= klass.find(params)
    end
    alias all to_a

    def method_missing(method_name, *args, &block)
      to_a.send(method_name, *args, &block)
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/scope.rb
json_api_client-0.9.5 lib/json_api_client/scope.rb
json_api_client-0.9.4 lib/json_api_client/scope.rb
json_api_client-0.9.3 lib/json_api_client/scope.rb
json_api_client-0.9.2 lib/json_api_client/scope.rb
json_api_client-0.9.0 lib/json_api_client/scope.rb
json_api_client-0.8.1 lib/json_api_client/scope.rb
json_api_client-0.8.0 lib/json_api_client/scope.rb
json_api_client-0.7.1 lib/json_api_client/scope.rb
json_api_client-0.7.0 lib/json_api_client/scope.rb
json_api_client-0.6.0 lib/json_api_client/scope.rb
json_api_client-0.5.1 lib/json_api_client/scope.rb
json_api_client-0.5.0 lib/json_api_client/scope.rb
json_api_client-0.4.0 lib/json_api_client/scope.rb
json_api_client-0.3.1 lib/json_api_client/scope.rb
json_api_client-0.3.0 lib/json_api_client/scope.rb
json_api_client-0.2.4 lib/json_api_client/scope.rb
json_api_client-0.2.3 lib/json_api_client/scope.rb
json_api_client-0.2.2 lib/json_api_client/scope.rb
json_api_client-0.2.1 lib/json_api_client/scope.rb