Module: MongoMapper::Plugins::Querying::ClassMethods

Defined in:
lib/mongo_mapper/plugins/querying.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



46
47
48
# File 'lib/mongo_mapper/plugins/querying.rb', line 46

def all(options={})
  find_many(options)
end

#count(options = {}) ⇒ Object



50
51
52
# File 'lib/mongo_mapper/plugins/querying.rb', line 50

def count(options={})
  query(options).count
end

#create(*docs) ⇒ Object



66
67
68
# File 'lib/mongo_mapper/plugins/querying.rb', line 66

def create(*docs)
  initialize_each(*docs) { |doc| doc.save }
end

#create!(*docs) ⇒ Object



70
71
72
# File 'lib/mongo_mapper/plugins/querying.rb', line 70

def create!(*docs)
  initialize_each(*docs) { |doc| doc.save! }
end

#criteria_hash(criteria = {}) ⇒ Object



125
126
127
# File 'lib/mongo_mapper/plugins/querying.rb', line 125

def criteria_hash(criteria={})
  Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys)
end

#delete(*ids) ⇒ Object



83
84
85
# File 'lib/mongo_mapper/plugins/querying.rb', line 83

def delete(*ids)
  query(:_id => ids.flatten).query.remove
end

#delete_all(options = {}) ⇒ Object



87
88
89
# File 'lib/mongo_mapper/plugins/querying.rb', line 87

def delete_all(options={})
  query(options).query.remove
end

#destroy(*ids) ⇒ Object



91
92
93
# File 'lib/mongo_mapper/plugins/querying.rb', line 91

def destroy(*ids)
  find_some!(ids.flatten).each(&:destroy)
end

#destroy_all(options = {}) ⇒ Object



95
96
97
# File 'lib/mongo_mapper/plugins/querying.rb', line 95

def destroy_all(options={})
  find_each(options) { |document| document.destroy }
end

#exists?(options = {}) ⇒ Boolean

Returns:



54
55
56
# File 'lib/mongo_mapper/plugins/querying.rb', line 54

def exists?(options={})
  !count(options).zero?
end

#fields(*args) ⇒ Object



103
104
105
# File 'lib/mongo_mapper/plugins/querying.rb', line 103

def fields(*args)
  query.fields(*args)
end

#find(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/mongo_mapper/plugins/querying.rb', line 5

def find(*args)
  options = args.extract_options!
  return nil if args.size == 0

  if args.first.is_a?(Array) || args.size > 1
    find_some(args, options)
  else
    find_one(options.merge(:_id => args[0]))
  end
end

#find!(*args) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mongo_mapper/plugins/querying.rb', line 16

def find!(*args)
  options = args.extract_options!
  raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0

  if args.first.is_a?(Array) || args.size > 1
    find_some!(args, options)
  else
    find_one(options.merge(:_id => args[0])) ||
      raise(DocumentNotFound, "Document match #{options.inspect} does not exist
                                in #{collection.name} collection")
  end
end

#find_by_id(id) ⇒ Object



33
34
35
# File 'lib/mongo_mapper/plugins/querying.rb', line 33

def find_by_id(id)
  find_one(:_id => id)
end

#find_each(options = {}) ⇒ Object



29
30
31
# File 'lib/mongo_mapper/plugins/querying.rb', line 29

def find_each(options={})
  query(options).query.find().each { |doc| yield load(doc) }
end

#first(options = {}) ⇒ Object



37
38
39
# File 'lib/mongo_mapper/plugins/querying.rb', line 37

def first(options={})
  find_one(options)
end

#first_or_create(args) ⇒ Object



58
59
60
# File 'lib/mongo_mapper/plugins/querying.rb', line 58

def first_or_create(args)
  first(args) || create(args.reject { |key, value| !key?(key) })
end

#first_or_new(args) ⇒ Object



62
63
64
# File 'lib/mongo_mapper/plugins/querying.rb', line 62

def first_or_new(args)
  first(args) || new(args.reject { |key, value| !key?(key) })
end

#last(options = {}) ⇒ Object

All bets are off an actual order if you provide none.



42
43
44
# File 'lib/mongo_mapper/plugins/querying.rb', line 42

def last(options={})
  find_one(query(options).reverse.to_hash)
end

#limit(*args) ⇒ Object



107
108
109
# File 'lib/mongo_mapper/plugins/querying.rb', line 107

def limit(*args)
  query.limit(*args)
end

#options_hash(options = {}) ⇒ Object



130
131
132
# File 'lib/mongo_mapper/plugins/querying.rb', line 130

def options_hash(options={})
  Plucky::OptionsHash.new(options)
end

#query(options = {}) ⇒ Object



120
121
122
# File 'lib/mongo_mapper/plugins/querying.rb', line 120

def query(options={})
  Query.new(self).update(options)
end

#skip(*args) ⇒ Object



111
112
113
# File 'lib/mongo_mapper/plugins/querying.rb', line 111

def skip(*args)
  query.skip(*args)
end

#sort(*args) ⇒ Object



115
116
117
# File 'lib/mongo_mapper/plugins/querying.rb', line 115

def sort(*args)
  query.sort(*args)
end

#update(*args) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/mongo_mapper/plugins/querying.rb', line 74

def update(*args)
  if args.length == 1
    update_multiple(args[0])
  else
    id, attributes = args
    update_single(id, attributes)
  end
end

#where(options = {}) ⇒ Object



99
100
101
# File 'lib/mongo_mapper/plugins/querying.rb', line 99

def where(options={})
  query.where(options)
end