Class: Plumb::ValueClass
- Inherits:
-
Object
- Object
- Plumb::ValueClass
- Includes:
- Composable
- Defined in:
- lib/plumb/value_class.rb
Overview
A single literal value, matched by ==.
Unlike the other bare matchers — a Constraint with no base, an
AttributeValueMatch — this does NOT report Types::Any as its #input_type.
Those opt out of #>>'s subtype check because they narrow arbitrary input and
have no declared domain to check against; a literal's domain is a known
singleton, so it keeps the default (self) and accepts only its own value.
That is what makes Value['a'] >> Value['b'] raise rather than build a chain
no value can satisfy. Narrowing a wider type down to a literal is spelled
#[] (Types::String['a']), which builds the refinement directly.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
Instance Method Summary collapse
- #[](value) ⇒ Object
- #call(result) ⇒ Object
-
#initialize(value = Undefined) ⇒ ValueClass
constructor
A new instance of ValueClass.
-
#value_preserving? ⇒ Boolean
Matches a specific value and passes it through unchanged.
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
Constructor Details
#initialize(value = Undefined) ⇒ ValueClass
Returns a new instance of ValueClass.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/plumb/value_class.rb', line 21 def initialize(value = Undefined) @value = value # Pre-build the failure message once (the object is frozen and @value is # fixed), so a miss on the hot path — eg. every non-matching branch of a # `Value[a] | Value[b]` enum union — doesn't allocate a fresh String. # Frozen because it's shared across every failing call (and across threads # under Types::Array#concurrent) and only ever read, never mutated. @error = "Must be equal to #{value}".freeze @children = [value].freeze freeze end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
19 20 21 |
# File 'lib/plumb/value_class.rb', line 19 def children @children end |
Instance Method Details
#[](value) ⇒ Object
33 |
# File 'lib/plumb/value_class.rb', line 33 def [](value) = self.class.new(value) |
#call(result) ⇒ Object
38 39 40 |
# File 'lib/plumb/value_class.rb', line 38 def call(result) @value == result.value ? result : result.invalid!(errors: @error) end |
#value_preserving? ⇒ Boolean
Matches a specific value and passes it through unchanged.
36 |
# File 'lib/plumb/value_class.rb', line 36 def value_preserving? = true |