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
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
56
57
58
|
# File 'lib/mongomapper/document.rb', line 56
def all(options={})
find_every(options)
end
|
#collection(name = nil) ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/mongomapper/document.rb', line 132
def collection(name=nil)
if name.nil?
@collection ||= database.collection(self.to_s.demodulize.tableize)
else
@collection = database.collection(name)
end
@collection
end
|
#connection(mongo_connection = nil) ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/mongomapper/document.rb', line 114
def connection(mongo_connection=nil)
if mongo_connection.nil?
@connection ||= MongoMapper.connection
else
@connection = mongo_connection
end
@connection
end
|
#count(conditions = {}) ⇒ Object
67
68
69
|
# File 'lib/mongomapper/document.rb', line 67
def count(conditions={})
collection.find(FinderOptions.to_mongo_criteria(conditions)).count
end
|
#create(*docs) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/mongomapper/document.rb', line 71
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
123
124
125
126
127
128
129
130
|
# File 'lib/mongomapper/document.rb', line 123
def database(name=nil)
if name.nil?
@database ||= MongoMapper.database
else
@database = connection.db(name)
end
@database
end
|
#delete(*ids) ⇒ Object
96
97
98
99
|
# File 'lib/mongomapper/document.rb', line 96
def delete(*ids)
criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten)
collection.remove(criteria)
end
|
#delete_all(conditions = {}) ⇒ Object
101
102
103
104
|
# File 'lib/mongomapper/document.rb', line 101
def delete_all(conditions={})
criteria = FinderOptions.to_mongo_criteria(conditions)
collection.remove(criteria)
end
|
#destroy(*ids) ⇒ Object
106
107
108
|
# File 'lib/mongomapper/document.rb', line 106
def destroy(*ids)
find_some(ids.flatten).each(&:destroy)
end
|
#destroy_all(conditions = {}) ⇒ Object
110
111
112
|
# File 'lib/mongomapper/document.rb', line 110
def destroy_all(conditions={})
find(:all, :conditions => conditions).each(&:destroy)
end
|
#find(*args) ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/mongomapper/document.rb', line 24
def find(*args)
options = args.
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
60
61
62
63
64
65
|
# File 'lib/mongomapper/document.rb', line 60
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
48
49
50
|
# File 'lib/mongomapper/document.rb', line 48
def first(options={})
find_first(options)
end
|
#last(options = {}) ⇒ Object
52
53
54
|
# File 'lib/mongomapper/document.rb', line 52
def last(options={})
find_last(options)
end
|
#paginate(options) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/mongomapper/document.rb', line 35
def paginate(options)
per_page = options.delete(: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[:offset] = collection.offset
collection.subject = find_every(options)
collection
end
|
#timestamps! ⇒ Object
141
142
143
144
145
146
|
# File 'lib/mongomapper/document.rb', line 141
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'}})
86
87
88
89
90
91
92
93
94
|
# File 'lib/mongomapper/document.rb', line 86
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
|
#validates_exclusion_of(*args) ⇒ Object
#validates_inclusion_of(*args) ⇒ Object
#validates_uniqueness_of(*args) ⇒ Object