Class: Camille::Types::Tuple
- Defined in:
- lib/camille/types/tuple.rb
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Attributes inherited from BasicType
Instance Method Summary collapse
- #check(value) ⇒ Object
-
#initialize(elements) ⇒ Tuple
constructor
A new instance of Tuple.
- #literal ⇒ Object
Methods inherited from BasicType
&, #&, #[], [], directly_instantiable?, inherited, instance, #transform, #|, |
Constructor Details
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
4 5 6 |
# File 'lib/camille/types/tuple.rb', line 4 def elements @elements end |
Instance Method Details
#check(value) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/camille/types/tuple.rb', line 11 def check value if value.is_a?(::Array) && value.size == @elements.size results = @elements.map.with_index do |type, index| [index, type.check(value[index])] end errors = results.map do |index, result| if result.type_error? ["tuple[#{index}]", result] else nil end end.compact if errors.empty? Camille::Checked.new(fingerprint, results.map{|_, checked| checked.value}) else Camille::TypeError.new(**errors.to_h) end else Camille::TypeError.new("Expected array of size #{@elements.size}, got #{value.inspect}.") end end |
#literal ⇒ Object
35 36 37 |
# File 'lib/camille/types/tuple.rb', line 35 def literal "[#{elements.map(&:literal).join(', ')}]" end |