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

Extended by:
Forwardable
Includes:
PluckyMethods
Defined in:
lib/mongo_mapper/plugins/querying.rb

Instance Method Summary collapse

Instance Method Details

#create(*docs) ⇒ Object



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

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

#create!(*docs) ⇒ Object



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

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

#criteria_hash(criteria = {}) ⇒ Object



76
77
78
# File 'lib/mongo_mapper/plugins/querying.rb', line 76

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

#delete(*ids) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

def destroy(*ids)
  find_some!(ids.flatten).each { |doc| doc.destroy }
end

#destroy_all(options = {}) ⇒ Object



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

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

#find_by_id(id) ⇒ Object



20
21
22
# File 'lib/mongo_mapper/plugins/querying.rb', line 20

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

#find_each(opts = {}) ⇒ Object



16
17
18
# File 'lib/mongo_mapper/plugins/querying.rb', line 16

def find_each(opts={})
  super(opts).each { |doc| yield(doc) }
end

#first_or_create(args) ⇒ Object



24
25
26
# File 'lib/mongo_mapper/plugins/querying.rb', line 24

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

#first_or_new(args) ⇒ Object



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

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

#query(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/mongo_mapper/plugins/querying.rb', line 66

def query(options={})
  query = Plucky::Query.new(collection, :transformer => transformer)
  query.extend(Decorator)
  query.object_ids(object_id_keys)
  query.amend(options)
  query.model(self)
  query
end

#update(*args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/mongo_mapper/plugins/querying.rb', line 40

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