Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



121
122
123
# File 'lib/mongo_mapper/document.rb', line 121

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

#collectionObject



254
255
256
# File 'lib/mongo_mapper/document.rb', line 254

def collection
  database.collection(collection_name)
end

#collection_nameObject



250
251
252
# File 'lib/mongo_mapper/document.rb', line 250

def collection_name
  @collection_name ||= self.to_s.tableize.gsub(/\//, '.')
end

#connection(mongo_connection = nil) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/mongo_mapper/document.rb', line 221

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

#count(options = {}) ⇒ Object



129
130
131
# File 'lib/mongo_mapper/document.rb', line 129

def count(options={})
  collection.find(to_criteria(options)).count
end

#create(*docs) ⇒ Object



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

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

#create!(*docs) ⇒ Object



141
142
143
# File 'lib/mongo_mapper/document.rb', line 141

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

#databaseObject



238
239
240
241
242
243
244
# File 'lib/mongo_mapper/document.rb', line 238

def database
  if database_name.nil?
    MongoMapper.database
  else
    connection.db(database_name)
  end
end

#database_nameObject



234
235
236
# File 'lib/mongo_mapper/document.rb', line 234

def database_name
  @database_name
end

#decrement(*args) ⇒ Object



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

def decrement(*args)
  criteria, keys = criteria_and_keys_from_args(args)
  values, to_decrement = keys.values, {}
  keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
  collection.update(criteria, {'$inc' => to_decrement}, :multi => true)
end

#delete(*ids) ⇒ Object



154
155
156
# File 'lib/mongo_mapper/document.rb', line 154

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

#delete_all(options = {}) ⇒ Object



158
159
160
# File 'lib/mongo_mapper/document.rb', line 158

def delete_all(options={})
  collection.remove(to_criteria(options))
end

#destroy(*ids) ⇒ Object



162
163
164
# File 'lib/mongo_mapper/document.rb', line 162

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

#destroy_all(options = {}) ⇒ Object



166
167
168
# File 'lib/mongo_mapper/document.rb', line 166

def destroy_all(options={})
  all(options).each(&:destroy)
end

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



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

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?(options = {}) ⇒ Boolean

Returns:



133
134
135
# File 'lib/mongo_mapper/document.rb', line 133

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

#find(*args) ⇒ Object



95
96
97
98
99
# File 'lib/mongo_mapper/document.rb', line 95

def find(*args)
  find!(*args)
rescue DocumentNotFound
  nil
end

#find!(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mongo_mapper/document.rb', line 76

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!(options.merge({:_id => args[0]}))
        else
          find_some(args, options)
      end
  end
end

#find_by_id(id) ⇒ Object



125
126
127
# File 'lib/mongo_mapper/document.rb', line 125

def find_by_id(id)
  find(id)
end

#first(options = {}) ⇒ Object



112
113
114
# File 'lib/mongo_mapper/document.rb', line 112

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

#increment(*args) ⇒ Object



170
171
172
# File 'lib/mongo_mapper/document.rb', line 170

def increment(*args)
  modifier_update('$inc', args)
end

#key(*args) ⇒ Object



60
61
62
63
64
# File 'lib/mongo_mapper/document.rb', line 60

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

#last(options = {}) ⇒ Object



116
117
118
119
# File 'lib/mongo_mapper/document.rb', line 116

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

#paginate(options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/mongo_mapper/document.rb', line 101

def paginate(options)
  per_page      = options.delete(:per_page) || self.per_page
  page          = options.delete(:page)
  total_entries = count(options)
  pagination    = Pagination::PaginationProxy.new(total_entries, page, per_page)

  options.merge!(:limit => pagination.limit, :skip => pagination.skip)
  pagination.subject = find_every(options)
  pagination
end

#pull(*args) ⇒ Object



199
200
201
# File 'lib/mongo_mapper/document.rb', line 199

def pull(*args)
  modifier_update('$pull', args)
end

#pull_all(*args) ⇒ Object



203
204
205
# File 'lib/mongo_mapper/document.rb', line 203

def pull_all(*args)
  modifier_update('$pullAll', args)
end

#push(*args) ⇒ Object



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

def push(*args)
  modifier_update('$push', args)
end

#push_all(*args) ⇒ Object



189
190
191
# File 'lib/mongo_mapper/document.rb', line 189

def push_all(*args)
  modifier_update('$pushAll', args)
end

#push_uniq(*args) ⇒ Object



193
194
195
196
197
# File 'lib/mongo_mapper/document.rb', line 193

def push_uniq(*args)
  criteria, keys = criteria_and_keys_from_args(args)
  keys.each { |key, value | criteria[key] = {'$ne' => value} }
  collection.update(criteria, {'$push' => keys}, :multi => true)
end

#set(*args) ⇒ Object



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

def set(*args)
  modifier_update('$set', args)
end

#set_collection_name(name) ⇒ Object



246
247
248
# File 'lib/mongo_mapper/document.rb', line 246

def set_collection_name(name)
  @collection_name = name
end

#set_database_name(name) ⇒ Object



230
231
232
# File 'lib/mongo_mapper/document.rb', line 230

def set_database_name(name)
  @database_name = name
end

#single_collection_inherited?Boolean

Returns:



271
272
273
# File 'lib/mongo_mapper/document.rb', line 271

def single_collection_inherited?
  keys.has_key?('_type') && single_collection_inherited_superclass?
end

#single_collection_inherited_superclass?Boolean

Returns:



275
276
277
# File 'lib/mongo_mapper/document.rb', line 275

def single_collection_inherited_superclass?
  superclass.respond_to?(:keys) && superclass.keys.has_key?('_type')
end

#timestamps!Object



258
259
260
261
262
# File 'lib/mongo_mapper/document.rb', line 258

def timestamps!
  key :created_at, Time
  key :updated_at, Time
  class_eval { before_save :update_timestamps }
end

#update(*args) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/mongo_mapper/document.rb', line 145

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

#userstamps!Object



264
265
266
267
268
269
# File 'lib/mongo_mapper/document.rb', line 264

def userstamps!
  key :creator_id, ObjectId
  key :updater_id, ObjectId
  belongs_to :creator, :class_name => 'User'
  belongs_to :updater, :class_name => 'User'
end