Module: Hanami::Interactor::Interface

Defined in:
lib/hanami/interactor.rb

Overview

Interactor interface

Since:

  • 1.1.0

Instance Method Summary collapse

Instance Method Details

#call(*args, **kwargs) ⇒ Hanami::Interactor::Result

Triggers the operation and return a result.

All the exposed instance variables will be available in the result.

ATTENTION: This must be implemented by the including class.

Examples:

Expose instance variables in result payload

require 'hanami/interactor'

class Signup
  include Hanami::Interactor
  expose :user, :params

  def call(params)
    @params = params
    @foo = 'bar'
    @user = UserRepository.new.persist(User.new(params))
  end
end

result = Signup.new(name: 'Luca').call
result.failure? # => false
result.successful? # => true

result.user   # => #<User:0x007fa311105778 @id=1 @name="Luca">
result.params # => { :name=>"Luca" }
result.foo    # => raises NoMethodError

Failed precondition

require 'hanami/interactor'

class Signup
  include Hanami::Interactor
  expose :user

  # THIS WON'T BE INVOKED BECAUSE #valid? WILL RETURN false
  def call(params)
    @user = User.new(params)
    @user = UserRepository.new.persist(@user)
  end

  private
  def valid?(params)
    params.valid?
  end
end

result = Signup.new.call(name: nil)
result.successful? # => false
result.failure? # => true

result.user   # => nil

Bad usage

require 'hanami/interactor'

class Signup
  include Hanami::Interactor

  # Method #call is not defined
end

Signup.new.call # => NoMethodError

Returns:

Raises:

  • (NoMethodError)

    if this isn’t implemented by the including class.

Since:

  • 1.1.0



358
359
360
361
# File 'lib/hanami/interactor.rb', line 358

def call(*args, **kwargs)
  @__result = ::Hanami::Interactor::Result.new
  _call(*args, **kwargs) { super }
end