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.



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

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



98
99
100
# File 'lib/json_api_client/query/builder.rb', line 98

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



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

def build
  klass.new(params)
end

#find(args = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/json_api_client/query/builder.rb', line 87

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

  klass.requestor.get(params)
end

#firstObject



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

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

#includes(*tables) ⇒ Object



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

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

#order(*args) ⇒ Object



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

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

#page(number) ⇒ Object



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

def page(number)
  @pagination_params[:number] = number
  self
end

#paginate(conditions = {}) ⇒ Object



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

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/json_api_client/query/builder.rb', line 71

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



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

def per(size)
  @pagination_params[:size] = size
  self
end

#select(fields) ⇒ Object



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

def select(fields)
  @fields += fields.split(",").map(&:strip)
  self
end

#to_aObject Also known as: all



82
83
84
# File 'lib/json_api_client/query/builder.rb', line 82

def to_a
  @to_a ||= find
end

#where(conditions = {}) ⇒ Object



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

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



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

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