module Interactor::Contracts

Create a contract for your interactor that specifies what it expects as inputs.

Constants

ContractsError

Base error class used for all errors within the gem.

NotAnInteractor

Raised when trying to include Interactor::Contracts into a class that is not an Interactor.

VERSION

Public Class Methods

included(descendant) click to toggle source

Called when the module is included into another class or module

@api private @param [Class, Module] descendant the including class or module @return [void]

# File lib/interactor/contracts.rb, line 17
def self.included(descendant)
  unless descendant.include?(Interactor)
    raise NotAnInteractor, "#{descendant} does not include `Interactor'"
  end
  descendant.extend(DSL)
end

Private Instance Methods

contract() click to toggle source

The Contract to enforce on calls to the Interactor

@api private @return [Contract]

# File lib/interactor/contracts.rb, line 30
def contract
  self.class.contract
end
enforce_contracts(contracts) click to toggle source

Checks for a breach of contract and applies consequences for a breach

@api private @param [#call] contracts a callable object @return [void]

# File lib/interactor/contracts.rb, line 39
def enforce_contracts(contracts)
  return unless (outcome = contracts.call(context)).failure?

  contract.consequences.each do |handler|
    instance_exec(outcome.breaches, &handler)
  end
end