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
Instance Method Summary collapse
-
#initialize(name:, type: :decimal, default: 0) ⇒ Accumulator
constructor
A new instance of Accumulator.
- #to_h ⇒ Object
Constructor Details
#initialize(name:, type: :decimal, default: 0) ⇒ Accumulator
Returns a new instance of Accumulator.
14 15 16 17 18 19 |
# File 'lib/inquirex/accumulator.rb', line 14 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)
11 12 13 |
# File 'lib/inquirex/accumulator.rb', line 11 def default @default end |
#name ⇒ Symbol (readonly)
accumulator identifier (e.g. :price)
11 12 13 |
# File 'lib/inquirex/accumulator.rb', line 11 def name @name end |
#type ⇒ Symbol (readonly)
one of Node::TYPES (typically :currency, :integer, :decimal)
11 12 13 |
# File 'lib/inquirex/accumulator.rb', line 11 def type @type end |
Class Method Details
.from_h(name, hash) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/inquirex/accumulator.rb', line 25 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 ⇒ Object
21 22 23 |
# File 'lib/inquirex/accumulator.rb', line 21 def to_h { "type" => @type.to_s, "default" => @default } end |