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
# File 'lib/json_api_client/query/builder.rb', line 7

def initialize(klass)
  @klass = klass
  @primary_key = nil
  @pagination_params = {}
  @path_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



91
92
93
# File 'lib/json_api_client/query/builder.rb', line 91

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



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

def build
  klass.new(params)
end

#find(args = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/json_api_client/query/builder.rb', line 80

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

  klass.requestor.get(params)
end

#firstObject



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

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

#includes(*tables) ⇒ Object



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

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

#order(*args) ⇒ Object



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

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

#page(number) ⇒ Object



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

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

#paginate(conditions = {}) ⇒ Object



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

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



65
66
67
68
69
70
71
72
73
# File 'lib/json_api_client/query/builder.rb', line 65

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

#per(size) ⇒ Object



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

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

#select(fields) ⇒ Object



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

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

#to_aObject Also known as: all



75
76
77
# File 'lib/json_api_client/query/builder.rb', line 75

def to_a
  @to_a ||= find
end

#where(conditions = {}) ⇒ Object



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

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