Class: Interactor::Contracts::Terms

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

Overview

The terms of a contract, either for promises or expectations

Instance Method Summary collapse

Constructor Details

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

Instantiates a new set of terms

Examples:

terms = Interactor::Contracts::Terms.new

Parameters:

  • terms (Dry::Validation::Contract) (defaults to: Class.new(Dry::Validation::Contract))

    the terms to start with



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

def initialize(terms = Class.new(Dry::Validation::Contract))
  @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

Parameters:

  • term (Block)

    the term to add to the terms



31
32
33
34
35
36
37
38
39
# File 'lib/interactor/contracts/terms.rb', line 31

def add(&term)
  @terms = Class.new(Dry::Validation::Contract).tap do |new_terms|
    new_terms.instance_variable_set(
      :@config,
      @terms.instance_variable_get(:@config).dup
    )
    new_terms.params(@terms.schema, &term)
  end
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")

Parameters:

  • context (#to_h)

    the context to validate the terms against

Returns:



53
54
55
56
57
# File 'lib/interactor/contracts/terms.rb', line 53

def call(context)
  define_empty_schema

  Outcome.new(@terms.new.call(context.to_h))
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 within the terms



64
65
66
# File 'lib/interactor/contracts/terms.rb', line 64

def config(&block)
  @terms.config.instance_exec(&block)
end