Module: MongoMapper::Document::ClassMethods
- Defined in:
- lib/novelys_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
- #embeddable? ⇒ Boolean
- #ensure_index(name_or_array, options = {}) ⇒ Object
- #exists?(options = {}) ⇒ Boolean
- #find(*args) ⇒ Object
- #find!(*args) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_each(options = {}) ⇒ Object
- #find_or_create(arg) ⇒ Object
- #first(options = {}) ⇒ Object
- #increment(*args) ⇒ Object
- #inherited(subclass) ⇒ Object
- #last(options = {}) ⇒ Object
- #pop(*args) ⇒ 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
97 98 99 |
# File 'lib/novelys_mongo_mapper/document.rb', line 97 def all(={}) find_many() end |
#collection ⇒ Object
220 221 222 |
# File 'lib/novelys_mongo_mapper/document.rb', line 220 def collection database.collection(collection_name) end |
#collection_name ⇒ Object
216 217 218 |
# File 'lib/novelys_mongo_mapper/document.rb', line 216 def collection_name @collection_name ||= self.to_s.tableize.gsub(/\//, '.') end |
#connection(mongo_connection = nil) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/novelys_mongo_mapper/document.rb', line 187 def connection(mongo_connection=nil) if mongo_connection.nil? @connection ||= MongoMapper.connection else @connection = mongo_connection end @connection end |
#count(options = {}) ⇒ Object
101 102 103 |
# File 'lib/novelys_mongo_mapper/document.rb', line 101 def count(={}) collection.find(to_criteria()).count end |
#create(*docs) ⇒ Object
109 110 111 |
# File 'lib/novelys_mongo_mapper/document.rb', line 109 def create(*docs) initialize_each(*docs) { |doc| doc.save } end |
#create!(*docs) ⇒ Object
113 114 115 |
# File 'lib/novelys_mongo_mapper/document.rb', line 113 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#database ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/novelys_mongo_mapper/document.rb', line 204 def database if database_name.nil? MongoMapper.database else connection.db(database_name) end end |
#database_name ⇒ Object
200 201 202 |
# File 'lib/novelys_mongo_mapper/document.rb', line 200 def database_name @database_name end |
#decrement(*args) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/novelys_mongo_mapper/document.rb', line 146 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
126 127 128 |
# File 'lib/novelys_mongo_mapper/document.rb', line 126 def delete(*ids) collection.remove(to_criteria(:_id => ids.flatten)) end |
#delete_all(options = {}) ⇒ Object
130 131 132 |
# File 'lib/novelys_mongo_mapper/document.rb', line 130 def delete_all(={}) collection.remove(to_criteria()) end |
#destroy(*ids) ⇒ Object
134 135 136 |
# File 'lib/novelys_mongo_mapper/document.rb', line 134 def destroy(*ids) find_some!(ids.flatten).each(&:destroy) end |
#destroy_all(options = {}) ⇒ Object
138 139 140 |
# File 'lib/novelys_mongo_mapper/document.rb', line 138 def destroy_all(={}) all().each(&:destroy) end |
#embeddable? ⇒ Boolean
183 184 185 |
# File 'lib/novelys_mongo_mapper/document.rb', line 183 def false end |
#ensure_index(name_or_array, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/novelys_mongo_mapper/document.rb', line 39 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
105 106 107 |
# File 'lib/novelys_mongo_mapper/document.rb', line 105 def exists?(={}) !count().zero? end |
#find(*args) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/novelys_mongo_mapper/document.rb', line 49 def find(*args) find_all_first_last_error(args) = args. return nil if args.size == 0 if args.first.is_a?(Array) || args.size > 1 find_some(args, ) else find_one(.merge({:_id => args[0]})) end end |
#find!(*args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/novelys_mongo_mapper/document.rb', line 61 def find!(*args) find_all_first_last_error(args) = args. raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0 if args.first.is_a?(Array) || args.size > 1 find_some!(args, ) else find_one(.merge({:_id => args[0]})) || raise(DocumentNotFound, "Document match #{.inspect} does not exist in #{collection.name} collection") end end |
#find_by_id(id) ⇒ Object
84 85 86 |
# File 'lib/novelys_mongo_mapper/document.rb', line 84 def find_by_id(id) find(id) end |
#find_each(options = {}) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/novelys_mongo_mapper/document.rb', line 77 def find_each(={}) criteria, = () collection.find(criteria, ).each do |doc| yield load(doc) end end |
#find_or_create(arg) ⇒ Object
73 74 75 |
# File 'lib/novelys_mongo_mapper/document.rb', line 73 def find_or_create(arg) first(arg) || create(arg) end |
#first(options = {}) ⇒ Object
88 89 90 |
# File 'lib/novelys_mongo_mapper/document.rb', line 88 def first(={}) find_one() end |
#increment(*args) ⇒ Object
142 143 144 |
# File 'lib/novelys_mongo_mapper/document.rb', line 142 def increment(*args) modifier_update('$inc', args) end |
#inherited(subclass) ⇒ Object
34 35 36 37 |
# File 'lib/novelys_mongo_mapper/document.rb', line 34 def inherited(subclass) subclass.set_collection_name(collection_name) super end |
#last(options = {}) ⇒ Object
92 93 94 95 |
# File 'lib/novelys_mongo_mapper/document.rb', line 92 def last(={}) raise ':order option must be provided when using last' if [:order].blank? find_one(.merge(:order => invert_order_clause([:order]))) end |
#pop(*args) ⇒ Object
179 180 181 |
# File 'lib/novelys_mongo_mapper/document.rb', line 179 def pop(*args) modifier_update('$pop', args) end |
#pull(*args) ⇒ Object
171 172 173 |
# File 'lib/novelys_mongo_mapper/document.rb', line 171 def pull(*args) modifier_update('$pull', args) end |
#pull_all(*args) ⇒ Object
175 176 177 |
# File 'lib/novelys_mongo_mapper/document.rb', line 175 def pull_all(*args) modifier_update('$pullAll', args) end |
#push(*args) ⇒ Object
157 158 159 |
# File 'lib/novelys_mongo_mapper/document.rb', line 157 def push(*args) modifier_update('$push', args) end |
#push_all(*args) ⇒ Object
161 162 163 |
# File 'lib/novelys_mongo_mapper/document.rb', line 161 def push_all(*args) modifier_update('$pushAll', args) end |
#push_uniq(*args) ⇒ Object
165 166 167 168 169 |
# File 'lib/novelys_mongo_mapper/document.rb', line 165 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
153 154 155 |
# File 'lib/novelys_mongo_mapper/document.rb', line 153 def set(*args) modifier_update('$set', args) end |
#set_collection_name(name) ⇒ Object
212 213 214 |
# File 'lib/novelys_mongo_mapper/document.rb', line 212 def set_collection_name(name) @collection_name = name end |
#set_database_name(name) ⇒ Object
196 197 198 |
# File 'lib/novelys_mongo_mapper/document.rb', line 196 def set_database_name(name) @database_name = name end |
#single_collection_inherited? ⇒ Boolean
237 238 239 |
# File 'lib/novelys_mongo_mapper/document.rb', line 237 def single_collection_inherited? keys.key?(:_type) && single_collection_inherited_superclass? end |
#single_collection_inherited_superclass? ⇒ Boolean
241 242 243 |
# File 'lib/novelys_mongo_mapper/document.rb', line 241 def single_collection_inherited_superclass? superclass.respond_to?(:keys) && superclass.keys.key?(:_type) end |
#timestamps! ⇒ Object
224 225 226 227 228 |
# File 'lib/novelys_mongo_mapper/document.rb', line 224 def key :created_at, Time key :updated_at, Time class_eval { before_save :update_timestamps } end |
#update(*args) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/novelys_mongo_mapper/document.rb', line 117 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |