class Repository::Base::Internals::RecordSaver

Reports on the success or failure of saving a *DAO record*, using the `Repository::Support::StoreResult` instance returned from `#result`. @since 0.0.1

Attributes

factory[R]
record[R]

Public Class Methods

new(record:, factory:) click to toggle source

Sets instance variable(s) on a new `RecordSaver` instanace. @param record DAO record to attempt to save.

# File lib/repository/base/internals/record_saver.rb, line 19
def initialize(record:, factory:)
  @record = record
  @factory = factory
end

Public Instance Methods

result() click to toggle source

Command-pattern method returning indication of success or failure of attempt to save record. @return Repository::Support::StoreResult @see failed_result @see successful_result

# File lib/repository/base/internals/record_saver.rb, line 29
def result
  if record.save
    successful_result
  else
    failed_result
  end
end

Private Instance Methods

error_hashes() click to toggle source

Represent error data sourced from an `ActiveModel::Errors` object as an Array of `{field: 'field', message: 'message'}` hashes.

# File lib/repository/base/internals/record_saver.rb, line 43
def error_hashes
  ErrorFactory.create record.errors
end
failed_result() click to toggle source
# File lib/repository/base/internals/record_saver.rb, line 47
def failed_result
  StoreResult::Failure.new error_hashes
end
successful_result() click to toggle source
# File lib/repository/base/internals/record_saver.rb, line 51
def successful_result
  entity = factory.create record
  StoreResult::Success.new entity
end