Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



83
84
85
# File 'lib/mongo_mapper/document.rb', line 83

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

#collectionObject



224
225
226
# File 'lib/mongo_mapper/document.rb', line 224

def collection
  database.collection(collection_name)
end

#collection_nameObject



220
221
222
# File 'lib/mongo_mapper/document.rb', line 220

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

#connection(mongo_connection = nil) ⇒ Object



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

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

#count(options = {}) ⇒ Object



91
92
93
# File 'lib/mongo_mapper/document.rb', line 91

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

#create(*docs) ⇒ Object



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

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

#create!(*docs) ⇒ Object



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

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

#databaseObject



208
209
210
211
212
213
214
# File 'lib/mongo_mapper/document.rb', line 208

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

#database_nameObject



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

def database_name
  @database_name
end

#decrement(*args) ⇒ Object



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

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



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

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

#delete_all(options = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

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

#destroy_all(options = {}) ⇒ Object



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

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

#embeddable?Boolean

Returns:



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

def embeddable?
  false
end

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



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

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:



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

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

#find(*args) ⇒ Object



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

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

#find!(*args) ⇒ Object



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

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



87
88
89
# File 'lib/mongo_mapper/document.rb', line 87

def find_by_id(id)
  find(id)
end

#first(options = {}) ⇒ Object



74
75
76
# File 'lib/mongo_mapper/document.rb', line 74

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

#increment(*args) ⇒ Object



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

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

#inherited(subclass) ⇒ Object



34
35
36
37
# File 'lib/mongo_mapper/document.rb', line 34

def inherited(subclass)
  subclass.set_collection_name(collection_name)
  super
end

#last(options = {}) ⇒ Object



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

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

#pop(*args) ⇒ Object



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

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

#pull(*args) ⇒ Object



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

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

#pull_all(*args) ⇒ Object



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

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

#push(*args) ⇒ Object



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

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

#push_all(*args) ⇒ Object



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

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

#push_uniq(*args) ⇒ Object



155
156
157
158
159
# File 'lib/mongo_mapper/document.rb', line 155

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



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

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

#set_collection_name(name) ⇒ Object



216
217
218
# File 'lib/mongo_mapper/document.rb', line 216

def set_collection_name(name)
  @collection_name = name
end

#set_database_name(name) ⇒ Object



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

def set_database_name(name)
  @database_name = name
end

#single_collection_inherited?Boolean

Returns:



241
242
243
# File 'lib/mongo_mapper/document.rb', line 241

def single_collection_inherited?
  keys.key?(:_type) && single_collection_inherited_superclass?
end

#single_collection_inherited_superclass?Boolean

Returns:



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

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

#timestamps!Object



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

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

#update(*args) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/mongo_mapper/document.rb', line 107

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

#userstamps!Object



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

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