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
- #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
- #first(options = {}) ⇒ Object
- #first_or_create(arg) ⇒ Object
- #first_or_new(arg) ⇒ Object
- #inherited(subclass) ⇒ Object
- #last(options = {}) ⇒ Object
- #set_collection_name(name) ⇒ Object
- #set_database_name(name) ⇒ Object
- #single_collection_inherited? ⇒ Boolean
- #single_collection_inherited_superclass? ⇒ Boolean
- #update(*args) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
104 105 106 |
# File 'lib/mongo_mapper/document.rb', line 104 def all(={}) find_many() end |
#collection ⇒ Object
186 187 188 |
# File 'lib/mongo_mapper/document.rb', line 186 def collection database.collection(collection_name) end |
#collection_name ⇒ Object
182 183 184 |
# File 'lib/mongo_mapper/document.rb', line 182 def collection_name @collection_name ||= self.to_s.tableize.gsub(/\//, '.') 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(options = {}) ⇒ Object
108 109 110 |
# File 'lib/mongo_mapper/document.rb', line 108 def count(={}) collection.find(to_criteria()).count 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 |
#create!(*docs) ⇒ Object
120 121 122 |
# File 'lib/mongo_mapper/document.rb', line 120 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#database ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/mongo_mapper/document.rb', line 170 def database if database_name.nil? MongoMapper.database else connection.db(database_name) end end |
#database_name ⇒ Object
166 167 168 |
# File 'lib/mongo_mapper/document.rb', line 166 def database_name @database_name end |
#delete(*ids) ⇒ Object
133 134 135 |
# File 'lib/mongo_mapper/document.rb', line 133 def delete(*ids) collection.remove(to_criteria(:_id => ids.flatten)) end |
#delete_all(options = {}) ⇒ Object
137 138 139 |
# File 'lib/mongo_mapper/document.rb', line 137 def delete_all(={}) collection.remove(to_criteria()) end |
#destroy(*ids) ⇒ Object
141 142 143 |
# File 'lib/mongo_mapper/document.rb', line 141 def destroy(*ids) find_some!(ids.flatten).each(&:destroy) end |
#destroy_all(options = {}) ⇒ Object
145 146 147 |
# File 'lib/mongo_mapper/document.rb', line 145 def destroy_all(={}) find_each() { |document| document.destroy } end |
#ensure_index(name_or_array, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/mongo_mapper/document.rb', line 42 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 collection.create_index(keys_to_index, [:unique]) end |
#exists?(options = {}) ⇒ Boolean
112 113 114 |
# File 'lib/mongo_mapper/document.rb', line 112 def exists?(={}) !count().zero? end |
#find(*args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mongo_mapper/document.rb', line 52 def find(*args) assert_no_first_last_or_all(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
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mongo_mapper/document.rb', line 64 def find!(*args) assert_no_first_last_or_all(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
83 84 85 |
# File 'lib/mongo_mapper/document.rb', line 83 def find_by_id(id) find(id) end |
#find_each(options = {}) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/mongo_mapper/document.rb', line 76 def find_each(={}) criteria, = to_query() collection.find(criteria, ).each do |doc| yield load(doc) end end |
#first(options = {}) ⇒ Object
95 96 97 |
# File 'lib/mongo_mapper/document.rb', line 95 def first(={}) find_one() end |
#first_or_create(arg) ⇒ Object
87 88 89 |
# File 'lib/mongo_mapper/document.rb', line 87 def first_or_create(arg) first(arg) || create(arg) end |
#first_or_new(arg) ⇒ Object
91 92 93 |
# File 'lib/mongo_mapper/document.rb', line 91 def first_or_new(arg) first(arg) || new(arg) end |
#inherited(subclass) ⇒ Object
37 38 39 40 |
# File 'lib/mongo_mapper/document.rb', line 37 def inherited(subclass) subclass.set_collection_name(collection_name) super end |
#last(options = {}) ⇒ Object
99 100 101 102 |
# File 'lib/mongo_mapper/document.rb', line 99 def last(={}) raise ':order option must be provided when using last' if [:order].blank? find_one(.merge(:order => invert_order_clause([:order]))) end |
#set_collection_name(name) ⇒ Object
178 179 180 |
# File 'lib/mongo_mapper/document.rb', line 178 def set_collection_name(name) @collection_name = name end |
#set_database_name(name) ⇒ Object
162 163 164 |
# File 'lib/mongo_mapper/document.rb', line 162 def set_database_name(name) @database_name = name end |
#single_collection_inherited? ⇒ Boolean
190 191 192 |
# File 'lib/mongo_mapper/document.rb', line 190 def single_collection_inherited? keys.key?(:_type) && single_collection_inherited_superclass? end |
#single_collection_inherited_superclass? ⇒ Boolean
194 195 196 |
# File 'lib/mongo_mapper/document.rb', line 194 def single_collection_inherited_superclass? superclass.respond_to?(:keys) && superclass.keys.key?(:_type) end |
#update(*args) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/mongo_mapper/document.rb', line 124 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |