Class: Stannum::Contracts::Parameters::ArgumentsContract Private
- Inherits:
-
TupleContract
- Object
- Stannum::Constraints::Base
- Base
- Stannum::Contract
- TupleContract
- Stannum::Contracts::Parameters::ArgumentsContract
- 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
Instance Method Summary collapse
-
#add_argument_constraint(index, type, default: false, **options) ⇒ Stannum::Contracts::Parameters::ArgumentsContract
private
Adds an argument constraint to the contract.
-
#initialize(**options) ⇒ ArgumentsContract
constructor
private
A new instance of ArgumentsContract.
-
#set_variadic_constraint(constraint, as: nil) ⇒ self
private
Sets a constraint for the variadic arguments.
-
#set_variadic_item_constraint(item_type, as: nil) ⇒ self
private
Sets a constraint for the variadic argument items.
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.
17 18 19 |
# File 'lib/stannum/contracts/parameters/arguments_contract.rb', line 17 def initialize(**) super(allow_extra_items: false, **) 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.
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, **) index ||= next_index constraint = Stannum::Support::Coercion.type_constraint(type, **) add_index_constraint(index, constraint, default: !!default, **) 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.
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? [:allow_extra_items] = true variadic_constraint.receiver = constraint variadic_definition.[: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.
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 |