Class: Plumb::RangeClass

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#&, #/, #>>, #absorb_input, #absorb_output, #accepted_type, #as_node, #build, #check, #defer, #fusable_step?, #fuse_with, #generate, #idempotent?, included, #input_type, #invalid, #invoke, #match, #metadata, #not, #output_type, #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(member_type = Types::Any) ⇒ RangeClass

Returns a new instance of RangeClass.



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

def initialize(member_type = Types::Any)
  @member_type = Composable.wrap(member_type)
  @children = [@member_type].freeze
  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#call(result) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plumb/range_class.rb', line 30

def call(result)
  return result.invalid!(errors: 'must be a Range') unless result.value.is_a?(::Range)

  range = result.value
  [range.begin, range.end].compact.each do |endpoint|
    r = @member_type.resolve(endpoint)
    return result.invalid!(errors: r.errors) unless r.valid?
  end

  result
end

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



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

def of(member_type)
  self.class.new(member_type)
end

#value_preserving?Boolean

#call only validates the Range's endpoints against the member type — it never coerces or rebuilds the Range — so a Range type always returns its input unchanged, regardless of member. This lets #| absorb a narrower branch (Range[Integer] | Range[1..10] -> Range[Integer]) and #>> drop a redundant re-check.

Returns:

  • (Boolean)


28
# File 'lib/plumb/range_class.rb', line 28

def value_preserving? = true