Class: Repository::Base::Internals::SlugFinder

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/repository/base/internals/slug_finder.rb

Overview

Find a slug in the DAO, reporting errors if not successful.

Since:

  • 0.0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug:, dao:, factory:) ⇒ SlugFinder

Initializes a new instance of `SlugFinder`.

Parameters:

Since:

  • 0.0.3



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

#daoObject (readonly, private)

Since:

  • 0.0.3



41
42
43
# File 'lib/repository/base/internals/slug_finder.rb', line 41

def dao
  @dao
end

#factoryObject (readonly, private)

Since:

  • 0.0.3



41
42
43
# File 'lib/repository/base/internals/slug_finder.rb', line 41

def factory
  @factory
end

#slugObject (readonly, private)

Since:

  • 0.0.3



41
42
43
# File 'lib/repository/base/internals/slug_finder.rb', line 41

def slug
  @slug
end

Instance Method Details

#entity_for_slugObject (private)

Builds an entity from an existing DAO record matching the slug.

Returns:

  • If the slug matches an existing DAO record, returns an entity as built from that record, otherwise returns `nil`.

Since:

  • 0.0.3



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_slugActiveModel::Errors (private)

Builds an [`ActiveModel::Errors`](api.rubyonrails.org/classes/ActiveModel/Errors.html) instance and adds a slug-not-found message to it.

Returns:

  • (ActiveModel::Errors)

See Also:

Since:

  • 0.0.3



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

#findRepository::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.

Returns:

  • (Repository::Support::StoreResult)

See Also:

Since:

  • 0.0.3



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.

Parameters:

  • record

    DAO record to pass to `ResultBuilder#initialize`.

Since:

  • 0.0.3



64
65
66
# File 'lib/repository/base/internals/slug_finder.rb', line 64

def result_builder(record)
  ResultBuilder.new record
end