Module: Hanami::Interactor

Defined in:
gems/gems/hanami-utils-1.3.3/lib/hanami/interactor.rb,
gems/gems/hanami-utils-1.3.8/lib/hanami/interactor.rb

Overview

Hanami Interactor

Since:

  • 0.3.5

Defined Under Namespace

Modules: Interface, LegacyInterface Classes: Result

Instance Method Summary collapse

Instance Method Details

#error(message) ⇒ Object (private)

Logs an error without interrupting the flow.

When used, the returned result won’t be successful.

Examples:

require 'hanami/interactor'

class CreateRecord
  include Hanami::Interactor
  expose :logger

  def initialize
    @logger = []
  end

  def call
    prepare_data!
    persist!
    sync!
  end

  private
  def prepare_data!
    @logger << __method__
    error "Prepare data error"
  end

  def persist!
    @logger << __method__
    error "Persist error"
  end

  def sync!
    @logger << __method__
  end
end

result = CreateRecord.new.call
result.successful? # => false

result.errors # => ["Prepare data error", "Persist error"]
result.logger # => [:prepare_data!, :persist!, :sync!]

Parameters:

  • message (String)

    the error message

Returns:

  • false

See Also:

Since:

  • 0.3.5

def error(message)
  @__result.add_error message
  false
end

#error!(message) ⇒ Object (private)

Logs an error and interrupts the flow.

When used, the returned result won’t be successful.

Examples:

require 'hanami/interactor'

class CreateRecord
  include Hanami::Interactor
  expose :logger

  def initialize
    @logger = []
  end

  def call
    prepare_data!
    persist!
    sync!
  end

  private
  def prepare_data!
    @logger << __method__
    error "Prepare data error"
  end

  def persist!
    @logger << __method__
    error! "Persist error"
  end

  # THIS WILL NEVER BE INVOKED BECAUSE WE USE #error! IN #persist!
  def sync!
    @logger << __method__
  end
end

result = CreateRecord.new.call
result.successful? # => false

result.errors # => ["Prepare data error", "Persist error"]
result.logger # => [:prepare_data!, :persist!]

Parameters:

  • message (String)

    the error message

See Also:

Since:

  • 0.3.5

def error!(message)
  error(message)
  fail!
end

#fail!Object (private)

Fails and interrupts the current flow.

Examples:

require 'hanami/interactor'

class CreateEmailTest
  include Hanami::Interactor

  def initialize(params)
    @params     = params
  end

  def call
    persist_email_test!
    capture_screenshot!
  end

  private
  def persist_email_test!
    @email_test = EmailTestRepository.new.create(@params)
  end

  # IF THIS RAISES AN EXCEPTION WE FORCE A FAILURE
  def capture_screenshot!
    Screenshot.new(@email_test).capture!
  rescue
    fail!
  end
end

result = CreateEmailTest.new(account_id: 1).call
result.successful? # => false

Since:

  • 0.3.5

def fail!
  @__result.fail!
  throw :fail
end

#valid?TrueClass, FalseClass (private)

Checks if proceed with #call invocation. By default it returns true.

Developers can override it.

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.3.5

def valid?(*)
  true
end