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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#defaultNumeric (readonly)

starting value (default: 0)

Returns:

  • (Numeric)

    the current value of default



11
12
13
# File 'lib/inquirex/accumulator.rb', line 11

def default
  @default
end

#nameSymbol (readonly)

accumulator identifier (e.g. :price)

Returns:

  • (Symbol)

    the current value of name



11
12
13
# File 'lib/inquirex/accumulator.rb', line 11

def name
  @name
end

#typeSymbol (readonly)

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

Returns:

  • (Symbol)

    the current value of type



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_hObject



21
22
23
# File 'lib/inquirex/accumulator.rb', line 21

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