Module: MongoMapper::Document::ClassMethods
- Defined in:
- lib/mongomapper/document.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #collection(name = nil) ⇒ Object
- #connection(mongo_connection = nil) ⇒ Object
-
#count(conditions = {}) ⇒ Object
TODO: remove the rescuing when ruby driver works correctly.
- #create(*docs) ⇒ Object
- #database(name = nil) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_all(conditions = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
- #destroy_all(conditions = {}) ⇒ Object
- #find(*args) ⇒ Object
- #find_by_id(id) ⇒ Object
- #first(options = {}) ⇒ Object
- #last(options = {}) ⇒ Object
- #paginate(options) ⇒ Object
-
#update(*args) ⇒ Object
For updating single document Person.update(1, => ‘bar’).
- #validates_exclusion_of(*args) ⇒ Object
- #validates_inclusion_of(*args) ⇒ Object
- #validates_uniqueness_of(*args) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
61 62 63 |
# File 'lib/mongomapper/document.rb', line 61 def all(={}) find_every() end |
#collection(name = nil) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/mongomapper/document.rb', line 134 def collection(name=nil) if name.nil? @collection ||= database.collection(self.to_s.demodulize.tableize) else @collection = database.collection(name) end @collection end |
#connection(mongo_connection = nil) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/mongomapper/document.rb', line 116 def connection(mongo_connection=nil) if mongo_connection.nil? @connection ||= MongoMapper.connection else @connection = mongo_connection end @connection end |
#count(conditions = {}) ⇒ Object
TODO: remove the rescuing when ruby driver works correctly
72 73 74 |
# File 'lib/mongomapper/document.rb', line 72 def count(conditions={}) collection.count(FinderOptions.to_mongo_criteria(conditions)) end |
#create(*docs) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/mongomapper/document.rb', line 76 def create(*docs) instances = [] docs.flatten.each do |attrs| doc = new(attrs); doc.save instances << doc end instances.size == 1 ? instances[0] : instances end |
#database(name = nil) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/mongomapper/document.rb', line 125 def database(name=nil) if name.nil? @database ||= MongoMapper.database else @database = connection.db(name) end @database end |
#delete(*ids) ⇒ Object
100 101 102 |
# File 'lib/mongomapper/document.rb', line 100 def delete(*ids) collection.remove(:_id => {'$in' => ids.flatten}) end |
#delete_all(conditions = {}) ⇒ Object
104 105 106 |
# File 'lib/mongomapper/document.rb', line 104 def delete_all(conditions={}) collection.remove(FinderOptions.to_mongo_criteria(conditions)) end |
#destroy(*ids) ⇒ Object
108 109 110 |
# File 'lib/mongomapper/document.rb', line 108 def destroy(*ids) find_some(ids.flatten).each(&:destroy) end |
#destroy_all(conditions = {}) ⇒ Object
112 113 114 |
# File 'lib/mongomapper/document.rb', line 112 def destroy_all(conditions={}) find(:all, :conditions => conditions).each(&:destroy) end |
#find(*args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mongomapper/document.rb', line 28 def find(*args) = args. case args.first when :first then find_first() when :last then find_last() when :all then find_every() else find_from_ids(args) end end |
#find_by_id(id) ⇒ Object
65 66 67 68 69 |
# File 'lib/mongomapper/document.rb', line 65 def find_by_id(id) if doc = collection.find_first({:_id => id}) new(doc) end end |
#first(options = {}) ⇒ Object
53 54 55 |
# File 'lib/mongomapper/document.rb', line 53 def first(={}) find_first() end |
#last(options = {}) ⇒ Object
57 58 59 |
# File 'lib/mongomapper/document.rb', line 57 def last(={}) find_last() end |
#paginate(options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongomapper/document.rb', line 39 def paginate() per_page = .delete(:per_page) page = .delete(:page) total_entries = count() collection = Pagination::PaginationProxy.new(total_entries, page, per_page) [:limit] = collection.limit [:skip] = collection.skip collection.subject = find_every() collection end |
#update(*args) ⇒ Object
For updating single document
Person.update(1, {:foo => 'bar'})
For updating multiple documents at once:
Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
90 91 92 93 94 95 96 97 98 |
# File 'lib/mongomapper/document.rb', line 90 def update(*args) updating_multiple = args.length == 1 if updating_multiple update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |
#validates_exclusion_of(*args) ⇒ Object
147 148 149 |
# File 'lib/mongomapper/document.rb', line 147 def validates_exclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesExclusionOf) end |
#validates_inclusion_of(*args) ⇒ Object
151 152 153 |
# File 'lib/mongomapper/document.rb', line 151 def validates_inclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesInclusionOf) end |
#validates_uniqueness_of(*args) ⇒ Object
143 144 145 |
# File 'lib/mongomapper/document.rb', line 143 def validates_uniqueness_of(*args) add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf) end |