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



194
195
196
197
198
# File 'lib/plucky/query.rb', line 194

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

#[]=(key, value) ⇒ Object



200
201
202
203
204
# File 'lib/plucky/query.rb', line 200

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

#amend(opts = {}) ⇒ Object



189
190
191
192
# File 'lib/plucky/query.rb', line 189

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

#criteria_hashObject



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

def criteria_hash
  @criteria.to_hash
end

#cursor(&block) ⇒ Object



235
236
237
# File 'lib/plucky/query.rb', line 235

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

#explainObject



216
217
218
# File 'lib/plucky/query.rb', line 216

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



220
221
222
223
224
225
# File 'lib/plucky/query.rb', line 220

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



206
207
208
209
210
# File 'lib/plucky/query.rb', line 206

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



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

def options_hash
  @options.to_hash
end

#to_hashObject



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

def to_hash
  criteria_hash.merge(options_hash)
end

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



184
185
186
187
# File 'lib/plucky/query.rb', line 184

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