Class: Backlex::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/backlex/query_builder.rb

Overview

Chainable builder that compiles to a ListQuery hash and runs it.

Instance Method Summary collapse

Constructor Details

#initialize(list_fn) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



6
7
8
9
# File 'lib/backlex/query_builder.rb', line 6

def initialize(list_fn)
  @list_fn = list_fn
  @q = { filter: nil, sort: [], fields: [], expand: [], limit: nil, offset: nil, meta: nil, locale: nil, q: nil }
end

Instance Method Details

#expand(*rels) ⇒ Object

Inline single-hop relations (replaces each FK with the related object).



33
34
35
36
# File 'lib/backlex/query_builder.rb', line 33

def expand(*rels)
  @q[:expand].concat(rels)
  self
end

#filter(cond) ⇒ Object

Replace the filter with a raw canonical condition (escape hatch).



17
18
19
20
# File 'lib/backlex/query_builder.rb', line 17

def filter(cond)
  @q[:filter] = Filter.normalize(cond)
  self
end

#limit(n) ⇒ Object



50
51
52
53
# File 'lib/backlex/query_builder.rb', line 50

def limit(n)
  @q[:limit] = n
  self
end

#listObject



71
72
73
# File 'lib/backlex/query_builder.rb', line 71

def list
  @list_fn.call(@q)
end

#locale(loc) ⇒ Object

Project i18n_text fields to one locale, or “*” for the full map.



39
40
41
42
# File 'lib/backlex/query_builder.rb', line 39

def locale(loc)
  @q[:locale] = loc
  self
end

#offset(n) ⇒ Object



55
56
57
58
# File 'lib/backlex/query_builder.rb', line 55

def offset(n)
  @q[:offset] = n
  self
end

#order_by(*sorts) ⇒ Object



27
28
29
30
# File 'lib/backlex/query_builder.rb', line 27

def order_by(*sorts)
  @q[:sort].concat(sorts)
  self
end

#search(text) ⇒ Object

Free-text search across readable text fields.



45
46
47
48
# File 'lib/backlex/query_builder.rb', line 45

def search(text)
  @q[:q] = text
  self
end

#select(*fields) ⇒ Object



22
23
24
25
# File 'lib/backlex/query_builder.rb', line 22

def select(*fields)
  @q[:fields].concat(fields)
  self
end

#to_queryObject

The assembled ListQuery hash — the canonical input the API takes.



67
68
69
# File 'lib/backlex/query_builder.rb', line 67

def to_query
  @q
end

#where(cond) ⇒ Object



11
12
13
14
# File 'lib/backlex/query_builder.rb', line 11

def where(cond)
  @q[:filter] = Filter.normalize(cond)
  self
end

#with_meta(m) ⇒ Object

Request an extra COUNT: “filter_count”, “total_count”, or “*”.



61
62
63
64
# File 'lib/backlex/query_builder.rb', line 61

def with_meta(m)
  @q[:meta] = m
  self
end