Class: Interactor::Contracts::Outcome

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/interactor/contracts/outcome.rb

Overview

The outcome of a Terms enforcement.

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Outcome

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.

Instantiates a new Outcome based on the results of a Terms enforcement

Parameters:

  • result (Dry::Validation::Result)


17
18
19
# File 'lib/interactor/contracts/outcome.rb', line 17

def initialize(result)
  @result = result
end

Instance Method Details

#breachesArray<Breach>

The list of breaches of the Terms

Examples:

terms = Interactor::Contract::Terms.new
terms.add do
  required(:name).filled
end

outcome = terms.call(:name => "Bilbo Baggins")
outcome.breaches  #=> []

Returns:

  • (Array<Breach>)

    the breaches of the Terms’ constraints



50
51
52
53
54
# File 'lib/interactor/contracts/outcome.rb', line 50

def breaches
  BreachSet.new(result.errors(full: true).to_h.map do |property, messages|
    Breach.new(property, messages)
  end)
end

#failure?Boolean

Checks whether the the Terms enforcement was a failure

Examples:

terms = Interactor::Contract::Terms.new
terms.add do
  required(:name).filled
end

outcome = terms.call(:name => "Bilbo Baggins")
outcome.failure?  #=> false.

Returns:

  • (Boolean)


69
70
71
# File 'lib/interactor/contracts/outcome.rb', line 69

def failure?
  !success?
end

#success?Boolean

Checks whether the the terms enforcement was a success

Examples:

terms = Interactor::Contract::Terms.new
terms.add do
  required(:name).filled
end

outcome = terms.call(:name => "Bilbo Baggins")
outcome.success?  #=> true

Returns:

  • (Boolean)


35
# File 'lib/interactor/contracts/outcome.rb', line 35

def_delegator :@result, :success?