Module: Minidoc::Finders::ClassMethods

Defined in:
lib/minidoc/finders.rb

Instance Method Summary collapse

Instance Method Details

#allObject



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

Returns:

  • (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, options = {})
  if id_or_selector.is_a?(Hash)
    ResultSet.new(collection.find(id_or_selector, options), wrapper)
  else
    raise ArgumentError unless options.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 = {}, options = {})
  # 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, options)
  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 = {}, options = {})
  find_one(selector, options) or raise DocumentNotFoundError
end

#find_one_or_initialize(attributes = {}, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
# File 'lib/minidoc/finders.rb', line 49

def find_one_or_initialize(attributes = {}, options = {})
  raise ArgumentError unless attributes.is_a?(Hash)
  find_one(attributes, options) || new(attributes)
end

#firstObject



14
15
16
# File 'lib/minidoc/finders.rb', line 14

def first
  find_one({})
end