Class: Interactor::Contracts::Terms
- Inherits:
-
Object
- Object
- Interactor::Contracts::Terms
- Defined in:
- lib/interactor/contracts/terms.rb
Overview
The terms of a contract, either for promises or expectations
Instance Method Summary collapse
-
#add(&term) ⇒ void
Add a new set of terms to the list of terms.
-
#call(context) ⇒ Outcome
Validates the terms against a given context.
-
#config(&block) ⇒ void
private
Configures the underlying contracts within the terms.
-
#initialize(terms = Class.new(Dry::Validation::Contract)) ⇒ Terms
constructor
Instantiates a new set of terms.
Constructor Details
#initialize(terms = Class.new(Dry::Validation::Contract)) ⇒ Terms
Instantiates a new set of terms
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
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
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 |