Class: Plucky::Query
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/plucky/query.rb
Constant Summary collapse
- OptionKeys =
[ :select, :offset, :order, # MM :fields, :skip, :limit, :sort, :hint, :snapshot, # Ruby Driver :batch_size, :timeout, :transformer # Ruby Driver ]
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #all(opts = {}) ⇒ Object
- #count(opts = {}) ⇒ Object
- #distinct(key, opts = {}) ⇒ Object
- #empty? ⇒ Boolean
- #exists?(options = {}) ⇒ Boolean (also: #exist?)
- #explain ⇒ Object
- #fields(*args) ⇒ Object
- #find(*ids) ⇒ Object
- #find_each(opts = {}) ⇒ Object
- #find_one(opts = {}) ⇒ Object
- #first(opts = {}) ⇒ Object
- #ignore(*args) ⇒ Object
-
#initialize(collection, opts = {}) ⇒ Query
constructor
A new instance of Query.
- #initialize_copy(source) ⇒ Object
- #inspect ⇒ Object
- #last(opts = {}) ⇒ Object
- #limit(count = nil) ⇒ Object
- #merge(other) ⇒ Object
- #object_ids(*keys) ⇒ Object
- #only(*args) ⇒ Object
- #paginate(opts = {}) ⇒ Object
- #per_page(limit = nil) ⇒ Object
- #remove(opts = {}) ⇒ Object
- #reverse ⇒ Object
- #size ⇒ Object
- #skip(count = nil) ⇒ Object (also: #offset)
- #sort(*args) ⇒ Object (also: #order)
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #update(opts = {}) ⇒ Object
- #where(hash = {}) ⇒ Object
Constructor Details
#initialize(collection, opts = {}) ⇒ Query
Returns a new instance of Query.
19 20 21 22 |
# File 'lib/plucky/query.rb', line 19 def initialize(collection, opts={}) @collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new opts.each { |key, value| self[key] = value } end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
14 15 16 |
# File 'lib/plucky/query.rb', line 14 def collection @collection end |
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
14 15 16 |
# File 'lib/plucky/query.rb', line 14 def criteria @criteria end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/plucky/query.rb', line 14 def @options end |
Instance Method Details
#[](key) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/plucky/query.rb', line 163 def [](key) key = key.to_sym if key.respond_to?(:to_sym) if OptionKeys.include?(key) @options[key] else @criteria[key] end end |
#[]=(key, value) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/plucky/query.rb', line 172 def []=(key, value) key = key.to_sym if key.respond_to?(:to_sym) if OptionKeys.include?(key) @options[key] = value else @criteria[key] = value end end |
#all(opts = {}) ⇒ Object
75 76 77 |
# File 'lib/plucky/query.rb', line 75 def all(opts={}) find_each(opts).to_a end |
#count(opts = {}) ⇒ Object
92 93 94 |
# File 'lib/plucky/query.rb', line 92 def count(opts={}) find_each(opts).count end |
#distinct(key, opts = {}) ⇒ Object
100 101 102 103 |
# File 'lib/plucky/query.rb', line 100 def distinct(key, opts = {}) query = clone.update(opts) query.collection.distinct(key, query.criteria.to_hash) end |
#empty? ⇒ Boolean
150 151 152 |
# File 'lib/plucky/query.rb', line 150 def empty? count.zero? end |
#exists?(options = {}) ⇒ Boolean Also known as: exist?
154 155 156 |
# File 'lib/plucky/query.rb', line 154 def exists?(={}) !count().zero? end |
#explain ⇒ Object
191 192 193 |
# File 'lib/plucky/query.rb', line 191 def explain collection.find(criteria.to_hash, .to_hash).explain end |
#fields(*args) ⇒ Object
110 111 112 |
# File 'lib/plucky/query.rb', line 110 def fields(*args) clone.tap { |query| query.[:fields] = *args } end |
#find(*ids) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/plucky/query.rb', line 66 def find(*ids) return nil if ids.empty? if ids.size == 1 && !ids[0].is_a?(Array) first(:_id => ids[0]) else all(:_id => ids.flatten) end end |
#find_each(opts = {}) ⇒ Object
56 57 58 59 |
# File 'lib/plucky/query.rb', line 56 def find_each(opts={}) query = clone.update(opts) query.collection.find(query.criteria.to_hash, query..to_hash) end |
#find_one(opts = {}) ⇒ Object
61 62 63 64 |
# File 'lib/plucky/query.rb', line 61 def find_one(opts={}) query = clone.update(opts) query.collection.find_one(query.criteria.to_hash, query..to_hash) end |
#first(opts = {}) ⇒ Object
79 80 81 |
# File 'lib/plucky/query.rb', line 79 def first(opts={}) find_one(opts) end |
#ignore(*args) ⇒ Object
114 115 116 |
# File 'lib/plucky/query.rb', line 114 def ignore(*args) set_fields(args, 0) end |
#initialize_copy(source) ⇒ Object
24 25 26 27 28 |
# File 'lib/plucky/query.rb', line 24 def initialize_copy(source) super @criteria = @criteria.dup @options = @options.dup end |
#inspect ⇒ Object
195 196 197 198 199 200 |
# File 'lib/plucky/query.rb', line 195 def inspect as_nice_string = to_hash.collect do |key, value| " #{key}: #{value.inspect}" end.sort.join(",") "#<#{self.class}#{as_nice_string}>" end |
#last(opts = {}) ⇒ Object
83 84 85 |
# File 'lib/plucky/query.rb', line 83 def last(opts={}) clone.update(opts).reverse.find_one end |
#limit(count = nil) ⇒ Object
122 123 124 |
# File 'lib/plucky/query.rb', line 122 def limit(count=nil) clone.tap { |query| query.[:limit] = count } end |
#merge(other) ⇒ Object
181 182 183 184 185 |
# File 'lib/plucky/query.rb', line 181 def merge(other) merged_criteria = criteria.merge(other.criteria).to_hash = .merge(other.).to_hash clone.update(merged_criteria).update() end |
#object_ids(*keys) ⇒ Object
30 31 32 33 34 |
# File 'lib/plucky/query.rb', line 30 def object_ids(*keys) return criteria.object_ids if keys.empty? criteria.object_ids = *keys self end |
#only(*args) ⇒ Object
118 119 120 |
# File 'lib/plucky/query.rb', line 118 def only(*args) set_fields(args, 1) end |
#paginate(opts = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/plucky/query.rb', line 42 def paginate(opts={}) page = opts.delete(:page) limit = opts.delete(:per_page) || per_page query = clone.update(opts) total = query.count paginator = Pagination::Paginator.new(total, page, limit) query[:limit] = paginator.limit query[:skip] = paginator.skip query.all.tap do |docs| docs.extend(Pagination::Decorator) docs.paginator(paginator) end end |
#per_page(limit = nil) ⇒ Object
36 37 38 39 40 |
# File 'lib/plucky/query.rb', line 36 def per_page(limit=nil) return @per_page || 25 if limit.nil? @per_page = limit self end |
#remove(opts = {}) ⇒ Object
87 88 89 90 |
# File 'lib/plucky/query.rb', line 87 def remove(opts={}) query = clone.update(opts) query.collection.remove(query.criteria.to_hash) end |
#reverse ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/plucky/query.rb', line 126 def reverse clone.tap do |query| query[:sort] = query[:sort].map do |s| [s[0], -s[1]] end unless query.[:sort].nil? end end |
#size ⇒ Object
96 97 98 |
# File 'lib/plucky/query.rb', line 96 def size count end |
#skip(count = nil) ⇒ Object Also known as: offset
134 135 136 |
# File 'lib/plucky/query.rb', line 134 def skip(count=nil) clone.tap { |query| query.[:skip] = count } end |
#sort(*args) ⇒ Object Also known as: order
139 140 141 |
# File 'lib/plucky/query.rb', line 139 def sort(*args) clone.tap { |query| query.[:sort] = *args } end |
#to_a ⇒ Object
159 160 161 |
# File 'lib/plucky/query.rb', line 159 def to_a all end |
#to_hash ⇒ Object
187 188 189 |
# File 'lib/plucky/query.rb', line 187 def to_hash criteria.to_hash.merge(.to_hash) end |
#update(opts = {}) ⇒ Object
105 106 107 108 |
# File 'lib/plucky/query.rb', line 105 def update(opts={}) opts.each { |key, value| self[key] = value } self end |
#where(hash = {}) ⇒ Object
144 145 146 147 148 |
# File 'lib/plucky/query.rb', line 144 def where(hash={}) clone.tap do |query| query.criteria.merge!(CriteriaHash.new(hash)) end end |