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, :transformer,                        # MM
  :projection, :fields, :skip, :limit, :sort, :hint, :snapshot,  # Ruby Driver
  :batch_size, :timeout, :max_scan, :return_key,    # Ruby Driver
  :show_disk_loc, :comment, :read,    # Ruby Driver
  :tag_sets, :acceptable_latency,                   # Ruby Driver
  :max_time_ms, :no_cursor_timeout, :collation, :modifiers
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

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

Constructor Details

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

Public



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

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.



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

def collection
  @collection
end

#criteriaObject (readonly)

Returns the value of attribute criteria.



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

def criteria
  @criteria
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#amend(opts = {}) ⇒ Object



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

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

#criteria_hashObject



241
242
243
# File 'lib/plucky/query.rb', line 241

def criteria_hash
  @criteria.to_hash
end

#enumeratorObject



264
265
266
267
268
269
270
# File 'lib/plucky/query.rb', line 264

def enumerator
  if transformer = options_hash[:transformer]
    Transformer.new(view, transformer).to_enum
  else
    view.to_enum
  end
end

#explainObject



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

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

#initialize_copy(original) ⇒ Object



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

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

#insert(document_or_array, driver_opts = {}) ⇒ Object



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

def insert(document_or_array, driver_opts={})
  query = clone

  if document_or_array.is_a?(Array)
    query.collection.insert_many(document_or_array, driver_opts)
  else
    query.collection.insert_one(document_or_array, driver_opts)
  end
end

#inspectObject



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

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



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

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



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

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

#options_hashObject



245
246
247
# File 'lib/plucky/query.rb', line 245

def options_hash
  @options.to_hash
end

#to_hashObject



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

def to_hash
  criteria_hash.merge(options_hash)
end

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



184
185
186
187
188
189
190
191
# File 'lib/plucky/query.rb', line 184

def update(document, driver_opts={})
  query = clone
  if driver_opts[:multi]
    query.collection.find(query.criteria_hash).update_many(document, driver_opts)
  else
    query.collection.find(query.criteria_hash).update_one(document, driver_opts)
  end
end

#viewObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/plucky/query.rb', line 249

def view
  driver_opts = options_hash.dup
  driver_opts.delete :transformer
  case driver_opts[:read]
  when Hash
    driver_opts[:read] = Mongo::ServerSelector.get(driver_opts[:read])
  when Symbol
    driver_opts[:read] = Mongo::ServerSelector.get(mode: driver_opts[:read])
  else
    raise "Unexpected read options: #{driver_opts[:read]} - expected hash or symbol"
  end if driver_opts.has_key?(:read)

  @collection.find(criteria_hash, driver_opts)
end