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

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

Instance Method Summary collapse

Instance Method Details

#create(*docs) ⇒ Object



26
27
28
# File 'lib/mongo_mapper/plugins/querying.rb', line 26

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

#create!(*docs) ⇒ Object



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

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

#criteria_hash(criteria = {}) ⇒ Object



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

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

#delete(*ids) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

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

#destroy_all(options = {}) ⇒ Object



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

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

#find_by_id(id) ⇒ Object



14
15
16
# File 'lib/mongo_mapper/plugins/querying.rb', line 14

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

#first_or_create(args) ⇒ Object



18
19
20
# File 'lib/mongo_mapper/plugins/querying.rb', line 18

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

#first_or_new(args) ⇒ Object



22
23
24
# File 'lib/mongo_mapper/plugins/querying.rb', line 22

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

#query(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/mongo_mapper/plugins/querying.rb', line 60

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



34
35
36
37
38
39
40
41
# File 'lib/mongo_mapper/plugins/querying.rb', line 34

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