class Interactor::Contracts::Terms
The terms of a contract, either for promises or expectations
Public Class Methods
new(terms = Class.new(Dry::Validation::Contract))
click to toggle source
Instantiates a new set of terms
@example
terms = Interactor::Contracts::Terms.new
@api public @param [Dry::Validation::Contract] terms the terms to start with
# File lib/interactor/contracts/terms.rb, line 16 def initialize(terms = Class.new(Dry::Validation::Contract)) @terms = terms end
Public Instance Methods
add(&term)
click to toggle source
Add a new set of terms to the list of terms
@example
terms = Interactor::Contracts::Terms.new terms.add do required(:name).filled end
@api public @param [Block] term the term to add to the terms @return [void]
# 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)
click to toggle source
Validates the terms against a given context
@example
terms = Interactor::Contracts::Terms.new terms.add do required(:name).filled end terms.call(:name => "Bilbo Baggins")
@api public @param [#to_h] context the context to validate the terms against @return [Outcome]
# 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)
click to toggle source
Configures the underlying contracts within the terms
@api private @private @return [void]
# File lib/interactor/contracts/terms.rb, line 64 def config(&block) @terms.config.instance_exec(&block) end
Private Instance Methods
define_empty_schema()
click to toggle source
Defines a no-op schema as a base to extend upon
This prevents the raising of a `Dry::Validation::SchemaMissingError` exception.
@api private @return [void]
# File lib/interactor/contracts/terms.rb, line 77 def define_empty_schema @terms.params.nil? && @terms.params {} end