Class: Stannum::Contracts::ArrayContract
- Inherits:
-
TupleContract
- Object
- Stannum::Constraints::Base
- Base
- Stannum::Contract
- TupleContract
- Stannum::Contracts::ArrayContract
- Defined in:
- lib/stannum/contracts/array_contract.rb
Overview
An ArrayContract defines constraints for an Array and its items.
In order to match an ArrayContract, the object must be an instance of Array, and the items in the array at each index must match the item constraint defined for that index. If the :item_type option is set, each item must match that type or constraint. Finally, unless the :allow_extra_items option is set to true, the object must not have any extra items.
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
-
#initialize(allow_extra_items: false, item_type: nil, **options, &block) ⇒ ArrayContract
constructor
A new instance of ArrayContract.
-
#item_type ⇒ Stannum::Constraints::Base?
The expected type for the items in the array.
- #with_options(**options) ⇒ Object
Methods inherited from TupleContract
#add_index_constraint, #allow_extra_items?
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, item_type: nil, **options, &block) ⇒ ArrayContract
Returns a new instance of ArrayContract.
77 78 79 |
# File 'lib/stannum/contracts/array_contract.rb', line 77 def initialize(allow_extra_items: false, item_type: nil, **, &block) super end |
Instance Method Details
#item_type ⇒ Stannum::Constraints::Base?
Returns the expected type for the items in the array.
83 84 85 |
# File 'lib/stannum/contracts/array_contract.rb', line 83 def item_type [:item_type] end |
#with_options(**options) ⇒ Object
88 89 90 91 92 |
# File 'lib/stannum/contracts/array_contract.rb', line 88 def (**) return super unless .key?(:item_type) raise ArgumentError, "can't change option :item_type" end |