Class: Repository::Base::Internals::RecordUpdater

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

Overview

Stows away details of reporting update success/failure.

Since:

  • 0.0.4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier:, updated_attrs:, dao:, factory:) ⇒ RecordUpdater

Initializes a new instance of `RecordUpdater`.

Parameters:

  • identifier (String)

    Slug uniquely identifying the record in the DAO to update

  • updated_attrs (Hash)

    Attributes to be updated.

  • dao

    Data Access Object implements persistence without business logic.

  • factory

    Class whose `#create` method converts a DAO record to an entity.

Since:

  • 0.0.4



23
24
25
26
27
28
# File 'lib/repository/base/internals/record_updater.rb', line 23

def initialize(identifier:, updated_attrs:, dao:, factory:)
  @identifier = identifier
  @updated_attrs = updated_attrs
  @dao = dao
  @factory = factory
end

Instance Attribute Details

#daoObject (readonly, private)

Since:

  • 0.0.4



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

def dao
  @dao
end

#factoryObject (readonly, private)

Since:

  • 0.0.4



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

def factory
  @factory
end

#identifierObject (readonly, private)

Since:

  • 0.0.4



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

def identifier
  @identifier
end

#recordObject (readonly, private)

Since:

  • 0.0.4



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

def record
  @record
end

#updated_attrsObject (readonly, private)

Since:

  • 0.0.4



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

def updated_attrs
  @updated_attrs
end

Instance Method Details

#failed_resultObject (private)

:nodoc:

Since:

  • 0.0.4



43
44
45
# File 'lib/repository/base/internals/record_updater.rb', line 43

def failed_result # :nodoc:
  StoreResult::Failure.new record.errors
end

#successful_resultObject (private)

:nodoc:

Since:

  • 0.0.4



47
48
49
50
# File 'lib/repository/base/internals/record_updater.rb', line 47

def successful_result # :nodoc:
  entity = factory.create record
  StoreResult::Success.new entity
end

#updateRepository::Support::StoreResult

Command-pattern method to update a record in the persistence layer, based on the parameters sent to `#initialize`.

Returns:

  • (Repository::Support::StoreResult)

Since:

  • 0.0.4



33
34
35
36
37
# File 'lib/repository/base/internals/record_updater.rb', line 33

def update
  @record = dao.where(slug: identifier).first
  return failed_result unless @record.update(updated_attrs.to_h)
  successful_result
end