Class: Interactor::Contracts::Contract

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Instantiates a new Contract with the given contraints

rubocop:disable Metrics/LineLength

Examples:

Interactor::Contracts::Contract.new


18
19
20
21
22
# File 'lib/interactor/contracts/contract.rb', line 18

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

Instance Attribute Details

#assurancesTerms (readonly)

The assurances the Contract will fulfill

Examples:

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


32
33
34
# File 'lib/interactor/contracts/contract.rb', line 32

def assurances
  @assurances
end

#expectationsTerms (readonly)

The expectations for arguments passed into the Interactor

Examples:

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


42
43
44
# File 'lib/interactor/contracts/contract.rb', line 42

def expectations
  @expectations
end

Instance Method Details

#add_assurance(&term) ⇒ void

This method returns an undefined value.

Adds an assurance to the Contract's set of assurances

Examples:

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


55
56
57
# File 'lib/interactor/contracts/contract.rb', line 55

def add_assurance(&term)
  assurances.add(&term)
end

#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


70
71
72
# File 'lib/interactor/contracts/contract.rb', line 70

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


85
86
87
# File 'lib/interactor/contracts/contract.rb', line 85

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

#consequencesArray<#call>

The consequences for the Contract

Examples:

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


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

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