Class: Mongrep::Repository Abstract

Inherits:
Object
  • Object
show all
Includes:
Abstractize
Defined in:
lib/mongrep/repository.rb

Overview

This class is abstract.

The base class for all repositories

Defined Under Namespace

Classes: DocumentExistsError, DocumentNotFoundError, UnpersistedModelError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mongo_client, collection_name = self.class.name.demodulize.underscore) ⇒ Repository

Returns a new instance of Repository.

Parameters:

  • mongo_client (Mongo::Client)

    The mongodb client to use

  • collection_name (String) (defaults to: self.class.name.demodulize.underscore)

    The name of the collection to use defaults to the demodulized and underscored class name



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

#collectionObject (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

Parameters:

  • model (Model)

    The model representing the document to be updated

Returns:

  • (Mongo::Operation::Write::Delete::Result)

    The result of the delete operation

Raises:



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.

Parameters:

  • field (Symbol, String)

    The field or dot notated path to the field

Returns:

  • (Array)

    An array with the distinct values



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

Examples:

With query hash

result = repository.find(name: 'test')

With Query object

repeating_query = Query.new(name: 'test')
result = repository.find(repeating_query)

With code block

result = repository.find do |query|
  query.where(name: 'test 1').or(name: 'test 2')
end

With query hash and options

result = repository.find({ name: 'test' }, limit: 1)

Overloads:

  • #find(query) ⇒ QueryResult<Model>

    Parameters:

    • query (Hash, Query)

      The mongodb query to perform

  • #find(query, options) ⇒ QueryResult<Model>

    Parameters:

    • query (Hash, Query)

      The mongodb query to perform

    • options (Hash)

      Options to pass to the query

  • #find {|query| ... } ⇒ QueryResult<Model>

    Yield Parameters:

    • query (Query)

      A new query

    Yield Returns:

    • (Query)

      The query to be used

  • #find(query) {|query| ... } ⇒ QueryResult<Model>

    Parameters:

    • query (Hash, Query)

      The initial query

    Yield Parameters:

    • query (Query)

      The query object

    Yield Returns:

    • (Query)

      The final query to be used

  • #find(query, options) {|query| ... } ⇒ QueryResult<Model>

    Parameters:

    • query (Hash, Query)

      The initial query

    • options (Hash)

      Options to pass to the query

    Yield Parameters:

    • query (Query)

      The query object

    Yield Returns:

    • (Query)

      The final query to be used

Returns:

See Also:



82
83
84
85
86
# File 'lib/mongrep/repository.rb', line 82

def find(query = {}, options = {})
  query_object = query.is_a?(Hash) ? Query.new(query) : query
  query_object = yield(query_object) if block_given?
  execute_query(query_object, options)
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

Overloads:

  • #find_one(query) ⇒ Model

    Parameters:

    • query (Hash, Query)

      The mongodb query to perform

  • #find_one(query, options) ⇒ Model

    Parameters:

    • query (Hash, Query)

      The mongodb query to perform

    • options (Hash)

      Options to pass to the query

  • #find_one {|query| ... } ⇒ Model

    Yield Parameters:

    • query (Query)

      A new query

    Yield Returns:

    • (Query)

      The query to be used

  • #find_one(query) {|query| ... } ⇒ Model

    Parameters:

    • query (Hash, Query)

      The initial query

    Yield Parameters:

    • query (Query)

      The query object

    Yield Returns:

    • (Query)

      The final query to be used

  • #find_one(query, options) {|query| ... } ⇒ Model

    Parameters:

    • query (Hash, Query)

      The initial query

    • options (Hash)

      Options to pass to the query

    Yield Parameters:

    • query (Query)

      The query object

    Yield Returns:

    • (Query)

      The final query to be used

Returns:

  • (Model)

    The single model instance representing the document matching the query

Raises:



109
110
111
112
# File 'lib/mongrep/repository.rb', line 109

def find_one(query = {}, options = {}, &block)
  # TODO: Pass some context to DocumentNotFoundError
  find(query, options, &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.

Examples:

repository = Shop::ShoppingCarts.new(mongo_client)
repository.initialize_model(document)
#=> #<Shop::Models::ShoppingCart:... >

Returns:

  • (Model)

    A model instance



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

Parameters:

  • model (Model)

    The model representing the document to be inserted

Returns:

  • (Mongo::Operation::Write::Insert::Result)

    The result of the insert operation



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

Parameters:

  • model (Model)

    The model representing the document to be updated

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :fields (Array<String>)

    The specific fields to update. If this option is omitted the whole document is updated

Returns:

  • (Mongo::Operation::Write::Update::Result)

    The result of the update operation

Raises:



135
136
137
138
139
140
141
142
143
144
# File 'lib/mongrep/repository.rb', line 135

def update(model, options = {})
  check_persistence!(model)
  result = collection.update_one(
    id_query(model),
    update_hash(model, options[:fields])
  )
  # TODO: Pass some context to DocumentNotFoundError
  raise(DocumentNotFoundError) if result.documents.first['n'].zero?
  result
end