Module: Minidoc::Finders::ClassMethods
- Defined in:
- lib/minidoc/finders.rb
Instance Method Summary collapse
- #all ⇒ Object
- #count(selector = {}) ⇒ Object
- #exists?(selector = {}) ⇒ Boolean
- #find(id_or_selector, options = {}) ⇒ Object
- #find_one(selector = {}, options = {}) ⇒ Object
- #find_one!(selector = {}, options = {}) ⇒ Object
- #find_one_or_initialize(attributes = {}, options = {}) ⇒ Object
- #first ⇒ Object
Instance Method Details
#all ⇒ Object
10 11 12 |
# File 'lib/minidoc/finders.rb', line 10 def all find({}) end |
#count(selector = {}) ⇒ Object
18 19 20 |
# File 'lib/minidoc/finders.rb', line 18 def count(selector = {}) collection.count_documents(selector) end |
#exists?(selector = {}) ⇒ Boolean
22 23 24 |
# File 'lib/minidoc/finders.rb', line 22 def exists?(selector = {}) find_one(selector).present? end |
#find(id_or_selector, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/minidoc/finders.rb', line 26 def find(id_or_selector, = {}) if id_or_selector.is_a?(Hash) ResultSet.new(collection.find(id_or_selector, ), wrapper) else raise ArgumentError unless .empty? id = BSON::ObjectId(id_or_selector.to_s) wrapper.call(collection.find(_id: id).first) end end |
#find_one(selector = {}, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/minidoc/finders.rb', line 36 def find_one(selector = {}, = {}) # running collection.find({}).first will leave the cursor open unless we iterate across all of the documents # so in order to do not let a cusror open we want to kill the cursor after having grabbed the first document view = collection.find(selector, ) wrapped_doc = wrapper.call(view.first) view.close_query wrapped_doc end |
#find_one!(selector = {}, options = {}) ⇒ Object
45 46 47 |
# File 'lib/minidoc/finders.rb', line 45 def find_one!(selector = {}, = {}) find_one(selector, ) or raise DocumentNotFoundError end |
#find_one_or_initialize(attributes = {}, options = {}) ⇒ Object
49 50 51 52 |
# File 'lib/minidoc/finders.rb', line 49 def find_one_or_initialize(attributes = {}, = {}) raise ArgumentError unless attributes.is_a?(Hash) find_one(attributes, ) || new(attributes) end |
#first ⇒ Object
14 15 16 |
# File 'lib/minidoc/finders.rb', line 14 def first find_one({}) end |