lib/plucky/query.rb in plucky-0.7.0 vs lib/plucky/query.rb in plucky-0.8.0
- old
+ new
@@ -7,15 +7,16 @@
include Enumerable
extend Forwardable
# Private
OptionKeys = Set[
- :select, :offset, :order, # MM
- :fields, :skip, :limit, :sort, :hint, :snapshot, # Ruby Driver
+ :select, :offset, :order, :transformer, # MM
+ :projection, :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
+ :show_disk_loc, :comment, :read, # Ruby Driver
:tag_sets, :acceptable_latency, # Ruby Driver
+ :max_time_ms, :no_cursor_timeout, :collation, :modifiers
]
attr_reader :criteria, :options, :collection
def_delegator :@criteria, :simple?
@@ -66,25 +67,23 @@
def find_each(opts={})
query = clone.amend(opts)
if block_given?
- result = nil
- query.cursor do |cursor|
- result = cursor
- cursor.each { |doc| yield doc }
- cursor.rewind!
+ enumerator = query.enumerator
+ enumerator.each do |doc|
+ yield doc
end
- result
+ enumerator
else
- query.cursor
+ query.enumerator
end
end
def find_one(opts={})
- query = clone.amend(opts)
- query.collection.find_one(query.criteria_hash, query.options_hash)
+ query = clone.amend(opts.merge(limit: -1))
+ query.enumerator.first
end
def find(*ids)
return nil if ids.empty?
@@ -107,26 +106,26 @@
clone.amend(opts).reverse.find_one
end
def remove(opts={}, driver_opts={})
query = clone.amend(opts)
- query.collection.remove(query.criteria_hash, driver_opts)
+ query.collection.find(query.criteria_hash, driver_opts).delete_many
end
def count(opts={})
query = clone.amend(opts)
- cursor = query.cursor
+ cursor = query.view
cursor.count
end
def distinct(key, opts = {})
query = clone.amend(opts)
query.collection.distinct(key, query.criteria_hash)
end
- def fields(*args)
- clone.tap { |query| query.options[:fields] = *args }
+ def projection(*args)
+ clone.tap { |query| query.options[:projection] = *args }
end
def ignore(*args)
set_field_inclusion(args, 0)
end
@@ -176,18 +175,33 @@
alias_method :offset, :skip
alias_method :order, :sort
alias_method :exist?, :exists?
alias_method :filter, :where
alias_method :to_a, :all
+ alias_method :fields, :projection
end
include DSL
def update(document, driver_opts={})
query = clone
- query.collection.update(query.criteria_hash, document, driver_opts)
+ 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
+ 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
+
def amend(opts={})
opts.each { |key, value| self[key] = value }
self
end
@@ -230,14 +244,33 @@
def options_hash
@options.to_hash
end
- def cursor(&block)
- @collection.find(criteria_hash, options_hash, &block)
+ 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
+ def enumerator
+ if transformer = options_hash[:transformer]
+ Transformer.new(view, transformer).to_enum
+ else
+ view.to_enum
+ end
+ end
+
private
# Private
def hash_for_key(key)
options_key?(key) ? @options : @criteria
@@ -259,9 +292,9 @@
# Private
def set_field_inclusion(fields, value)
fields_option = {}
fields.each { |field| fields_option[symbolized_key(field)] = value }
- clone.tap { |query| query.options[:fields] = fields_option }
+ clone.tap { |query| query.options[:projection] = fields_option }
end
end
end