Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongomapper/document.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



60
61
62
# File 'lib/mongomapper/document.rb', line 60

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

#collection(name = nil) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/mongomapper/document.rb', line 133

def collection(name=nil)
  if name.nil?
    @collection ||= database.collection(self.to_s.demodulize.tableize)
  else
    @collection = database.collection(name)
  end
  @collection
end

#connection(mongo_connection = nil) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/mongomapper/document.rb', line 115

def connection(mongo_connection=nil)
  if mongo_connection.nil?
    @connection ||= MongoMapper.connection
  else
    @connection = mongo_connection
  end
  @connection
end

#count(conditions = {}) ⇒ Object

TODO: remove the rescuing when ruby driver works correctly



71
72
73
# File 'lib/mongomapper/document.rb', line 71

def count(conditions={})
  collection.count(FinderOptions.to_mongo_criteria(conditions))
end

#create(*docs) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/mongomapper/document.rb', line 75

def create(*docs)
  instances = []
  docs.flatten.each do |attrs|
    doc = new(attrs); doc.save
    instances << doc
  end
  instances.size == 1 ? instances[0] : instances
end

#database(name = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/mongomapper/document.rb', line 124

def database(name=nil)
  if name.nil?
    @database ||= MongoMapper.database
  else
    @database = connection.db(name)
  end
  @database
end

#delete(*ids) ⇒ Object



99
100
101
# File 'lib/mongomapper/document.rb', line 99

def delete(*ids)
  collection.remove(:_id => {'$in' => ids.flatten})
end

#delete_all(conditions = {}) ⇒ Object



103
104
105
# File 'lib/mongomapper/document.rb', line 103

def delete_all(conditions={})
  collection.remove(FinderOptions.to_mongo_criteria(conditions))
end

#destroy(*ids) ⇒ Object



107
108
109
# File 'lib/mongomapper/document.rb', line 107

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

#destroy_all(conditions = {}) ⇒ Object



111
112
113
# File 'lib/mongomapper/document.rb', line 111

def destroy_all(conditions={})
  find(:all, :conditions => conditions).each(&:destroy)
end

#find(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/mongomapper/document.rb', line 27

def find(*args)
  options = args.extract_options!

  case args.first
    when :first then find_first(options)
    when :last  then find_last(options)
    when :all   then find_every(options)
    else             find_from_ids(args)
  end
end

#find_by_id(id) ⇒ Object



64
65
66
67
68
# File 'lib/mongomapper/document.rb', line 64

def find_by_id(id)
  if doc = collection.find_first({:_id => id})
    new(doc)
  end
end

#first(options = {}) ⇒ Object



52
53
54
# File 'lib/mongomapper/document.rb', line 52

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

#last(options = {}) ⇒ Object



56
57
58
# File 'lib/mongomapper/document.rb', line 56

def last(options={})
  find_last(options)
end

#paginate(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongomapper/document.rb', line 38

def paginate(options)
  per_page      = options.delete(:per_page)
  page          = options.delete(:page)
  total_entries = count(options)
  
  collection = Pagination::PaginationProxy.new(total_entries, page, per_page)
  
  options[:limit] = collection.limit
  options[:skip] = collection.skip
  
  collection.subject = find_every(options)
  collection
end

#update(*args) ⇒ Object

For updating single document

Person.update(1, {:foo => 'bar'})

For updating multiple documents at once:

Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})


89
90
91
92
93
94
95
96
97
# File 'lib/mongomapper/document.rb', line 89

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