Class: Plucky::Query

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Enumerable, DSL
Defined in:
lib/plucky/query.rb

Defined Under Namespace

Modules: DSL

Constant Summary collapse

OptionKeys =

Private

Set[
  :select, :offset, :order,                         # MM
  :fields, :skip, :limit, :sort, :hint, :snapshot,  # Ruby Driver
  :batch_size, :timeout, :max_scan, :return_key,    # Ruby Driver
  :transformer, :show_disk_loc, :comment, :read,    # Ruby Driver
  :tag_sets, :acceptable_latency,                   # Ruby Driver
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#all, #count, #distinct, #empty?, #exists?, #fields, #find, #find_each, #find_one, #ignore, #last, #limit, #only, #paginate, #per_page, #remove, #reverse, #skip, #sort, #where

Constructor Details

#initialize(collection, query_options = {}) ⇒ Query

Public



26
27
28
29
# File 'lib/plucky/query.rb', line 26

def initialize(collection, query_options = {})
  @collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
  query_options.each { |key, value| self[key] = value }
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



19
20
21
# File 'lib/plucky/query.rb', line 19

def collection
  @collection
end

#criteriaObject (readonly)

Returns the value of attribute criteria.



19
20
21
# File 'lib/plucky/query.rb', line 19

def criteria
  @criteria
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/plucky/query.rb', line 19

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



191
192
193
194
195
# File 'lib/plucky/query.rb', line 191

def [](key)
  key = symbolized_key(key)
  source = hash_for_key(key)
  source[key]
end

#[]=(key, value) ⇒ Object



197
198
199
200
201
# File 'lib/plucky/query.rb', line 197

def []=(key, value)
  key = symbolized_key(key)
  source = hash_for_key(key)
  source[key] = value
end

#amend(opts = {}) ⇒ Object



186
187
188
189
# File 'lib/plucky/query.rb', line 186

def amend(opts={})
  opts.each { |key, value| self[key] = value }
  self
end

#criteria_hashObject



224
225
226
# File 'lib/plucky/query.rb', line 224

def criteria_hash
  @criteria.to_hash
end

#cursorObject



232
233
234
# File 'lib/plucky/query.rb', line 232

def cursor
  @collection.find(criteria_hash, options_hash)
end

#explainObject



213
214
215
# File 'lib/plucky/query.rb', line 213

def explain
  @collection.find(criteria_hash, options_hash).explain
end

#initialize_copy(original) ⇒ Object



31
32
33
34
35
# File 'lib/plucky/query.rb', line 31

def initialize_copy(original)
  super
  @criteria = @criteria.dup
  @options  = @options.dup
end

#inspectObject



217
218
219
220
221
222
# File 'lib/plucky/query.rb', line 217

def inspect
  as_nice_string = to_hash.collect do |key, value|
    " #{key}: #{value.inspect}"
  end.sort.join(",")
  "#<#{self.class}#{as_nice_string}>"
end

#merge(other) ⇒ Object



203
204
205
206
207
# File 'lib/plucky/query.rb', line 203

def merge(other)
  merged_criteria = @criteria.merge(other.criteria).to_hash
  merged_options  = @options.merge(other.options).to_hash
  clone.amend(merged_criteria).amend(merged_options)
end

#object_ids(*keys) ⇒ Object

Public



38
39
40
41
42
# File 'lib/plucky/query.rb', line 38

def object_ids(*keys)
  return @criteria.object_ids if keys.empty?
  @criteria.object_ids = *keys
  self
end

#options_hashObject



228
229
230
# File 'lib/plucky/query.rb', line 228

def options_hash
  @options.to_hash
end

#to_hashObject



209
210
211
# File 'lib/plucky/query.rb', line 209

def to_hash
  criteria_hash.merge(options_hash)
end

#update(document, driver_opts = {}) ⇒ Object



181
182
183
184
# File 'lib/plucky/query.rb', line 181

def update(document, driver_opts={})
  query = clone
  query.collection.update(query.criteria_hash, document, driver_opts)
end