Class: Stannum::Contracts::Parameters::ArgumentsContract Private

Inherits:
TupleContract show all
Defined in:
lib/stannum/contracts/parameters/arguments_contract.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

An ArgumentsContract constrains the arguments given for a method.

Constant Summary collapse

UNDEFINED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Value used when arguments array does not have a value for the given index.

Object.new.freeze

Constants inherited from Stannum::Constraints::Base

Stannum::Constraints::Base::NEGATED_TYPE, Stannum::Constraints::Base::TYPE

Instance Attribute Summary

Attributes inherited from Stannum::Constraints::Base

#options

Instance Method Summary collapse

Methods inherited from TupleContract

#add_index_constraint, #allow_extra_items?, #with_options

Methods inherited from Stannum::Contract

#add_constraint, #add_property_constraint

Methods inherited from Base

#==, #add_constraint, #concat, #does_not_match?, #each_constraint, #each_pair, #errors_for, #match, #matches?, #negated_errors_for, #negated_match

Methods inherited from Stannum::Constraints::Base

#==, #clone, #does_not_match?, #dup, #errors_for, #match, #matches?, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

#initialize(**options) ⇒ ArgumentsContract

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.

Returns a new instance of ArgumentsContract.

Parameters:

  • options (Hash<Symbol, Object>)

    Configuration options for the contract. Defaults to an empty Hash.



17
18
19
# File 'lib/stannum/contracts/parameters/arguments_contract.rb', line 17

def initialize(**options)
  super(allow_extra_items: false, **options)
end

Instance Method Details

#add_argument_constraint(index, type, default: false, **options) ⇒ Stannum::Contracts::Parameters::ArgumentsContract

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.

Adds an argument constraint to the contract.

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.

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.

Parameters:

  • index (Integer, nil)

    The index of the argument. If not given, then the next argument will be constrained with the type.

  • type (Class, Module, Stannum::Constraints:Base)

    The expected type of the argument.

  • default (Boolean) (defaults to: false)

    If true, the argument has a default value, and the constraint will ignore arguments with no value at that index.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Returns:



45
46
47
48
49
50
51
52
# File 'lib/stannum/contracts/parameters/arguments_contract.rb', line 45

def add_argument_constraint(index, type, default: false, **options)
  index    ||= next_index
  constraint = Stannum::Support::Coercion.type_constraint(type, **options)

  add_index_constraint(index, constraint, default: !!default, **options)

  self
end

#set_variadic_constraint(constraint, as: nil) ⇒ self

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.

Sets a constraint for the variadic arguments.

The given constraint must match the variadic arguments array as a whole. To constraint each individual item, use #set_variadic_item_constraint.

Parameters:

  • constraint (Stannum::Constraints::Base)

    The constraint to add. The variadic arguments (an array) as a whole must match the given constraint.

  • as (Symbol) (defaults to: nil)

    A human-friendly reference for the additional arguments. Used when generating errors. Should be the same name used in the method definition.

Returns:

  • (self)

    the contract.

Raises:

  • (RuntimeError)

    if the variadic arguments constraint is already set.

See Also:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stannum/contracts/parameters/arguments_contract.rb', line 71

def set_variadic_constraint(constraint, as: nil)
  raise 'variadic arguments constraint is already set' if allow_extra_items?

  options[:allow_extra_items] = true

  variadic_constraint.receiver = constraint

  variadic_definition.options[:property_name] = as if as

  self
end

#set_variadic_item_constraint(item_type, as: nil) ⇒ self

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.

Sets a constraint for the variadic argument items.

The given type or constraint must individually match each item (if any) in the variadic arguments. To constrain the variadic arguments as a whole, use #set_variadic_constraint.

Parameters:

  • item_type (Stannum::Constraints::Base, Class, Module)

    The type or constraint to add. If the type is a Class or Module, then it is converted to a Stannum::Constraints::Type. Each item in the variadic arguments must match the given constraint.

  • as (Symbol) (defaults to: nil)

    A human-friendly reference for the additional arguments. Used when generating errors. Should be the same name used in the method definition.

Returns:

  • (self)

    the contract.

Raises:

  • (RuntimeError)

    if the variadic arguments constraint is already set.

See Also:



102
103
104
105
106
107
# File 'lib/stannum/contracts/parameters/arguments_contract.rb', line 102

def set_variadic_item_constraint(item_type, as: nil)
  type       = coerce_item_type(item_type)
  constraint = Stannum::Constraints::Types::ArrayType.new(item_type: type)

  set_variadic_constraint(constraint, as: as)
end