Class: Plumb::TupleClass

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CovariantFusion

#fuse_with

Methods included from Composable

#&, #/, #>>, #absorb_input, #absorb_output, #as_node, #build, #check, #defer, #fusable_step?, #fuse_with, #generate, #idempotent?, included, #input_type, #invalid, #invoke, #match, #metadata, #not, #pipeline, #policy, resolve_operand, #static, #subtype_identity, #to_json_schema, #to_mermaid, #to_plumb_type, #to_s, #transform, #value, #where, #with, wrap, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(*children) ⇒ TupleClass

Returns a new instance of TupleClass.



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

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#accepted_typeObject

As a consumer, a Tuple accepts each position relaxed to what that position's type accepts (see HashClass#accepted_type).



33
# File 'lib/plumb/tuple_class.rb', line 33

def accepted_type = Plumb::Subtyping.map_children(self) { |c| Plumb::Subtyping.accepted_type(c) }

#call(result) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/plumb/tuple_class.rb', line 39

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: []



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

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

#output_typeObject

The value you GET after validating each position: every position type resolved to what it produces (mirror of #accepted_type).



37
# File 'lib/plumb/tuple_class.rb', line 37

def output_type = Plumb::Subtyping.map_children(self) { |c| Plumb::Subtyping.resolved_output(c) }

#value_preserving?Boolean

A Tuple validates each position through its type, so it preserves the value only when every position type does (a coercing position would change the tuple).

Returns:

  • (Boolean)


26
# File 'lib/plumb/tuple_class.rb', line 26

def value_preserving? = children.all? { |c| Plumb::Subtyping.value_preserving?(c) }

#with_children(children) ⇒ Object

Rebuild around new children (see Plumb::Subtyping.map_children).



29
# File 'lib/plumb/tuple_class.rb', line 29

def with_children(children) = of(*children)