Class: Contracts::Constraints::Tuple
- Defined in:
- lib/contracts/structured.rb
Overview
Matches a fixed-length Array whose positions use independent constraints.
Instance Method Summary collapse
- #description ⇒ Object
-
#initialize(*items) ⇒ Tuple
constructor
A new instance of Tuple.
- #matches?(value) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(*items) ⇒ Tuple
Returns a new instance of Tuple.
9 10 11 |
# File 'lib/contracts/structured.rb', line 9 def initialize(*items) @items = items.map { |item| Constraints.coerce(item) }.freeze end |
Instance Method Details
#description ⇒ Object
18 19 20 |
# File 'lib/contracts/structured.rb', line 18 def description "Tuple<#{@items.map(&:description).join(', ')}>" end |
#matches?(value) ⇒ Boolean
13 14 15 16 |
# File 'lib/contracts/structured.rb', line 13 def matches?(value) value.is_a?(Array) && value.length == @items.length && @items.each_with_index.all? { |constraint, index| constraint.matches?(value[index]) } end |
#to_h ⇒ Object
22 23 24 |
# File 'lib/contracts/structured.rb', line 22 def to_h super.merge(items: @items.map(&:description)) end |