Class: JsonApiClient::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Scope

Returns a new instance of Scope.



5
6
7
8
# File 'lib/json_api_client/scope.rb', line 5

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



43
44
45
# File 'lib/json_api_client/scope.rb', line 43

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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/json_api_client/scope.rb', line 3

def klass
  @klass
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/json_api_client/scope.rb', line 3

def params
  @params
end

Instance Method Details

#buildObject



34
35
36
# File 'lib/json_api_client/scope.rb', line 34

def build
  klass.new(params)
end

#firstObject



30
31
32
# File 'lib/json_api_client/scope.rb', line 30

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

#includes(*tables) ⇒ Object



20
21
22
23
24
# File 'lib/json_api_client/scope.rb', line 20

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

#order(conditions) ⇒ Object



16
17
18
# File 'lib/json_api_client/scope.rb', line 16

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

#page(number) ⇒ Object



26
27
28
# File 'lib/json_api_client/scope.rb', line 26

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

#to_aObject Also known as: all



38
39
40
# File 'lib/json_api_client/scope.rb', line 38

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

#where(conditions = {}) ⇒ Object Also known as: paginate



10
11
12
13
# File 'lib/json_api_client/scope.rb', line 10

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