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.
Instance Attribute Summary collapse
-
#default ⇒ Numeric
readonly
starting value (default: 0).
-
#name ⇒ Symbol
readonly
accumulator identifier (e.g. :price).
-
#type ⇒ Symbol
readonly
one of Node::TYPES (typically :currency, :integer, :decimal).
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: 0) ⇒ Accumulator
constructor
A new instance of Accumulator.
-
#to_h ⇒ Hash{String => Object}
Serializes the accumulator to its wire format.
Constructor Details
#initialize(name:, type: :decimal, default: 0) ⇒ Accumulator
Returns a new instance of Accumulator.
26 27 28 29 30 31 |
# File 'lib/inquirex/accumulator.rb', line 26 def initialize(name:, type: :decimal, default: 0) @name = name.to_sym @type = type.to_sym @default = default freeze end |
Instance Attribute Details
#default ⇒ Numeric (readonly)
starting value (default: 0)
20 21 22 |
# File 'lib/inquirex/accumulator.rb', line 20 def default @default end |
#name ⇒ Symbol (readonly)
accumulator identifier (e.g. :price)
20 21 22 |
# File 'lib/inquirex/accumulator.rb', line 20 def name @name end |
#type ⇒ Symbol (readonly)
one of Node::TYPES (typically :currency, :integer, :decimal)
20 21 22 |
# File 'lib/inquirex/accumulator.rb', line 20 def type @type end |
Class Method Details
.from_h(name, hash) ⇒ Accumulator
Deserializes an Accumulator from its wire format.
46 47 48 49 50 51 52 |
# File 'lib/inquirex/accumulator.rb', line 46 def self.from_h(name, hash) new( name: name, type: hash["type"] || hash[:type] || :decimal, default: hash["default"] || hash[:default] || 0 ) end |
Instance Method Details
#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.
37 38 39 |
# File 'lib/inquirex/accumulator.rb', line 37 def to_h { "type" => @type.to_s, "default" => @default } end |