Class: Alula::FilterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/filter_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, query_interface, **args) ⇒ FilterBuilder

Returns a new instance of FilterBuilder.



8
9
10
11
12
# File 'lib/alula/filter_builder.rb', line 8

def initialize(model_class, query_interface, **args)
  self.query_interface = query_interface
  self.model_class = model_class
  @cache = Hash.new
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



6
7
8
# File 'lib/alula/filter_builder.rb', line 6

def builder
  @builder
end

#model_classObject

Returns the value of attribute model_class.



6
7
8
# File 'lib/alula/filter_builder.rb', line 6

def model_class
  @model_class
end

#query_interfaceObject

Returns the value of attribute query_interface.



6
7
8
# File 'lib/alula/filter_builder.rb', line 6

def query_interface
  @query_interface
end

Instance Method Details

#and(fields) ⇒ Object

Takes a hash, for each key => value pair returns a strict hash response for a $and $where query



56
57
58
59
60
61
62
63
64
65
# File 'lib/alula/filter_builder.rb', line 56

def and(fields)
  cleaned = fields.each_pair.each_with_object({}) do |(key, value), collector|
    field_name = validate_field_filterability! key
    field_value = simple_value! value

    collector[field_name] = field_value
  end

  update_and_return({ '$and' => cleaned })
end

#as_jsonObject



173
174
175
# File 'lib/alula/filter_builder.rb', line 173

def as_json
  @cache
end

#between(**args) ⇒ Object

Generate a $between query for hash, the values of the hash must be an array with 2 elements that are JSON-encodable



125
126
127
128
129
130
131
132
133
# File 'lib/alula/filter_builder.rb', line 125

def between(**args)
  new_values = args.each_pair.each_with_object({}) do |(key, range), collector|
    field_name = validate_field_filterability! key
    range_start, range_end = validate_range_value! range, field_name
    collector[field_name] = { '$between' => [range_start, range_end] }
  end

  update_and_return(new_values)
end

#in(args = {}) ⇒ Object

Generate an $in query for hash, the values of the hash must be an array that is JSON-encodable



151
152
153
154
155
156
157
158
159
# File 'lib/alula/filter_builder.rb', line 151

def in(args = {})
  new_values = args.each_pair.each_with_object({}) do |(key, range), collector|
    field_name = validate_field_filterability! key
    values = simple_values! range
    collector[field_name] = { '$in' => values }
  end

  update_and_return(new_values)
end

#not_between(args = {}) ⇒ Object

Generate a $not_between query for hash, the values of the hash must be an array with 2 elements that are JSON-encodable



138
139
140
141
142
143
144
145
146
# File 'lib/alula/filter_builder.rb', line 138

def not_between(args = {})
  new_values = args.each_pair.each_with_object({}) do |(key, range), collector|
    field_name = validate_field_filterability! key
    range_start, range_end = validate_range_value! range, field_name
    collector[field_name] = { '$notBetween' => [range_start, range_end] }
  end

  update_and_return(new_values)
end

#not_in(args = {}) ⇒ Object

Generate an $in query for hash, the values of the hash must be an array that is JSON-encodable



163
164
165
166
167
168
169
170
171
# File 'lib/alula/filter_builder.rb', line 163

def not_in(args = {})
  new_values = args.each_pair.each_with_object({}) do |(key, range), collector|
    field_name = validate_field_filterability! key
    values = simple_values! range
    collector[field_name] = { '$notIn' => values }
  end

  update_and_return(new_values)
end

#or(*args, &block) ⇒ Object

Dual operation If given a block it will yield a fresh instance of FilterBuilder, the contents of which will be stuffed directly into the $or operator. If given an array, the 1st value is



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/alula/filter_builder.rb', line 94

def or(*args, &block)
  if block_given?
    new_builder = yield self.class.new(model_class, query_interface)
    return update_and_return({ '$or' => new_builder.as_json })
  end

  value = args[0]
  fields = args[1..-1]

  new_values = fields.each_with_object('$or'=>{}) do |key, collector|
    field_name = validate_field_filterability! key
    collector['$or'][field_name] = value
  end

  update_and_return(new_values)
end

#or_like(args = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/alula/filter_builder.rb', line 111

def or_like(args = {})
  new_values = args.each_pair.each_with_object({}) do |(key, value), collector|
    field_name = validate_field_filterability! key
    field_value = simple_value! value

    collector[field_name] = { '$like' => field_value }
  end

  update_and_return('$or' => new_values)
end

#sort(args) ⇒ Object

Takes a hash, one for each key



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alula/filter_builder.rb', line 28

def sort(args)
  cleaned_sorts = args.each_pair.each_with_object({}) do |(field, value), collector|
    field_name = validate_field_sortability! field
    field_value = validate_sort_value! value

    collector[field_name] = field_value
  end

  query_interface.filter(as_json)
  query_interface.query_sort(cleaned_sorts)
end

#where(fields) ⇒ Object

Takes a hash, for each key => value pair returns a strict hash response for a $where query



43
44
45
46
47
48
49
50
51
52
# File 'lib/alula/filter_builder.rb', line 43

def where(fields)
  cleaned = fields.each_pair.each_with_object({}) do |(key, value), collector|
    field_name = validate_field_filterability! key
    field_value = simple_value! value

    collector[field_name] = field_value
  end

  update_and_return(cleaned)
end