Module: Hanami::Interactor::Interface

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

Interactor interface

Since:

  • 1.1.0

Instance Method Summary collapse

Instance Method Details

#call(*args) ⇒ 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

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

#validate!(*args) ⇒ Object (private)

Since:

  • 1.1.0

def validate!(*args)
  fail! unless valid?(*args)
end