Module: Plucky::Query::DSL
- Included in:
- Plucky::Query
- Defined in:
- lib/plucky/query.rb
Overview
finder DSL methods to delegate to your model if you’re building an ODM e.g. MyModel.last needs to be equivalent to MyModel.query.last
Instance Method Summary collapse
- #all(opts = {}) ⇒ Object (also: #to_a)
- #count(opts = {}) ⇒ Object (also: #size)
- #distinct(key, opts = {}) ⇒ Object
- #empty? ⇒ Boolean
- #exists?(query_options = {}) ⇒ Boolean (also: #exist?)
- #fields(*args) ⇒ Object
- #find(*ids) ⇒ Object
- #find_each(opts = {}) ⇒ Object (also: #each)
- #find_one(opts = {}) ⇒ Object (also: #first)
- #ignore(*args) ⇒ Object
- #last(opts = {}) ⇒ Object
- #limit(count = nil) ⇒ Object
- #only(*args) ⇒ Object
- #paginate(opts = {}) ⇒ Object
- #per_page(limit = nil) ⇒ Object
- #remove(opts = {}, driver_opts = {}) ⇒ Object
- #reverse ⇒ Object
- #skip(count = nil) ⇒ Object (also: #offset)
- #sort(*args) ⇒ Object (also: #order)
- #where(hash = {}) ⇒ Object (also: #filter)
Instance Method Details
#all(opts = {}) ⇒ Object Also known as: to_a
97 98 99 100 101 |
# File 'lib/plucky/query.rb', line 97 def all(opts={}) [].tap do |docs| find_each(opts) {|doc| docs << doc } end end |
#count(opts = {}) ⇒ Object Also known as: size
112 113 114 115 116 |
# File 'lib/plucky/query.rb', line 112 def count(opts={}) query = clone.amend(opts) cursor = query.cursor cursor.count end |
#distinct(key, opts = {}) ⇒ Object
118 119 120 121 |
# File 'lib/plucky/query.rb', line 118 def distinct(key, opts = {}) query = clone.amend(opts) query.collection.distinct(key, query.criteria_hash) end |
#empty? ⇒ Boolean
162 163 164 |
# File 'lib/plucky/query.rb', line 162 def empty? count == 0 end |
#exists?(query_options = {}) ⇒ Boolean Also known as: exist?
166 167 168 |
# File 'lib/plucky/query.rb', line 166 def exists?(={}) !only(:_id).find_one().nil? end |
#fields(*args) ⇒ Object
123 124 125 |
# File 'lib/plucky/query.rb', line 123 def fields(*args) clone.tap { |query| query.[:fields] = *args } end |
#find(*ids) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/plucky/query.rb', line 85 def find(*ids) return nil if ids.empty? single_id_find = ids.size == 1 && !ids[0].is_a?(Array) if single_id_find first(:_id => ids[0]) else all(:_id => ids.flatten) end end |
#find_each(opts = {}) ⇒ Object Also known as: each
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/plucky/query.rb', line 68 def find_each(opts={}) query = clone.amend(opts) cursor = query.cursor if block_given? cursor.each { |doc| yield doc } cursor.rewind! end cursor end |
#find_one(opts = {}) ⇒ Object Also known as: first
80 81 82 83 |
# File 'lib/plucky/query.rb', line 80 def find_one(opts={}) query = clone.amend(opts) query.collection.find_one(query.criteria_hash, query.) end |
#ignore(*args) ⇒ Object
127 128 129 |
# File 'lib/plucky/query.rb', line 127 def ignore(*args) set_field_inclusion(args, 0) end |
#last(opts = {}) ⇒ Object
103 104 105 |
# File 'lib/plucky/query.rb', line 103 def last(opts={}) clone.amend(opts).reverse.find_one end |
#limit(count = nil) ⇒ Object
135 136 137 |
# File 'lib/plucky/query.rb', line 135 def limit(count=nil) clone.tap { |query| query.[:limit] = count } end |
#only(*args) ⇒ Object
131 132 133 |
# File 'lib/plucky/query.rb', line 131 def only(*args) set_field_inclusion(args, 1) end |
#paginate(opts = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/plucky/query.rb', line 53 def paginate(opts={}) page = opts.delete(:page) limit = opts.delete(:per_page) || per_page query = clone.amend(opts) paginator = Pagination::Paginator.new(query.count, page, limit) docs = query.amend({ :limit => paginator.limit, :skip => paginator.skip, }).all docs.extend(Pagination::Decorator) docs.paginator(paginator) docs end |
#per_page(limit = nil) ⇒ Object
47 48 49 50 51 |
# File 'lib/plucky/query.rb', line 47 def per_page(limit=nil) return @per_page || 25 if limit.nil? @per_page = limit self end |
#remove(opts = {}, driver_opts = {}) ⇒ Object
107 108 109 110 |
# File 'lib/plucky/query.rb', line 107 def remove(opts={}, driver_opts={}) query = clone.amend(opts) query.collection.remove(query.criteria_hash, driver_opts) end |
#reverse ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/plucky/query.rb', line 139 def reverse clone.tap do |query| sort = query[:sort] if sort.nil? query.[:sort] = [[:_id, -1]] else query.[:sort] = sort.map { |s| [s[0], -s[1]] } end end end |
#skip(count = nil) ⇒ Object Also known as: offset
150 151 152 |
# File 'lib/plucky/query.rb', line 150 def skip(count=nil) clone.tap { |query| query.[:skip] = count } end |
#sort(*args) ⇒ Object Also known as: order
154 155 156 |
# File 'lib/plucky/query.rb', line 154 def sort(*args) clone.tap { |query| query.[:sort] = *args } end |
#where(hash = {}) ⇒ Object Also known as: filter
158 159 160 |
# File 'lib/plucky/query.rb', line 158 def where(hash={}) clone.tap { |query| query.criteria.merge!(CriteriaHash.new(hash)) } end |