Class: Inquirex::Accumulation
- Inherits:
-
Object
- Object
- Inquirex::Accumulation
- Defined in:
- lib/inquirex/accumulator.rb
Overview
Per-step declaration of how a single answer contributes to one accumulator. A Node may carry zero or more Accumulation entries, one per target accumulator.
Supported shapes (exactly one must be set):
lookup: Hash of answer_value => amount (for :enum)
per_selection: Hash of option_value => amount (for :multi_enum, summed)
per_unit: Numeric rate multiplied by the answer (for numeric types)
flat: Numeric amount added if the step has any answer
Constant Summary collapse
- SHAPES =
%i[lookup per_selection per_unit flat].freeze
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
shape-specific data (Hash or Numeric).
-
#shape ⇒ Symbol
readonly
one of :lookup, :per_selection, :per_unit, :flat.
-
#target ⇒ Symbol
readonly
accumulator name to contribute to (e.g. :price).
Class Method Summary collapse
-
.from_h(target, hash) ⇒ Accumulation
Parses a single-key Hash like ‘{ “lookup” => … }` into an Accumulation.
Instance Method Summary collapse
-
#contribution(answer) ⇒ Numeric
Computes this accumulation’s contribution given the step’s answer.
-
#initialize(target:, shape:, payload:) ⇒ Accumulation
constructor
A new instance of Accumulation.
- #to_h ⇒ Object
Constructor Details
#initialize(target:, shape:, payload:) ⇒ Accumulation
Returns a new instance of Accumulation.
51 52 53 54 55 56 57 58 |
# File 'lib/inquirex/accumulator.rb', line 51 def initialize(target:, shape:, payload:) @target = target.to_sym @shape = shape.to_sym raise Errors::DefinitionError, "Unknown accumulator shape: #{shape.inspect}" unless SHAPES.include?(@shape) @payload = self.class.send(:normalize_payload, @shape, payload).freeze freeze end |
Instance Attribute Details
#payload ⇒ Object (readonly)
shape-specific data (Hash or Numeric)
46 47 48 |
# File 'lib/inquirex/accumulator.rb', line 46 def payload @payload end |
#shape ⇒ Symbol (readonly)
one of :lookup, :per_selection, :per_unit, :flat
46 47 48 |
# File 'lib/inquirex/accumulator.rb', line 46 def shape @shape end |
#target ⇒ Symbol (readonly)
accumulator name to contribute to (e.g. :price)
46 47 48 |
# File 'lib/inquirex/accumulator.rb', line 46 def target @target end |
Class Method Details
.from_h(target, hash) ⇒ Accumulation
Parses a single-key Hash like ‘{ “lookup” => … }` into an Accumulation.
85 86 87 88 89 90 91 |
# File 'lib/inquirex/accumulator.rb', line 85 def self.from_h(target, hash) pair = hash.to_a.find { |k, _| SHAPES.include?(k.to_sym) } raise Errors::SerializationError, "Invalid accumulation entry: #{hash.inspect}" unless pair shape_key, payload = pair new(target: target, shape: shape_key.to_sym, payload: payload) end |
Instance Method Details
#contribution(answer) ⇒ Numeric
Computes this accumulation’s contribution given the step’s answer. Returns 0 when the answer is absent or does not match the shape.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/inquirex/accumulator.rb', line 65 def contribution(answer) return 0 if answer.nil? case @shape when :lookup then lookup_amount(answer) when :per_selection then selection_amount(answer) when :per_unit then unit_amount(answer) when :flat then flat_amount(answer) end end |
#to_h ⇒ Object
76 77 78 |
# File 'lib/inquirex/accumulator.rb', line 76 def to_h { @shape.to_s => serialize_payload } end |