Class: Mongrep::Repository Abstract
- Inherits:
-
Object
- Object
- Mongrep::Repository
- Includes:
- Abstractize
- Defined in:
- lib/mongrep/repository.rb
Overview
The base class for all repositories
Defined Under Namespace
Classes: DocumentExistsError, DocumentNotFoundError, UnpersistedModelError
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
-
#delete(model) ⇒ Mongo::Operation::Write::Delete::Result
Delete an existing document from the database.
-
#distinct(field) ⇒ Array
Get a distinct list of values for the given field over all documents in the collection.
-
#find(query = {}, options = {}) ⇒ QueryResult<Model>
Finds documents matching the given query.
-
#find_one(query = {}, options = {}, &block) ⇒ Model
Finds a single document matching the given query.
-
#initialize(mongo_client, collection_name = self.class.name.demodulize.underscore) ⇒ Repository
constructor
A new instance of Repository.
-
#initialize_model(document) ⇒ Model
Derives the model class from the class name and instanciates a model of that class with the given document.
-
#insert(model) ⇒ Mongo::Operation::Write::Insert::Result
Inserts a document into the database.
-
#update(model, options = {}) ⇒ Mongo::Operation::Write::Update::Result
Update an existing document in the database.
Constructor Details
#initialize(mongo_client, collection_name = self.class.name.demodulize.underscore) ⇒ Repository
Returns a new instance of Repository.
28 29 30 31 32 33 |
# File 'lib/mongrep/repository.rb', line 28 def initialize( mongo_client, collection_name = self.class.name.demodulize.underscore ) @collection = mongo_client[collection_name] end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
23 24 25 |
# File 'lib/mongrep/repository.rb', line 23 def collection @collection end |
Instance Method Details
#delete(model) ⇒ Mongo::Operation::Write::Delete::Result
Delete an existing document from the database
156 157 158 159 160 161 162 |
# File 'lib/mongrep/repository.rb', line 156 def delete(model) check_persistence!(model) result = collection.delete_one(id_query(model)) # TODO: Pass some context to DocumentNotFoundError raise(DocumentNotFoundError) if result.documents.first['n'].zero? result end |
#distinct(field) ⇒ Array
Get a distinct list of values for the given field over all documents in the collection.
169 170 171 |
# File 'lib/mongrep/repository.rb', line 169 def distinct(field) collection.distinct(field) end |
#find(query) ⇒ QueryResult<Model> #find(query, options) ⇒ QueryResult<Model> #find {|query| ... } ⇒ QueryResult<Model> #find(query) {|query| ... } ⇒ QueryResult<Model> #find(query, options) {|query| ... } ⇒ QueryResult<Model>
Finds documents matching the given query
82 83 84 85 86 |
# File 'lib/mongrep/repository.rb', line 82 def find(query = {}, = {}) query_object = query.is_a?(Hash) ? Query.new(query) : query query_object = yield(query_object) if block_given? execute_query(query_object, ) end |
#find_one(query) ⇒ Model #find_one(query, options) ⇒ Model #find_one {|query| ... } ⇒ Model #find_one(query) {|query| ... } ⇒ Model #find_one(query, options) {|query| ... } ⇒ Model
Finds a single document matching the given query
109 110 111 112 |
# File 'lib/mongrep/repository.rb', line 109 def find_one(query = {}, = {}, &block) # TODO: Pass some context to DocumentNotFoundError find(query, , &block).first || raise(DocumentNotFoundError) end |
#initialize_model(document) ⇒ Model
Derives the model class from the class name and instanciates a model of that class with the given document. This is passed to a QueryResult which uses it to return model instances. It should be overridden when implementing polymorphism based on a field in the document.
44 45 46 47 48 |
# File 'lib/mongrep/repository.rb', line 44 def initialize_model(document) model_name = self.class.name.demodulize.singularize model_class = Mongrep.models_namespace.const_get(model_name) model_class.new(document) end |
#insert(model) ⇒ Mongo::Operation::Write::Insert::Result
Inserts a document into the database
118 119 120 121 122 123 |
# File 'lib/mongrep/repository.rb', line 118 def insert(model) collection.insert_one(model.to_h) rescue Mongo::Error::OperationFailure => error # TODO: Pass relevant info to DocumentExistsError message raise(error.duplicate_key_error? ? DocumentExistsError : error) end |
#update(model, options = {}) ⇒ Mongo::Operation::Write::Update::Result
Update an existing document in the database
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mongrep/repository.rb', line 135 def update(model, = {}) check_persistence!(model) result = collection.update_one( id_query(model), update_hash(model, [:fields]) ) # TODO: Pass some context to DocumentNotFoundError raise(DocumentNotFoundError) if result.documents.first['n'].zero? result end |