Module: MongoMapper::Document::ClassMethods
- Defined in:
- lib/mongo_mapper/document.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #collection ⇒ Object
- #collection_name ⇒ Object
- #connection(mongo_connection = nil) ⇒ Object
- #count(options = {}) ⇒ Object
- #create(*docs) ⇒ Object
- #create!(*docs) ⇒ Object
- #database ⇒ Object
- #database_name ⇒ Object
- #decrement(*args) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_all(options = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
- #destroy_all(options = {}) ⇒ Object
- #ensure_index(name_or_array, options = {}) ⇒ Object
- #exists?(options = {}) ⇒ Boolean
- #find(*args) ⇒ Object
- #find!(*args) ⇒ Object
- #find_by_id(id) ⇒ Object
- #first(options = {}) ⇒ Object
- #increment(*args) ⇒ Object
- #key(*args) ⇒ Object
- #last(options = {}) ⇒ Object
- #paginate(options) ⇒ Object
- #pull(*args) ⇒ Object
- #pull_all(*args) ⇒ Object
- #push(*args) ⇒ Object
- #push_all(*args) ⇒ Object
- #push_uniq(*args) ⇒ Object
- #set(*args) ⇒ Object
- #set_collection_name(name) ⇒ Object
- #set_database_name(name) ⇒ Object
- #single_collection_inherited? ⇒ Boolean
- #single_collection_inherited_superclass? ⇒ Boolean
- #timestamps! ⇒ Object
- #update(*args) ⇒ Object
- #userstamps! ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
121 122 123 |
# File 'lib/mongo_mapper/document.rb', line 121 def all(={}) find_every() end |
#collection ⇒ Object
254 255 256 |
# File 'lib/mongo_mapper/document.rb', line 254 def collection database.collection(collection_name) end |
#collection_name ⇒ Object
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(={}) collection.find(to_criteria()).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 |
#database ⇒ Object
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_name ⇒ Object
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(={}) collection.remove(to_criteria()) 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(={}) all().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, ={}) 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, ) end |
#exists?(options = {}) ⇒ Boolean
133 134 135 |
# File 'lib/mongo_mapper/document.rb', line 133 def exists?(={}) !count().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) = args. case args.first when :first then first() when :last then last() when :all then find_every() when Array then find_some(args, ) else case args.size when 0 raise DocumentNotFound, "Couldn't find without an ID" when 1 find_one!(.merge({:_id => args[0]})) else find_some(args, ) 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(={}) find_one() 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(={}) raise ':order option must be provided when using last' if [:order].blank? find_one(.merge(:order => invert_order_clause([: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() per_page = .delete(:per_page) || self.per_page page = .delete(:page) total_entries = count() pagination = Pagination::PaginationProxy.new(total_entries, page, per_page) .merge!(:limit => pagination.limit, :skip => pagination.skip) pagination.subject = find_every() 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
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
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 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 |