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
- #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
60 61 62 |
# File 'lib/mongomapper/document.rb', line 60 def all(={}) find_every() end |
#collection(name = nil) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/mongomapper/document.rb', line 136 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
118 119 120 121 122 123 124 125 |
# File 'lib/mongomapper/document.rb', line 118 def connection(mongo_connection=nil) if mongo_connection.nil? @connection ||= MongoMapper.connection else @connection = mongo_connection end @connection end |
#count(conditions = {}) ⇒ Object
71 72 73 |
# File 'lib/mongomapper/document.rb', line 71 def count(conditions={}) collection.count(FinderOptions.to_mongo_criteria(conditions)) end |
#create(*docs) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/mongomapper/document.rb', line 75 def create(*docs) instances = [] docs = [{}] if docs.blank? docs.flatten.each do |attrs| doc = new(attrs); doc.save instances << doc end instances.size == 1 ? instances[0] : instances end |
#database(name = nil) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/mongomapper/document.rb', line 127 def database(name=nil) if name.nil? @database ||= MongoMapper.database else @database = connection.db(name) end @database end |
#delete(*ids) ⇒ Object
100 101 102 103 |
# File 'lib/mongomapper/document.rb', line 100 def delete(*ids) criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten) collection.remove(criteria) end |
#delete_all(conditions = {}) ⇒ Object
105 106 107 108 |
# File 'lib/mongomapper/document.rb', line 105 def delete_all(conditions={}) criteria = FinderOptions.to_mongo_criteria(conditions) collection.remove(criteria) end |
#destroy(*ids) ⇒ Object
110 111 112 |
# File 'lib/mongomapper/document.rb', line 110 def destroy(*ids) find_some(ids.flatten).each(&:destroy) end |
#destroy_all(conditions = {}) ⇒ Object
114 115 116 |
# File 'lib/mongomapper/document.rb', line 114 def destroy_all(conditions={}) find(:all, :conditions => conditions).each(&:destroy) end |
#find(*args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mongomapper/document.rb', line 27 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
64 65 66 67 68 69 |
# File 'lib/mongomapper/document.rb', line 64 def find_by_id(id) criteria = FinderOptions.to_mongo_criteria(:_id => id) if doc = collection.find_first(criteria) new(doc) end end |
#first(options = {}) ⇒ Object
52 53 54 |
# File 'lib/mongomapper/document.rb', line 52 def first(={}) find_first() end |
#last(options = {}) ⇒ Object
56 57 58 |
# File 'lib/mongomapper/document.rb', line 56 def last(={}) find_last() end |
#paginate(options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mongomapper/document.rb', line 38 def paginate() per_page = .delete(:per_page) page = .delete(:page) total_entries = count([:conditions] || {}) collection = Pagination::PaginationProxy.new(total_entries, page, per_page) [:limit] = collection.limit [:offset] = collection.offset collection.subject = find_every() collection end |
#update(*args) ⇒ Object
For updating single document Person.update(1, => 'bar')
For updating multiple documents at once: Person.update(=> {:foo => 'bar', '2' => => '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
149 150 151 |
# File 'lib/mongomapper/document.rb', line 149 def validates_exclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesExclusionOf) end |
#validates_inclusion_of(*args) ⇒ Object
153 154 155 |
# File 'lib/mongomapper/document.rb', line 153 def validates_inclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesInclusionOf) end |
#validates_uniqueness_of(*args) ⇒ Object
145 146 147 |
# File 'lib/mongomapper/document.rb', line 145 def validates_uniqueness_of(*args) add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf) end |