Stannum

A library for defining and validating data structures.

Class: Stannum::Contracts::ParametersContract::Builder

Parent Namespace
Stannum::Contracts::ParametersContract
Inherited Classes
Stannum::Contract::Builder > Stannum::Contracts::Base::Builder > Object
Defined In
lib/stannum/contracts/parameters_contract.rb

Table Of Contents

Overview

Builder class for defining item constraints for a ParametersContract.

This class should not be invoked directly. Instead, pass a block to the constructor for ParametersContract.

Back To Top

Instance Methods

#argument(name, type, index: nil, **options) => Stannum::Contracts::ParametersContract::Builder
#argument(name, index: nil, **options, &block) => Stannum::Contracts::ParametersContract::Builder

Adds an argument constraint to the contract.

If the index is specified, then the constraint will be added for the argument at the specified index. If the index is not given, then the constraint will be applied to the next unconstrained argument. For example, the first argument constraint will be added for the argument at index 0, the second constraint for the argument at index 1, and so on.

Overloads

#argument(name, type, index: nil, **options) => Stannum::Contracts::ParametersContract::Builder

Generates an argument constraint based on the given type. If the type is a constraint, then the given constraint will be copied with the given options and added for the argument at the index. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type and options and added for the argument.

Parameters
  • name (String, Symbol) — The name of the argument.
  • type (Class, Module, Stannum::Constraints:Base) — The expected type of the argument.
  • index (Integer, nil) — The index of the argument. If not given, then the next argument will be constrained with the type.
  • options (Hash<Symbol, Object>) — Configuration options for the constraint. Defaults to an empty Hash.
Returns
#argument(name, index: nil, **options, &block) => Stannum::Contracts::ParametersContract::Builder

Generates a new Stannum::Constraint using the block.

Parameters
  • name (String, Symbol) — The name of the argument.
  • index (Integer, nil) — The index of the argument. If not given, then the next argument will be constrained with the type.
  • options (Hash<Symbol, Object>) — Configuration options for the constraint. Defaults to an empty Hash.
Yields
  • The definition for the constraint. Each time #matches? is called for this constraint, the given object will be passed to this block and the result of the block will be returned.
Yield Parameters
  • actual (Object) — The object to check against the constraint.
Yield Returns
  • (true, false) — true if the given object matches the constraint, otherwise false.
Returns

#arguments(name, type) => Stannum::Contracts::ParametersContract::Builder

Sets the variadic arguments constraint for the contract.

If the parameters includes variadic (or “splatted”) arguments, then each item in the variadic arguments array must match the given type or constraint. If the type is a constraint, then the given constraint will be copied with the given options. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type.

Parameters

Returns

Raises

#block(present) => Stannum::Contracts::ParametersContract

Sets the block parameter constraint for the contract.

If the expected presence is true, a block must be given as part of the parameters. If the expected presence is false, a block must not be given. If the presence is a constraint, then the block must match the constraint.

Parameters

Returns

Raises

#keyword(name, type, **options) => Stannum::Contracts::ParametersContract::Builder
#keyword(name, **options, &block) => Stannum::Contracts::ParametersContract::Builder

Adds a keyword constraint to the contract.

Overloads

#keyword(name, type, **options) => Stannum::Contracts::ParametersContract::Builder

Generates a keyword constraint based on the given type. If the type is a constraint, then the given constraint will be copied with the given options and added for the given keyword. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type and options and added for the keyword.

Parameters
  • keyword (Symbol) — The keyword to constrain.
  • type (Class, Module, Stannum::Constraints:Base) — The expected type of the keyword.
  • options (Hash<Symbol, Object>) — Configuration options for the constraint. Defaults to an empty Hash.
Returns
#keyword(name, **options, &block) => Stannum::Contracts::ParametersContract::Builder

Generates a new Stannum::Constraint using the block.

Parameters
  • keyword (Symbol) — The keyword to constrain.
  • options (Hash<Symbol, Object>) — Configuration options for the constraint. Defaults to an empty Hash.
Yields
  • The definition for the constraint. Each time #matches? is called for this constraint, the given object will be passed to this block and the result of the block will be returned.
Yield Parameters
  • actual (Object) — The object to check against the constraint.
Yield Returns
  • (true, false) — true if the given object matches the constraint, otherwise false.
Returns

#keywords(name, type) => Stannum::Contracts::ParametersContract::Builder

Sets the variadic keywords constraint for the contract.

If the parameters includes variadic (or “splatted”) keywords, then each value in the variadic keywords hash must match the given type or constraint. If the type is a constraint, then the given constraint will be copied with the given options. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type.

Parameters

Returns

Raises

#property(property, constraint, **options) => Object
#property(**options) { |value| } => Object

Inherited From
Stannum::Contract::Builder

Defines a property constraint on the contract.

Overloads

#property(property, constraint, **options) => Object

Adds the given constraint to the contract for the property.

Parameters
  • property (String, Symbol, Array<String, Symbol>) — The property to constrain.
  • constraint (Stannum::Constraint::Base) — The constraint to add.
  • options (Hash<Symbol, Object>) — Options for the constraint.
#property(**options) { |value| } => Object

Creates a new Stannum::Constraint object with the given block, and adds that constraint to the contract for the property.

Back To Top


Back to Documentation | Reference | Stannum | Stannum::Contracts | Stannum::Contracts::ParametersContract