Module: Minidoc::Finders::ClassMethods
- Defined in:
- lib/minidoc/finders.rb
Instance Method Summary collapse
- #count(selector = {}) ⇒ Object
- #exists?(selector = {}) ⇒ Boolean
- #find(id_or_selector, options = {}) ⇒ Object
- #find_one(selector, options = {}) ⇒ Object
- #first ⇒ Object
- #from_db(attrs) ⇒ Object
- #wrap(doc) ⇒ Object
Instance Method Details
#count(selector = {}) ⇒ Object
11 12 13 |
# File 'lib/minidoc/finders.rb', line 11 def count(selector = {}) collection.count(query: selector) end |
#exists?(selector = {}) ⇒ Boolean
15 16 17 |
# File 'lib/minidoc/finders.rb', line 15 def exists?(selector = {}) count(selector) > 0 end |
#find(id_or_selector, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/minidoc/finders.rb', line 19 def find(id_or_selector, = {}) if id_or_selector.is_a?(Hash) .merge!(transformer: method(:wrap)) collection.find(id_or_selector, ) else raise ArgumentError unless .empty? id = BSON::ObjectId(id_or_selector.to_s) wrap(collection.find_one(_id: id)) end end |
#find_one(selector, options = {}) ⇒ Object
30 31 32 |
# File 'lib/minidoc/finders.rb', line 30 def find_one(selector, = {}) wrap(collection.find_one(selector, )) end |
#first ⇒ Object
7 8 9 |
# File 'lib/minidoc/finders.rb', line 7 def first find_one({}) end |
#from_db(attrs) ⇒ Object
34 35 36 37 38 |
# File 'lib/minidoc/finders.rb', line 34 def from_db(attrs) doc = new(attrs) doc.instance_variable_set("@new_record", false) doc end |
#wrap(doc) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/minidoc/finders.rb', line 40 def wrap(doc) return nil unless doc if doc.is_a?(Array) || doc.is_a?(Mongo::Cursor) doc.map { |d| from_db(d) } else from_db(doc) end end |