Module: Hanami::Interactor::LegacyInterface

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 legacy interface

Since:

  • 0.3.5

Instance Method Summary collapse

Instance Method Details

#callHanami::Interactor::Result

Triggers the operation and return a result.

All the 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 initialize(params)
    @params = params
    @foo    = 'bar'
  end

  def call
    @user = UserRepository.new.create(@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

  def initialize(params)
    @params = params
  end

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

  private
  def valid?
    @params.valid?
  end
end

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

result.user   # => #<User:0x007fa311105778 @id=nil @name="Luca">

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:

  • 0.3.5

def call
  _call { super }
end

#initialize(*args) ⇒ Hanami::Interactor

Initialize an interactor

It accepts arbitrary number of arguments. Developers can override it.

Examples:

Override #initialize

require 'hanami/interactor'

class UpdateProfile
  include Hanami::Interactor

  def initialize(user, params)
    @user   = user
    @params = params
  end

  def call
    # ...
  end
end

Parameters:

  • args (Array<Object>)

    arbitrary number of arguments

Returns:

Since:

  • 0.3.5

def initialize(*args)
  super
ensure
  @__result = ::Hanami::Interactor::Result.new
end

#validate!Object (private)

Since:

  • 0.3.5

def validate!
  fail! unless valid?
end