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

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

Instance Method Summary collapse

Instance Method Details

#create(*docs) ⇒ Object



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

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

#create!(*docs) ⇒ Object



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

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

#criteria_hash(criteria = {}) ⇒ Object



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

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

#delete(*ids) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

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

#destroy_all(options = {}) ⇒ Object



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

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

#find_by_id(id) ⇒ Object



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

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

#find_each(opts = {}) ⇒ Object



11
12
13
# File 'lib/mongo_mapper/plugins/querying.rb', line 11

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

#first_or_create(args) ⇒ Object



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

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

#first_or_new(args) ⇒ Object



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

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

#query(options = {}) ⇒ Object



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

def query(options={})
  Plucky::Query.new(collection).tap do |query|
    query.extend(Decorator)
    query.object_ids(object_id_keys)
    query.update(options)
    query.model(self)
  end
end

#update(*args) ⇒ Object



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

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