Module: Hanami::Interactor

Defined in:
gems/gems/hanami-utils-1.3.0/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)

Log 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)

Log an error AND 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

  # 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)

Fail and interrupt 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)

Check if proceed with #call invokation. 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