Class: Repository::Base
- Inherits:
-
Object
- Object
- Repository::Base
- Includes:
- Internals
- Defined in:
- lib/repository/base.rb,
lib/repository/base/internals/record_deleter.rb,
lib/repository/base/internals/record_saver.rb,
lib/repository/base/internals/record_updater.rb,
lib/repository/base/internals/slug_finder.rb,
lib/repository/base/version.rb
Overview
Base class for Repository in Data Mapper pattern.
Defined Under Namespace
Modules: Internals
Constant Summary
- VERSION =
Gem version, following [RubyGems.org](rubygems.org) and [SemVer](semver.org/) conventions.
'0.4.0'
Instance Attribute Summary collapse
-
#dao ⇒ Object
readonly
Returns the value of attribute dao.
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
Instance Method Summary collapse
-
#add(entity) ⇒ Repository::Support::StoreResult
Add a new record with attributes matching the specified entity to the associated DAO.
-
#all ⇒ Array
Return an array of entities matching all records currently in the associated DAO.
-
#delete(identifier) ⇒ Repository::Support::StoreResult
Remove a record from the underlying DAO whose slug matches the passed-in identifier.
-
#filtered_attributes_for(entity) ⇒ Object
private
supporting #add.
-
#find_by_slug(slug) ⇒ Repository::Support::StoreResult
Find a record in the DAO and, on success, return a corresponding entity using the specified [slug](en.wikipedia.org/wiki/Semantic_URL#Slug), not a numeric record ID, as a search identifier.
-
#initialize(factory:, dao:) ⇒ Base
constructor
Initialise a new `Repository::Base` instance.
-
#update(identifier:, updated_attrs:) ⇒ Object
Update a record in the DAO corresponding to the specified identifier, using the specified attribute-name/value pairs.
-
#validate_initializer_argument(arg_sym, value) ⇒ boolean
private
Verifies that parameter passed to #initialize is a Class.
Constructor Details
#initialize(factory:, dao:) ⇒ Base
Initialise a new `Repository::Base` instance.
32 33 34 35 36 37 |
# File 'lib/repository/base.rb', line 32 def initialize(factory:, dao:) validate_initializer_argument(:dao, dao) validate_initializer_argument(:factory, factory) @factory = factory @dao = dao end |
Instance Attribute Details
#dao ⇒ Object (readonly)
Returns the value of attribute dao
26 27 28 |
# File 'lib/repository/base.rb', line 26 def dao @dao end |
#factory ⇒ Object (readonly)
Returns the value of attribute factory
26 27 28 |
# File 'lib/repository/base.rb', line 26 def factory @factory end |
Instance Method Details
#add(entity) ⇒ Repository::Support::StoreResult
Add a new record with attributes matching the specified entity to the associated DAO.
44 45 46 47 |
# File 'lib/repository/base.rb', line 44 def add(entity) record = dao.new filtered_attributes_for(entity) RecordSaver.new(record: record, factory: factory).result end |
#all ⇒ Array
Return an array of entities matching all records currently in the associated DAO.
53 54 55 |
# File 'lib/repository/base.rb', line 53 def all dao.all.map { |record| factory.create record } end |
#delete(identifier) ⇒ Repository::Support::StoreResult
Remove a record from the underlying DAO whose slug matches the passed-in identifier.
64 65 66 67 |
# File 'lib/repository/base.rb', line 64 def delete(identifier) RecordDeleter.new(identifier: identifier, dao: dao, factory: factory) .delete end |
#filtered_attributes_for(entity) ⇒ Object (private)
supporting #add
111 112 113 |
# File 'lib/repository/base.rb', line 111 def filtered_attributes_for(entity) # :nodoc: entity.attributes.to_hash.reject { |k, _v| k == :errors } end |
#find_by_slug(slug) ⇒ Repository::Support::StoreResult
Find a record in the DAO and, on success, return a corresponding entity using the specified [slug](en.wikipedia.org/wiki/Semantic_URL#Slug), not a numeric record ID, as a search identifier.
77 78 79 |
# File 'lib/repository/base.rb', line 77 def find_by_slug(slug) SlugFinder.new(slug: slug, dao: dao, factory: factory).find end |
#update(identifier:, updated_attrs:) ⇒ Object
Update a record in the DAO corresponding to the specified identifier, using the specified attribute-name/value pairs.
90 91 92 93 |
# File 'lib/repository/base.rb', line 90 def update(identifier:, updated_attrs:) RecordUpdater.new(identifier: identifier, updated_attrs: updated_attrs, dao: dao, factory: factory).update end |
#validate_initializer_argument(arg_sym, value) ⇒ boolean (private)
Verifies that parameter passed to #initialize is a Class.
104 105 106 107 |
# File 'lib/repository/base.rb', line 104 def validate_initializer_argument(arg_sym, value) = "the :#{arg_sym} argument must be a Class" raise ArgumentError, unless value.respond_to? :new end |