Class: Interactor::Contracts::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/interactor/contracts/contract.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promises: Terms.new, expectations: Terms.new, consequences: []) ⇒ Contract

Instantiates a new Contract with the given constraints

rubocop:disable Metrics/LineLength

Examples:

Interactor::Contracts::Contract.new

Parameters:

  • promises (Terms) (defaults to: Terms.new)

    the Contract’s promises

  • expectations (Terms) (defaults to: Terms.new)

    the Contract’s expectations

  • consequences (Array<#call>) (defaults to: [])

    the Contract’s consequences



20
21
22
23
24
# File 'lib/interactor/contracts/contract.rb', line 20

def initialize(promises: Terms.new, expectations: Terms.new, consequences: [])
  @promises = promises
  @consequences = consequences
  @expectations = expectations
end

Instance Attribute Details

#expectationsTerms (readonly)

The expectations for arguments passed into the Interactor

Examples:

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

Returns:

  • (Terms)

    the terms for the expectations



45
46
47
# File 'lib/interactor/contracts/contract.rb', line 45

def expectations
  @expectations
end

#promisesTerms (readonly)

The promises the Contract will fulfill

Examples:

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

Returns:

  • (Terms)

    the terms for the promises



35
36
37
# File 'lib/interactor/contracts/contract.rb', line 35

def promises
  @promises
end

Instance Method Details

#add_consequence(consequence) ⇒ void

This method returns an undefined value.

Adds a consequence to the Contract’s set of consequences

Examples:

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

Parameters:

  • consequence (#call)

    the consequence as a callable with arity 1



73
74
75
# File 'lib/interactor/contracts/contract.rb', line 73

def add_consequence(consequence)
  @consequences << consequence
end

#add_expectation(&term) ⇒ void

This method returns an undefined value.

Adds an expectation to the Contract’s set of expectations

Examples:

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

Parameters:

  • term (Block)

    the expectation as a block of arity 0



88
89
90
# File 'lib/interactor/contracts/contract.rb', line 88

def add_expectation(&term)
  expectations.add(&term)
end

#add_promise(&term) ⇒ void

This method returns an undefined value.

Adds an promise to the Contract’s set of promises

Examples:

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

Parameters:

  • term (Block)

    the promise as a block of arity 0



58
59
60
# File 'lib/interactor/contracts/contract.rb', line 58

def add_promise(&term)
  promises.add(&term)
end

#config(&block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Configures the underlying contracts for the validation schemata



97
98
99
100
# File 'lib/interactor/contracts/contract.rb', line 97

def config(&block)
  promises.config(&block)
  expectations.config(&block)
end

#consequencesArray<#call>

The consequences for the Contract

Examples:

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

Returns:

  • (Array<#call>)

    the consequences for the Contract



110
111
112
113
114
115
116
# File 'lib/interactor/contracts/contract.rb', line 110

def consequences
  if @consequences.empty?
    Array(default_consequence)
  else
    @consequences
  end
end