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.
- #batched_each(size, &block) ⇒ Object
- #batches(size, &block) ⇒ Object
- #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 |
# File 'lib/schron/query.rb', line 73 def all with_pagination(archive.exec_query(self)) end |
#batched_each(size, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/schron/query.rb', line 89 def batched_each(size, &block) enum = Enumerator.new do |y| batches(size) do |group| group.each do |item| y << item end end end block_given? ? enum.each(&block) : enum end |
#batches(size, &block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/schron/query.rb', line 102 def batches(size, &block) enum = Enumerator.new do |y| current_page = 1 begin results = page(current_page, per_page: size).all y << results current_page = results.paging.next_page end while current_page end block_given? ? enum.each(&block) : enum end |
#count ⇒ Object
81 82 83 |
# File 'lib/schron/query.rb', line 81 def count archive.exec_count(self) end |
#datastore ⇒ Object
23 24 25 |
# File 'lib/schron/query.rb', line 23 def datastore archive.datastore end |
#exists? ⇒ Boolean
85 86 87 |
# File 'lib/schron/query.rb', line 85 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
77 78 79 |
# File 'lib/schron/query.rb', line 77 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 |