Class: JsonApiClient::Query::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Builder

Returns a new instance of Builder.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/json_api_client/query/builder.rb', line 8

def initialize(klass)
  @klass = klass
  @primary_key = nil
  @pagination_params = {}
  @path_params = {}
  @additional_params = {}
  @filters = {}
  @includes = []
  @orders = []
  @fields = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



103
104
105
# File 'lib/json_api_client/query/builder.rb', line 103

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.



5
6
7
# File 'lib/json_api_client/query/builder.rb', line 5

def klass
  @klass
end

Instance Method Details

#buildObject



72
73
74
# File 'lib/json_api_client/query/builder.rb', line 72

def build
  klass.new(params)
end

#find(args = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/json_api_client/query/builder.rb', line 92

def find(args = {})
  case args
  when Hash
    where(args)
  else
    @primary_key = args
  end

  klass.requestor.get(params)
end

#firstObject



64
65
66
# File 'lib/json_api_client/query/builder.rb', line 64

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

#includes(*tables) ⇒ Object



32
33
34
35
# File 'lib/json_api_client/query/builder.rb', line 32

def includes(*tables)
  @includes += parse_related_links(*tables)
  self
end

#lastObject



68
69
70
# File 'lib/json_api_client/query/builder.rb', line 68

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

#order(*args) ⇒ Object



27
28
29
30
# File 'lib/json_api_client/query/builder.rb', line 27

def order(*args)
  @orders += parse_orders(*args)
  self
end

#page(number) ⇒ Object



49
50
51
52
# File 'lib/json_api_client/query/builder.rb', line 49

def page(number)
  @pagination_params[ klass.paginator.page_param ] = number
  self
end

#paginate(conditions = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/json_api_client/query/builder.rb', line 42

def paginate(conditions = {})
  scope = self
  scope = scope.page(conditions[:page]) if conditions[:page]
  scope = scope.per(conditions[:per_page]) if conditions[:per_page]
  scope
end

#paramsObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/json_api_client/query/builder.rb', line 76

def params
  filter_params
    .merge(pagination_params)
    .merge(includes_params)
    .merge(order_params)
    .merge(select_params)
    .merge(primary_key_params)
    .merge(path_params)
    .merge(additional_params)
end

#per(size) ⇒ Object



54
55
56
57
# File 'lib/json_api_client/query/builder.rb', line 54

def per(size)
  @pagination_params[ klass.paginator.per_page_param ] = size
  self
end

#select(*fields) ⇒ Object



37
38
39
40
# File 'lib/json_api_client/query/builder.rb', line 37

def select(*fields)
  @fields += parse_fields(*fields)
  self
end

#to_aObject Also known as: all



87
88
89
# File 'lib/json_api_client/query/builder.rb', line 87

def to_a
  @to_a ||= find
end

#where(conditions = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/json_api_client/query/builder.rb', line 20

def where(conditions = {})
  # pull out any path params here
  @path_params.merge!(conditions.slice(*klass.prefix_params))
  @filters.merge!(conditions.except(*klass.prefix_params))
  self
end

#with_params(more_params) ⇒ Object



59
60
61
62
# File 'lib/json_api_client/query/builder.rb', line 59

def with_params(more_params)
  @additional_params.merge!(more_params)
  self
end