Class: JSONAPI::Consumer::Query::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, requestor = nil) ⇒ Builder

Returns a new instance of Builder.



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

def initialize(klass, requestor = nil)
  @klass = klass
  @requestor = requestor || klass.requestor
  @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



104
105
106
# File 'lib/jsonapi/consumer/query/builder.rb', line 104

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/jsonapi/consumer/query/builder.rb', line 5

def klass
  @klass
end

#requestorObject (readonly)

Returns the value of attribute requestor.



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

def requestor
  @requestor
end

Instance Method Details

#buildObject



73
74
75
# File 'lib/jsonapi/consumer/query/builder.rb', line 73

def build
  klass.new(params)
end

#find(args = {}) ⇒ Object



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

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

  requestor.get(params)
end

#firstObject



65
66
67
# File 'lib/jsonapi/consumer/query/builder.rb', line 65

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

#includes(*tables) ⇒ Object



33
34
35
36
# File 'lib/jsonapi/consumer/query/builder.rb', line 33

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

#lastObject



69
70
71
# File 'lib/jsonapi/consumer/query/builder.rb', line 69

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

#order(*args) ⇒ Object



28
29
30
31
# File 'lib/jsonapi/consumer/query/builder.rb', line 28

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

#page(number) ⇒ Object



50
51
52
53
# File 'lib/jsonapi/consumer/query/builder.rb', line 50

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

#paginate(conditions = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/jsonapi/consumer/query/builder.rb', line 43

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



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

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



55
56
57
58
# File 'lib/jsonapi/consumer/query/builder.rb', line 55

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

#select(*fields) ⇒ Object



38
39
40
41
# File 'lib/jsonapi/consumer/query/builder.rb', line 38

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

#to_aObject Also known as: all



88
89
90
# File 'lib/jsonapi/consumer/query/builder.rb', line 88

def to_a
  @to_a ||= find
end

#where(conditions = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/jsonapi/consumer/query/builder.rb', line 21

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



60
61
62
63
# File 'lib/jsonapi/consumer/query/builder.rb', line 60

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