Module: MongoMapper::Document::ClassMethods
- Defined in:
- lib/mongo_mapper/document.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #count(options = {}) ⇒ Object
- #create(*docs) ⇒ Object
- #create!(*docs) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_all(options = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
- #destroy_all(options = {}) ⇒ Object
- #embeddable? ⇒ Boolean
- #enslave ⇒ Object
- #enslave? ⇒ Boolean
- #ensure_index(spec, 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(args) ⇒ Object
- #first_or_new(args) ⇒ Object
- #inherited(subclass) ⇒ Object
- #last(options = {}) ⇒ Object
- #query(options = {}) ⇒ Object
- #single_collection_inherited? ⇒ Boolean
- #single_collection_inherited_superclass? ⇒ Boolean
- #update(*args) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
97 98 99 |
# File 'lib/mongo_mapper/document.rb', line 97 def all(={}) find_many() end |
#count(options = {}) ⇒ Object
101 102 103 |
# File 'lib/mongo_mapper/document.rb', line 101 def count(={}) query().count end |
#create(*docs) ⇒ Object
109 110 111 |
# File 'lib/mongo_mapper/document.rb', line 109 def create(*docs) initialize_each(*docs) { |doc| doc.save } end |
#create!(*docs) ⇒ Object
113 114 115 |
# File 'lib/mongo_mapper/document.rb', line 113 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#delete(*ids) ⇒ Object
126 127 128 |
# File 'lib/mongo_mapper/document.rb', line 126 def delete(*ids) query(:_id => ids.flatten).remove end |
#delete_all(options = {}) ⇒ Object
130 131 132 |
# File 'lib/mongo_mapper/document.rb', line 130 def delete_all(={}) query().remove end |
#destroy(*ids) ⇒ Object
134 135 136 |
# File 'lib/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/mongo_mapper/document.rb', line 138 def destroy_all(={}) find_each() { |document| document.destroy } end |
#enslave ⇒ Object
146 147 148 |
# File 'lib/mongo_mapper/document.rb', line 146 def enslave @enslave = true end |
#enslave? ⇒ Boolean
150 151 152 |
# File 'lib/mongo_mapper/document.rb', line 150 def enslave? @enslave ||= false end |
#ensure_index(spec, options = {}) ⇒ Object
44 45 46 |
# File 'lib/mongo_mapper/document.rb', line 44 def ensure_index(spec, ={}) collection.create_index(spec, ) end |
#exists?(options = {}) ⇒ Boolean
105 106 107 |
# File 'lib/mongo_mapper/document.rb', line 105 def exists?(={}) !count().zero? end |
#find(*args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongo_mapper/document.rb', line 48 def find(*args) = args. return nil if args.size == 0 if args.first.is_a?(Array) || args.size > 1 find_some(args, ) else query = query().update(:_id => args[0]) find_one(query.to_hash) end end |
#find!(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mongo_mapper/document.rb', line 60 def find!(*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 query = query().update(:_id => args[0]) find_one(query.to_hash) || raise(DocumentNotFound, "Document match #{.inspect} does not exist in #{collection.name} collection") end end |
#find_by_id(id) ⇒ Object
76 77 78 |
# File 'lib/mongo_mapper/document.rb', line 76 def find_by_id(id) find(id) end |
#find_each(options = {}) ⇒ Object
72 73 74 |
# File 'lib/mongo_mapper/document.rb', line 72 def find_each(={}) query().find().each { |doc| yield load(doc) } end |
#first(options = {}) ⇒ Object
88 89 90 |
# File 'lib/mongo_mapper/document.rb', line 88 def first(={}) find_one() end |
#first_or_create(args) ⇒ Object
80 81 82 |
# File 'lib/mongo_mapper/document.rb', line 80 def first_or_create(args) first(args) || create(args.reject { |key, value| !key?(key) }) end |
#first_or_new(args) ⇒ Object
84 85 86 |
# File 'lib/mongo_mapper/document.rb', line 84 def first_or_new(args) first(args) || new(args.reject { |key, value| !key?(key) }) end |
#inherited(subclass) ⇒ Object
39 40 41 42 |
# File 'lib/mongo_mapper/document.rb', line 39 def inherited(subclass) subclass.set_collection_name(collection_name) super end |
#last(options = {}) ⇒ Object
92 93 94 95 |
# File 'lib/mongo_mapper/document.rb', line 92 def last(={}) raise ':order option must be provided when using last' if [:order].blank? find_one(query().reverse.to_hash) end |
#query(options = {}) ⇒ Object
163 164 165 |
# File 'lib/mongo_mapper/document.rb', line 163 def query(={}) Query.new(self, ) end |
#single_collection_inherited? ⇒ Boolean
154 155 156 |
# File 'lib/mongo_mapper/document.rb', line 154 def single_collection_inherited? keys.key?(:_type) && single_collection_inherited_superclass? end |
#single_collection_inherited_superclass? ⇒ Boolean
158 159 160 |
# File 'lib/mongo_mapper/document.rb', line 158 def single_collection_inherited_superclass? superclass.respond_to?(:keys) && superclass.keys.key?(:_type) end |
#update(*args) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/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 |