Module: Minidoc::Finders::ClassMethods

Defined in:
lib/minidoc/finders.rb

Instance Method Summary collapse

Instance Method Details

#allObject



9
10
11
# File 'lib/minidoc/finders.rb', line 9

def all
  find({})
end

#count(selector = {}) ⇒ Object



17
18
19
# File 'lib/minidoc/finders.rb', line 17

def count(selector = {})
  collection.count(query: selector)
end

#exists?(selector = {}) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/minidoc/finders.rb', line 21

def exists?(selector = {})
  find_one(selector).present?
end

#find(id_or_selector, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/minidoc/finders.rb', line 25

def find(id_or_selector, options = {})
  if id_or_selector.is_a?(Hash)
    options.merge!(transformer: method(:wrap))
    collection.find(id_or_selector, options)
  else
    raise ArgumentError unless options.empty?
    id = BSON::ObjectId(id_or_selector.to_s)
    wrap(collection.find_one(_id: id))
  end
end

#find_one(selector = {}, options = {}) ⇒ Object



36
37
38
# File 'lib/minidoc/finders.rb', line 36

def find_one(selector = {}, options = {})
  wrap(collection.find_one(selector, options))
end

#find_one!(selector = {}, options = {}) ⇒ Object



40
41
42
# File 'lib/minidoc/finders.rb', line 40

def find_one!(selector = {}, options = {})
  find_one(selector, options) or raise DocumentNotFoundError
end

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

Raises:

  • (ArgumentError)


44
45
46
47
# File 'lib/minidoc/finders.rb', line 44

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

#firstObject



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

def first
  find_one({})
end