Class: Repository::Base::Internals::SlugFinder
- Inherits:
-
Object
- Object
- Repository::Base::Internals::SlugFinder
- Includes:
- Support
- Defined in:
- lib/repository/base/internals/slug_finder.rb
Overview
Find a slug in the DAO, reporting errors if not successful.
Instance Attribute Summary collapse
- #dao ⇒ Object readonly private
- #factory ⇒ Object readonly private
- #slug ⇒ Object readonly private
Instance Method Summary collapse
-
#entity_for_slug ⇒ Object
private
Builds an entity from an existing DAO record matching the slug.
-
#errors_for_slug ⇒ ActiveModel::Errors
private
Builds an [`ActiveModel::Errors`](api.rubyonrails.org/classes/ActiveModel/Errors.html) instance and adds a slug-not-found message to it.
-
#find ⇒ Repository::Support::StoreResult
Command-pattern method to search underlying DAO for record matching slug.
-
#initialize(slug:, dao:, factory:) ⇒ SlugFinder
constructor
Initializes a new instance of `SlugFinder`.
-
#result_builder(record) ⇒ Object
private
Returns a new `Repository::Support::ResultBuilder` instance, passing the parameter specified to this method as its `#initialize` parameter.
Constructor Details
#initialize(slug:, dao:, factory:) ⇒ SlugFinder
Initializes a new instance of `SlugFinder`.
20 21 22 23 24 |
# File 'lib/repository/base/internals/slug_finder.rb', line 20 def initialize(slug:, dao:, factory:) @slug = slug @dao = dao @factory = factory end |
Instance Attribute Details
#dao ⇒ Object (readonly, private)
41 42 43 |
# File 'lib/repository/base/internals/slug_finder.rb', line 41 def dao @dao end |
#factory ⇒ Object (readonly, private)
41 42 43 |
# File 'lib/repository/base/internals/slug_finder.rb', line 41 def factory @factory end |
#slug ⇒ Object (readonly, private)
41 42 43 |
# File 'lib/repository/base/internals/slug_finder.rb', line 41 def slug @slug end |
Instance Method Details
#entity_for_slug ⇒ Object (private)
Builds an entity from an existing DAO record matching the slug.
46 47 48 49 |
# File 'lib/repository/base/internals/slug_finder.rb', line 46 def entity_for_slug record = dao.where(slug: slug).first factory.create(record) if record end |
#errors_for_slug ⇒ ActiveModel::Errors (private)
Builds an [`ActiveModel::Errors`](api.rubyonrails.org/classes/ActiveModel/Errors.html) instance and adds a slug-not-found message to it.
55 56 57 58 59 |
# File 'lib/repository/base/internals/slug_finder.rb', line 55 def errors_for_slug errors = ActiveModel::Errors.new dao errors.add :slug, "not found: '#{slug}'" errors end |
#find ⇒ Repository::Support::StoreResult
Command-pattern method to search underlying DAO for record matching slug. Returns a `Repository::Support::StoreResult` instance with the corresponding entity on success, or with the error hash built by `Repository::Support::ErrorFactory.create` on failure.
33 34 35 36 37 |
# File 'lib/repository/base/internals/slug_finder.rb', line 33 def find result_builder(entity_for_slug).build do |_failed_record| ErrorFactory.create errors_for_slug end end |
#result_builder(record) ⇒ Object (private)
Returns a new `Repository::Support::ResultBuilder` instance, passing the parameter specified to this method as its `#initialize` parameter.
64 65 66 |
# File 'lib/repository/base/internals/slug_finder.rb', line 64 def result_builder(record) ResultBuilder.new record end |