Class: Inquirex::Accumulator

Inherits:
Object
  • Object
show all
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.

Examples:

Declare a running price total and contribute to it from a step

Inquirex.define do
  accumulator :price, type: :currency, default: 0
  ask :dependents do
    type :integer
    accumulate :price, per_unit: 50
  end
end

Declare a transcript the engine fills in on its own

Inquirex.define do
  accumulator :transcript, type: :text
  say(:intro) { text "Here is how depreciation works." }
end
# engine.advance
# engine.text(:transcript)  # => "Here is how depreciation works."

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: :decimal, default: nil) ⇒ Accumulator

Returns a new instance of Accumulator.

Parameters:

  • name (Symbol, String)

    accumulator identifier

  • type (Symbol, String) (defaults to: :decimal)

    value type, one of Node::TYPES

  • default (Numeric, String, nil) (defaults to: nil)

    starting value before any contributions; nil selects the type's own zero ("" for :text, else 0)



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

#defaultNumeric, String (readonly)

starting value (0, or "" when :text)

Returns:

  • (Numeric, String)

    the current value of default



37
38
39
# File 'lib/inquirex/accumulator.rb', line 37

def default
  @default
end

#nameSymbol (readonly)

accumulator identifier (e.g. :price)

Returns:

  • (Symbol)

    the current value of name



37
38
39
# File 'lib/inquirex/accumulator.rb', line 37

def name
  @name
end

#typeSymbol (readonly)

one of Node::TYPES (typically :currency, :integer, :decimal, :text)

Returns:

  • (Symbol)

    the current value of type



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.

Parameters:

  • name (Symbol, String)

    accumulator identifier (the map key)

  • hash (Hash)

    type/default attributes (string or symbol keys)

Returns:



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.

Returns:

  • (Boolean)


59
60
61
# File 'lib/inquirex/accumulator.rb', line 59

def text?
  @type == TEXT_TYPE
end

#to_hHash{String => Object}

Serializes the accumulator to its wire format. The name is omitted — Definition#to_h keys the accumulators map by name.

Returns:

  • (Hash{String => Object})

    e.g. { "type" => "currency", "default" => 0 }



67
68
69
# File 'lib/inquirex/accumulator.rb', line 67

def to_h
  { "type" => @type.to_s, "default" => @default }
end