Module: Mongorm::Repository
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongorm/repository.rb,
lib/mongorm/repository/class_methods.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_name ⇒ Object
- #connection ⇒ Object
- #delete(object) ⇒ Object
- #delete_all ⇒ Object
- #fields ⇒ Object
- #find(id) ⇒ Object
- #mapping ⇒ Object
- #save(object) ⇒ Object
- #where(args = {}) ⇒ Object
Instance Method Details
#collection ⇒ Object
30 31 32 |
# File 'lib/mongorm/repository.rb', line 30 def collection connection[collection_name] end |
#collection_name ⇒ Object
14 15 16 |
# File 'lib/mongorm/repository.rb', line 14 def collection_name mapping.to_s.downcase end |
#connection ⇒ Object
26 27 28 |
# File 'lib/mongorm/repository.rb', line 26 def connection self.class.connection end |
#delete(object) ⇒ Object
47 48 49 |
# File 'lib/mongorm/repository.rb', line 47 def delete object collection.remove(:_id => object.id) end |
#delete_all ⇒ Object
51 52 53 |
# File 'lib/mongorm/repository.rb', line 51 def delete_all collection.remove end |
#fields ⇒ Object
18 19 20 |
# File 'lib/mongorm/repository.rb', line 18 def fields self.class.fields end |
#find(id) ⇒ Object
41 42 43 44 45 |
# File 'lib/mongorm/repository.rb', line 41 def find id hash = collection.find_one(:_id => id) raise Mongorm::Errors::DocumentNotFoundError unless hash __object_from_mongo__(hash) end |
#mapping ⇒ Object
22 23 24 |
# File 'lib/mongorm/repository.rb', line 22 def mapping self.class.mapping end |
#save(object) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/mongorm/repository.rb', line 34 def save object value = fields.inject({}) do |sum, field| sum.merge(Hash[field, object.send(field)]) end object.id = collection.insert(value) end |
#where(args = {}) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/mongorm/repository.rb', line 55 def where(args={}) criteria = self.class.criteria.where(args) collection.find(*criteria.arguments).map do |hash| __object_from_mongo__(hash) end end |