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)



195
196
197
198
199
200
201
202
203
204
# File 'lib/mongo_mapper/document.rb', line 195

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



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

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

#collectionObject

Returns the mongo ruby driver collection object



183
184
185
# File 'lib/mongo_mapper/document.rb', line 183

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

#collection_nameObject

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



178
179
180
# File 'lib/mongo_mapper/document.rb', line 178

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

#connection(mongo_connection = nil) ⇒ Object



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

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

#count(conditions = {}) ⇒ Object



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

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

#create(*docs) ⇒ Object



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

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

#create!(*docs) ⇒ Object



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

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

#database(name = nil) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/mongo_mapper/document.rb', line 162

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

#delete(*ids) ⇒ Object



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

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

#delete_all(conditions = {}) ⇒ Object



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

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

#destroy(*ids) ⇒ Object



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

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

#destroy_all(conditions = {}) ⇒ Object



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

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

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



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

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:



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

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

#find(*args) ⇒ Object



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

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



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

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



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

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

#key(*args) ⇒ Object



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

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

#last(options = {}) ⇒ Object



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

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



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

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



172
173
174
175
# File 'lib/mongo_mapper/document.rb', line 172

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

#timestamps!Object



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

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


125
126
127
128
129
130
131
132
133
# File 'lib/mongo_mapper/document.rb', line 125

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