Class: ClassicApi::Query::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Builder

delegate :key_formatter, to: :klass



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/classic_api/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



106
107
108
# File 'lib/classic_api/query/builder.rb', line 106

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.



4
5
6
# File 'lib/classic_api/query/builder.rb', line 4

def klass
  @klass
end

Instance Method Details

#buildObject



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

def build
  klass.new(params)
end

#find(args = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/classic_api/query/builder.rb', line 95

def find(args = {})
  case args
  when Hash
    where(args)
    klass.requestor.get(params)
  else
    @primary_key = args
    klass.requestor.get(params).first
  end
end

#firstObject



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

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

#includes(*tables) ⇒ Object



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

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

#lastObject



71
72
73
# File 'lib/classic_api/query/builder.rb', line 71

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

#order(*args) ⇒ Object



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

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

#page(number) ⇒ Object



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

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

#paginate(conditions = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/classic_api/query/builder.rb', line 45

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



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

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



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

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

#select(*fields) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/classic_api/query/builder.rb', line 36

def select(*fields)
  fields = Array(fields).flatten
  fields = fields.map { |i| i.to_s.split(",") }.flatten

  @fields += fields.map(&:strip)

  self
end

#to_aObject Also known as: all



90
91
92
# File 'lib/classic_api/query/builder.rb', line 90

def to_a
  @to_a ||= find
end

#where(conditions = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/classic_api/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



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

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