class Interactor::Contracts::Contract

Contains the promises, expectations, and consequences of an interactor's contract.

Attributes

expectations[R]

The expectations for arguments passed into the Interactor

@example

contract = Interactor::Contracts::Contract.new
contract.expectations  #=> <#Interactor::Contracts::Terms>

@api semipublic @return [Terms] the terms for the expectations

promises[R]

The promises the Contract will fulfill

@example

contract = Interactor::Contracts::Contract.new
contract.promises  #=> <#Interactor::Contracts::Terms>

@api semipublic @return [Terms] the terms for the promises

Public Class Methods

new(promises: Terms.new, expectations: Terms.new, consequences: []) click to toggle source

Instantiates a new Contract with the given constraints

@example

Interactor::Contracts::Contract.new

@api semipublic @param [Terms] promises the Contract's promises @param [Terms] expectations the Contract's expectations @param [Array<#call>] consequences the Contract's consequences rubocop:disable Metrics/LineLength

# File lib/interactor/contracts/contract.rb, line 20
def initialize(promises: Terms.new, expectations: Terms.new, consequences: [])
  @promises = promises
  @consequences = consequences
  @expectations = expectations
end

Public Instance Methods

add_consequence(consequence) click to toggle source

Adds a consequence to the Contract's set of consequences

@example

contract = Interactor::Contracts::Contract.new
contract.add_expectation do |breaches|
  context[:message] = breaches.first.messages.first
end

@api semipublic @param [#call] consequence the consequence as a callable with arity 1 @return [void]

# File lib/interactor/contracts/contract.rb, line 73
def add_consequence(consequence)
  @consequences << consequence
end
add_expectation(&term) click to toggle source

Adds an expectation to the Contract's set of expectations

@example

contract = Interactor::Contracts::Contract.new
contract.add_expectation do
  required(:name).filled
end

@api semipublic @param [Block] term the expectation as a block of arity 0 @return [void]

# File lib/interactor/contracts/contract.rb, line 88
def add_expectation(&term)
  expectations.add(&term)
end
add_promise(&term) click to toggle source

Adds an promise to the Contract's set of promises

@example

contract = Interactor::Contracts::Contract.new
contract.add_promise do
  required(:name).filled
end

@api semipublic @param [Block] term the promise as a block of arity 0 @return [void]

# File lib/interactor/contracts/contract.rb, line 58
def add_promise(&term)
  promises.add(&term)
end
config(&block) click to toggle source

Configures the underlying contracts for the validation schemata

@api private @private @return [void]

# File lib/interactor/contracts/contract.rb, line 97
def config(&block)
  promises.config(&block)
  expectations.config(&block)
end
consequences() click to toggle source

The consequences for the Contract

@example

contract = Interactor::Contracts::Contract.new
contract.consequences  #=> [<#Proc>]

@api semipublic @return [Array<#call>] the consequences for the Contract

# File lib/interactor/contracts/contract.rb, line 110
def consequences
  if @consequences.empty?
    Array(default_consequence)
  else
    @consequences
  end
end

Private Instance Methods

default_consequence() click to toggle source

The default consequence of a breached contract

@api private @return [#call] the default consequence

# File lib/interactor/contracts/contract.rb, line 124
def default_consequence
  ->(breaches) { context.fail!(breaches) }
end