Class: Schron::Query
- Inherits:
-
Object
- Object
- Schron::Query
- Defined in:
- lib/schron/query.rb
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#sorts ⇒ Object
readonly
Returns the value of attribute sorts.
Instance Method Summary collapse
-
#all ⇒ Object
Query Terminators.
- #count ⇒ Object
- #datastore ⇒ Object
- #exists? ⇒ Boolean
-
#filter(attr, op = nil, val = nil) ⇒ Object
filter(:name, ‘=’, ‘David’) filter(:age, ‘>’, 18) filter(name: ‘David’, city: ‘Tahoma’) # => equivalent to filter(:name, ‘=’, ‘David’).filter(:city, ‘=’, ‘Tahoma’).
- #first ⇒ Object
-
#initialize(archive = nil) {|_self| ... } ⇒ Query
constructor
A new instance of Query.
- #kind ⇒ Object
-
#limit(val = nil) ⇒ Object
Fluent Builder.
- #offset(val = nil) ⇒ Object
- #page(page, per_page: 20) ⇒ Object
- #sort(attr, dir = :asc) ⇒ Object
Constructor Details
#initialize(archive = nil) {|_self| ... } ⇒ Query
Returns a new instance of Query.
11 12 13 14 15 16 17 |
# File 'lib/schron/query.rb', line 11 def initialize(archive=nil, &block) @archive = archive @filters = [] @sorts = [] @page = false yield self if block_given? end |
Instance Attribute Details
#archive ⇒ Object (readonly)
Returns the value of attribute archive.
9 10 11 |
# File 'lib/schron/query.rb', line 9 def archive @archive end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
7 8 9 |
# File 'lib/schron/query.rb', line 7 def filters @filters end |
#sorts ⇒ Object (readonly)
Returns the value of attribute sorts.
8 9 10 |
# File 'lib/schron/query.rb', line 8 def sorts @sorts end |
Instance Method Details
#all ⇒ Object
Query Terminators
73 74 75 76 77 |
# File 'lib/schron/query.rb', line 73 def all raw_results = datastore.exec_query(self) loaded_results = archive.load_all(raw_results) with_pagination(loaded_results) end |
#count ⇒ Object
83 84 85 |
# File 'lib/schron/query.rb', line 83 def count datastore.exec_count(self) end |
#datastore ⇒ Object
23 24 25 |
# File 'lib/schron/query.rb', line 23 def datastore archive.datastore end |
#exists? ⇒ Boolean
87 88 89 |
# File 'lib/schron/query.rb', line 87 def exists? count > 0 end |
#filter(attr, op = nil, val = nil) ⇒ Object
filter(:name, ‘=’, ‘David’) filter(:age, ‘>’, 18) filter(name: ‘David’, city: ‘Tahoma’) # => equivalent to
filter(:name, '=', 'David').filter(:city, '=', 'Tahoma')
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/schron/query.rb', line 54 def filter(attr, op=nil, val=nil) if op.nil? attr.each do |key, value| if value.kind_of?(Enumerable) @filters << [key.to_sym, 'in', value] else @filters << [key.to_sym, '=', value] end end else @filters << [attr.to_sym, op, val] end self end |
#first ⇒ Object
79 80 81 |
# File 'lib/schron/query.rb', line 79 def first limit(1).all.first end |
#kind ⇒ Object
19 20 21 |
# File 'lib/schron/query.rb', line 19 def kind archive.kind end |
#limit(val = nil) ⇒ Object
Fluent Builder
31 32 33 |
# File 'lib/schron/query.rb', line 31 def limit(val=nil) set_or_get(:limit, val) end |
#offset(val = nil) ⇒ Object
35 36 37 |
# File 'lib/schron/query.rb', line 35 def offset(val=nil) set_or_get(:offset, val) end |
#page(page, per_page: 20) ⇒ Object
39 40 41 42 |
# File 'lib/schron/query.rb', line 39 def page(page, per_page: 20) @page = page limit(per_page).offset((page - 1) * per_page) end |
#sort(attr, dir = :asc) ⇒ Object
44 45 46 47 48 |
# File 'lib/schron/query.rb', line 44 def sort(attr, dir=:asc) raise "invalid sort dir #{dir}, must be :asc or :desc" unless [:asc, :desc].include?(dir) @sorts << [attr.to_sym, dir] self end |