class Repository::Base::Internals::SlugFinder
Find a slug in the DAO, reporting errors if not successful. @since 0.0.3
Attributes
Public Class Methods
Initializes a new instance of `SlugFinder`. @param slug [String] [Slug](en.wikipedia.org/wiki/Semantic_URL#Slug)
for record to be deleted.
@param dao Data Access Object implements persistence without business
logic.
# File lib/repository/base/internals/slug_finder.rb, line 20 def initialize(slug:, dao:, factory:) @slug = slug @dao = dao @factory = factory end
Public Instance Methods
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. @return [Repository::Support::StoreResult] @see errors_for_slug
@see result_builder
# 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
Private Instance Methods
Builds an entity from an existing DAO record matching the slug. @return If the slug matches an existing DAO record, returns an entity
as built from that record, otherwise returns `nil`.
# 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
Builds an [`ActiveModel::Errors`](api.rubyonrails.org/classes/ActiveModel/Errors.html) instance and adds a slug-not-found message to it. @return [ActiveModel::Errors] @see find
# 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
Returns a new `Repository::Support::ResultBuilder` instance, passing the parameter specified to this method as its `#initialize` parameter. @param record DAO record to pass to `ResultBuilder#initialize`.
# File lib/repository/base/internals/slug_finder.rb, line 64 def result_builder(record) ResultBuilder.new record end