Class: Plumb::TupleClass

Inherits:
Object
  • Object
show all
Includes:
Composable
Defined in:
lib/plumb/tuple_class.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#>>, #as_node, #build, #check, #defer, #generate, included, #invalid, #invoke, #match, #metadata, #not, #pipeline, #policy, #static, #to_json_schema, #to_s, #transform, #value, wrap, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(*children) ⇒ TupleClass

Returns a new instance of TupleClass.



11
12
13
14
# File 'lib/plumb/tuple_class.rb', line 11

def initialize(*children)
  @children = children.map { |t| Composable.wrap(t) }.freeze
  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/plumb/tuple_class.rb', line 9

def children
  @children
end

Instance Method Details

#call(result) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plumb/tuple_class.rb', line 22

def call(result)
  return result.invalid(errors: 'must be an Array') unless result.value.is_a?(::Array)
  return result.invalid(errors: 'must have the same size') unless result.value.size == @children.size

  errors = {}
  values = @children.map.with_index do |type, idx|
    val = result.value[idx]
    r = type.resolve(val)
    errors[idx] = ["expected #{type.inspect}, got #{val.inspect}", r.errors].flatten unless r.valid?
    r.value
  end

  return result.valid(values) unless errors.any?

  result.invalid(errors:)
end

#of(*types) ⇒ Object Also known as: []



16
17
18
# File 'lib/plumb/tuple_class.rb', line 16

def of(*types)
  self.class.new(*types)
end