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. 
- 
  
    
      #insert(model)  ⇒ Mongo::Operation::Write::Insert::Result 
    
    
  
  
  
  
  
  
  
  
  
    Inserts a document into the database. 
- 
  
    
      #model_class  ⇒ Model.class 
    
    
  
  
  
  
  
  
  
  
  
    Derives the model class from the class name, requires and returns it. 
- 
  
    
      #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
| 151 152 153 154 155 156 157 | # File 'lib/mongrep/repository.rb', line 151 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.
| 164 165 166 | # File 'lib/mongrep/repository.rb', line 164 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
| 77 78 79 80 81 | # File 'lib/mongrep/repository.rb', line 77 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
| 104 105 106 107 | # File 'lib/mongrep/repository.rb', line 104 def find_one(query = {}, = {}, &block) # TODO: Pass some context to DocumentNotFoundError find(query, , &block).first || raise(DocumentNotFoundError) end | 
#insert(model) ⇒ Mongo::Operation::Write::Insert::Result
Inserts a document into the database
| 113 114 115 116 117 118 | # File 'lib/mongrep/repository.rb', line 113 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 | 
#model_class ⇒ Model.class
Derives the model class from the class name, requires and returns it
| 40 41 42 43 | # File 'lib/mongrep/repository.rb', line 40 def model_class model_name = self.class.name.demodulize.singularize Mongrep.models_namespace.const_get(model_name) end | 
#update(model, options = {}) ⇒ Mongo::Operation::Write::Update::Result
Update an existing document in the database
| 130 131 132 133 134 135 136 137 138 139 | # File 'lib/mongrep/repository.rb', line 130 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 |