Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongomapper/document.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mongomapper/document.rb', line 161

def method_missing(method, *args)
  finder = DynamicFinder.new(self, method)
  
  if finder.valid?
    meta_def(finder.options[:method]) do |*args|
      find_with_args(args, finder.options)
    end
    
    send(finder.options[:method], *args)
  else
    super
  end
end

Instance Method Details

#all(options = {}) ⇒ Object



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

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

#collectionObject

Returns the mongo ruby driver collection object



149
150
151
# File 'lib/mongomapper/document.rb', line 149

def collection
  @collection ||= database.collection(collection_name)
end

#collection_nameObject

Returns the collection name, if not set, defaults to class name tableized



144
145
146
# File 'lib/mongomapper/document.rb', line 144

def collection_name
  @collection_name ||= self.to_s.demodulize.tableize
end

#connection(mongo_connection = nil) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/mongomapper/document.rb', line 119

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

#count(conditions = {}) ⇒ Object



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

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

#create(*docs) ⇒ Object



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

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

#database(name = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/mongomapper/document.rb', line 128

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

#delete(*ids) ⇒ Object



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

def delete(*ids)
  criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten)
  collection.remove(criteria)
end

#delete_all(conditions = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

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

#destroy_all(conditions = {}) ⇒ Object



115
116
117
# File 'lib/mongomapper/document.rb', line 115

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

#find(*args) ⇒ Object



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

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, options)
  end
end

#find_by_id(id) ⇒ Object



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

def find_by_id(id)
  criteria = FinderOptions.to_mongo_criteria(:_id => id)
  if doc = collection.find_one(criteria)
    new(doc)
  end
end

#first(options = {}) ⇒ Object



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

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

#last(options = {}) ⇒ Object



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

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

#paginate(options) ⇒ Object



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

def paginate(options)
  per_page      = options.delete(:per_page) ||  self.per_page
  page          = options.delete(:page)
  total_entries = count(options[:conditions] || {})
  collection    = Pagination::PaginationProxy.new(total_entries, page, per_page)

  options[:limit] = collection.limit
  options[:skip]  = collection.skip

  collection.subject = find_every(options)
  collection
end

#set_collection_name(name = nil) ⇒ Object

Changes the collection name from the default to whatever you want



138
139
140
141
# File 'lib/mongomapper/document.rb', line 138

def set_collection_name(name=nil)
  @collection = nil
  @collection_name = name
end

#timestamps!Object



153
154
155
156
157
158
# File 'lib/mongomapper/document.rb', line 153

def timestamps!
  key :created_at, Time
  key :updated_at, Time
  
  class_eval { before_save :update_timestamps }
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'}})


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

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