Class: Interactor::Contracts::Terms

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

Overview

The terms of a contract, either for assurances or expectations

Instance Method Summary collapse

Constructor Details

#initialize(terms = Class.new(Dry::Validation::Schema)) ⇒ Terms

Instantiates a new set of terms

Examples:

terms = Interactor::Contracts::Terms.new

15
16
17
# File 'lib/interactor/contracts/terms.rb', line 15

def initialize(terms = Class.new(Dry::Validation::Schema))
  @terms = terms
end

Instance Method Details

#add(&term) ⇒ void

This method returns an undefined value.

Add a new set of terms to the list of terms

Examples:

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

30
31
32
# File 'lib/interactor/contracts/terms.rb', line 30

def add(&term)
  @terms = Dry::Validation.Schema(@terms, {:build => false}, &term)
end

#call(context) ⇒ Outcome

Validates the terms against a given context

Examples:

terms = Interactor::Contracts::Terms.new
terms.add do
  required(:name).filled
end
terms.call(:name => "Bilbo Baggins")

46
47
48
# File 'lib/interactor/contracts/terms.rb', line 46

def call(context)
  Outcome.new(@terms.new.call(context.to_h))
end