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
- #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
- #single_collection_inherited? ⇒ Boolean
- #single_collection_inherited_superclass? ⇒ Boolean
- #update(*args) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
99 100 101 |
# File 'lib/mongo_mapper/document.rb', line 99 def all(={}) find_many() end |
#count(options = {}) ⇒ Object
103 104 105 |
# File 'lib/mongo_mapper/document.rb', line 103 def count(={}) collection.find(to_criteria()).count end |
#create(*docs) ⇒ Object
111 112 113 |
# File 'lib/mongo_mapper/document.rb', line 111 def create(*docs) initialize_each(*docs) { |doc| doc.save } end |
#create!(*docs) ⇒ Object
115 116 117 |
# File 'lib/mongo_mapper/document.rb', line 115 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#delete(*ids) ⇒ Object
128 129 130 |
# File 'lib/mongo_mapper/document.rb', line 128 def delete(*ids) collection.remove(to_criteria(:_id => ids.flatten)) end |
#delete_all(options = {}) ⇒ Object
132 133 134 |
# File 'lib/mongo_mapper/document.rb', line 132 def delete_all(={}) collection.remove(to_criteria()) end |
#destroy(*ids) ⇒ Object
136 137 138 |
# File 'lib/mongo_mapper/document.rb', line 136 def destroy(*ids) find_some!(ids.flatten).each(&:destroy) end |
#destroy_all(options = {}) ⇒ Object
140 141 142 |
# File 'lib/mongo_mapper/document.rb', line 140 def destroy_all(={}) find_each() { |document| document.destroy } end |
#ensure_index(spec, options = {}) ⇒ Object
43 44 45 |
# File 'lib/mongo_mapper/document.rb', line 43 def ensure_index(spec, ={}) collection.create_index(spec, ) end |
#exists?(options = {}) ⇒ Boolean
107 108 109 |
# File 'lib/mongo_mapper/document.rb', line 107 def exists?(={}) !count().zero? end |
#find(*args) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongo_mapper/document.rb', line 47 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
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongo_mapper/document.rb', line 59 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
78 79 80 |
# File 'lib/mongo_mapper/document.rb', line 78 def find_by_id(id) find(id) end |
#find_each(options = {}) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/mongo_mapper/document.rb', line 71 def find_each(={}) criteria, = to_query() collection.find(criteria, ).each do |doc| yield load(doc) end end |
#first(options = {}) ⇒ Object
90 91 92 |
# File 'lib/mongo_mapper/document.rb', line 90 def first(={}) find_one() end |
#first_or_create(args) ⇒ Object
82 83 84 |
# File 'lib/mongo_mapper/document.rb', line 82 def first_or_create(args) first(args) || create(args.reject { |key, value| !key?(key) }) end |
#first_or_new(args) ⇒ Object
86 87 88 |
# File 'lib/mongo_mapper/document.rb', line 86 def first_or_new(args) first(args) || new(args.reject { |key, value| !key?(key) }) end |
#inherited(subclass) ⇒ Object
38 39 40 41 |
# File 'lib/mongo_mapper/document.rb', line 38 def inherited(subclass) subclass.set_collection_name(collection_name) super end |
#last(options = {}) ⇒ Object
94 95 96 97 |
# File 'lib/mongo_mapper/document.rb', line 94 def last(={}) raise ':order option must be provided when using last' if [:order].blank? find_one(.merge(:order => invert_order_clause([:order]))) end |
#single_collection_inherited? ⇒ Boolean
148 149 150 |
# File 'lib/mongo_mapper/document.rb', line 148 def single_collection_inherited? keys.key?(:_type) && single_collection_inherited_superclass? end |
#single_collection_inherited_superclass? ⇒ Boolean
152 153 154 |
# File 'lib/mongo_mapper/document.rb', line 152 def single_collection_inherited_superclass? superclass.respond_to?(:keys) && superclass.keys.key?(:_type) end |
#update(*args) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/mongo_mapper/document.rb', line 119 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |