Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongo_mapper/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)



198
199
200
201
202
203
204
205
206
207
# File 'lib/mongo_mapper/document.rb', line 198

def method_missing(method, *args)
  finder = DynamicFinder.new(method)
  
  if finder.found?
    meta_def(finder.method) { |*args| dynamic_find(finder, args) }
    send(finder.method, *args)
  else
    super
  end
end

Instance Method Details

#all(options = {}) ⇒ Object



94
95
96
# File 'lib/mongo_mapper/document.rb', line 94

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

#collectionObject

Returns the mongo ruby driver collection object



186
187
188
# File 'lib/mongo_mapper/document.rb', line 186

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

#collection_nameObject

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



181
182
183
# File 'lib/mongo_mapper/document.rb', line 181

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

#connection(mongo_connection = nil) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/mongo_mapper/document.rb', line 156

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

#count(conditions = {}) ⇒ Object



105
106
107
# File 'lib/mongo_mapper/document.rb', line 105

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

#create(*docs) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/mongo_mapper/document.rb', line 113

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



165
166
167
168
169
170
171
172
# File 'lib/mongo_mapper/document.rb', line 165

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

#delete(*ids) ⇒ Object



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

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

#delete_all(conditions = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



148
149
150
# File 'lib/mongo_mapper/document.rb', line 148

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

#destroy_all(conditions = {}) ⇒ Object



152
153
154
# File 'lib/mongo_mapper/document.rb', line 152

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

#ensure_index(name_or_array, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/mongo_mapper/document.rb', line 37

def ensure_index(name_or_array, options={})
  keys_to_index = if name_or_array.is_a?(Array)
    name_or_array.map { |pair| [pair[0], pair[1]] }
  else
    name_or_array
  end
  
  MongoMapper.ensure_index(self, keys_to_index, options)
end

#exists?(conditions = {}) ⇒ Boolean

Returns:



109
110
111
# File 'lib/mongo_mapper/document.rb', line 109

def exists?(conditions={})
  !count(conditions).zero?
end

#find(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongo_mapper/document.rb', line 47

def find(*args)
  options = args.extract_options!
  case args.first
    when :first then first(options)
    when :last  then last(options)
    when :all   then find_every(options)
    when Array  then find_some(args, options)
    else
      case args.size
        when 0
          raise DocumentNotFound, "Couldn't find without an ID"
        when 1
          find_one(args[0], options)
        else
          find_some(args, options)
      end
  end
end

#find_by_id(id) ⇒ Object



98
99
100
101
102
103
# File 'lib/mongo_mapper/document.rb', line 98

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



79
80
81
82
# File 'lib/mongo_mapper/document.rb', line 79

def first(options={})
  options.merge!(:limit => 1)
  find_every(options)[0]
end

#key(*args) ⇒ Object



31
32
33
34
35
# File 'lib/mongo_mapper/document.rb', line 31

def key(*args)
  key = super
  create_indexes_for(key)
  key
end

#last(options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/mongo_mapper/document.rb', line 84

def last(options={})
  if options[:order].blank?
    raise ':order option must be provided when using last'
  end
  
  options.merge!(:limit => 1)
  options[:order] = invert_order_clause(options[:order])
  find_every(options)[0]
end

#paginate(options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mongo_mapper/document.rb', line 66

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



175
176
177
178
# File 'lib/mongo_mapper/document.rb', line 175

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

#timestamps!Object



190
191
192
193
194
195
# File 'lib/mongo_mapper/document.rb', line 190

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'}})


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

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