Class: Inquirex::Accumulator
- Inherits:
-
Object
- Object
- Inquirex::Accumulator
- Defined in:
- lib/inquirex/accumulator.rb
Overview
Declares a named running total (e.g. :price, :complexity, :credit_score) that flows accumulate into as the user answers questions. Pure data, serializable to JSON, evaluated identically on Ruby and JS sides.
A :text accumulator is different in kind: nothing declares accumulate
into it, and no Accumulation ever targets it. The Engine appends to it
automatically as the user moves through the flow — every display step's
text, and every question with the answer given to it. That running
narrative is what an LLM summarize step reads, and it is the only way a
flow that mostly tells the user things (a help or explainer flow) has
anything to summarize at all: such a flow collects few answers, so the
answers hash alone says nothing about what the user was shown.
Constant Summary collapse
- TEXT_TYPE =
The accumulator type whose running value is appended prose rather than a running total. See the class docs for why it exists.
:text
Instance Attribute Summary collapse
-
#default ⇒ Numeric, String
readonly
starting value (0, or "" when :text).
-
#name ⇒ Symbol
readonly
accumulator identifier (e.g. :price).
-
#type ⇒ Symbol
readonly
one of Node::TYPES (typically :currency, :integer, :decimal, :text).
Class Method Summary collapse
-
.from_h(name, hash) ⇒ Accumulator
Deserializes an Accumulator from its wire format.
Instance Method Summary collapse
-
#initialize(name:, type: :decimal, default: nil) ⇒ Accumulator
constructor
A new instance of Accumulator.
-
#text? ⇒ Boolean
Whether this accumulator accumulates prose rather than a running total.
-
#to_h ⇒ Hash{String => Object}
Serializes the accumulator to its wire format.
Constructor Details
#initialize(name:, type: :decimal, default: nil) ⇒ Accumulator
Returns a new instance of Accumulator.
48 49 50 51 52 53 |
# File 'lib/inquirex/accumulator.rb', line 48 def initialize(name:, type: :decimal, default: nil) @name = name.to_sym @type = type.to_sym @default = default.nil? ? zero_value : default freeze end |
Instance Attribute Details
#default ⇒ Numeric, String (readonly)
starting value (0, or "" when :text)
37 38 39 |
# File 'lib/inquirex/accumulator.rb', line 37 def default @default end |
#name ⇒ Symbol (readonly)
accumulator identifier (e.g. :price)
37 38 39 |
# File 'lib/inquirex/accumulator.rb', line 37 def name @name end |
#type ⇒ Symbol (readonly)
one of Node::TYPES (typically :currency, :integer, :decimal, :text)
37 38 39 |
# File 'lib/inquirex/accumulator.rb', line 37 def type @type end |
Class Method Details
.from_h(name, hash) ⇒ Accumulator
Deserializes an Accumulator from its wire format.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/inquirex/accumulator.rb', line 76 def self.from_h(name, hash) # `fetch`, not `||` — a text accumulator's serialized default is "", # which `||` would discard in favour of the numeric zero. default = hash.fetch("default") { hash.fetch(:default, nil) } new( name: name, type: hash["type"] || hash[:type] || :decimal, default: default ) end |
Instance Method Details
#text? ⇒ Boolean
Whether this accumulator accumulates prose rather than a running total. Text accumulators are filled by the Engine, never by an Accumulation.
59 60 61 |
# File 'lib/inquirex/accumulator.rb', line 59 def text? @type == TEXT_TYPE end |
#to_h ⇒ Hash{String => Object}
Serializes the accumulator to its wire format. The name is omitted — Definition#to_h keys the accumulators map by name.
67 68 69 |
# File 'lib/inquirex/accumulator.rb', line 67 def to_h { "type" => @type.to_s, "default" => @default } end |