Class: Stannum::Contracts::TupleContract
- Inherits:
-
Stannum::Contract
- Object
- Stannum::Constraints::Base
- Base
- Stannum::Contract
- Stannum::Contracts::TupleContract
- Defined in:
- lib/stannum/contracts/tuple_contract.rb
Overview
A TupleContract defines constraints for an ordered, indexed object.
In order to match a TupleContract, the object must respond to the :[], :each, and :size methods, and the items in the object at each index must match the item constraint defined for that index. Finally, unless the :allow_extra_items option is set to true, the object must not have any extra items.
Direct Known Subclasses
Defined Under Namespace
Classes: Builder
Constant Summary
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_index_constraint(index, constraint, sanity: false, **options) ⇒ self
Adds an index constraint to the contract.
-
#allow_extra_items? ⇒ true, false
If false, then a tuple with extra items after the last expected item will not match the contract.
-
#initialize(allow_extra_items: false, **options, &block) ⇒ TupleContract
constructor
A new instance of TupleContract.
- #with_options(**options) ⇒ Object
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
Constructor Details
#initialize(allow_extra_items: false, **options, &block) ⇒ TupleContract
Returns a new instance of TupleContract.
119 120 121 |
# File 'lib/stannum/contracts/tuple_contract.rb', line 119 def initialize(allow_extra_items: false, **, &block) super(allow_extra_items: allow_extra_items, **, &block) end |
Instance Method Details
#add_index_constraint(index, constraint, sanity: false, **options) ⇒ self
Adds an index constraint to the contract.
When the contract is called, the contract will find the value of the object at the given index.
140 141 142 143 144 145 146 147 148 |
# File 'lib/stannum/contracts/tuple_contract.rb', line 140 def add_index_constraint(index, constraint, sanity: false, **) add_constraint( constraint, property: index, property_type: :index, sanity: sanity, ** ) end |
#allow_extra_items? ⇒ true, false
Returns If false, then a tuple with extra items after the last expected item will not match the contract.
152 153 154 |
# File 'lib/stannum/contracts/tuple_contract.rb', line 152 def allow_extra_items? !![:allow_extra_items] end |
#with_options(**options) ⇒ Object
157 158 159 160 161 |
# File 'lib/stannum/contracts/tuple_contract.rb', line 157 def (**) return super unless .key?(:allow_extra_items) raise ArgumentError, "can't change option :allow_extra_items" end |