Module: Inquirex::Engine::StateSerializer
- Defined in:
- lib/inquirex/engine/state_serializer.rb
Overview
Handles state serialization for Engine persistence (DB, session store, etc.). Normalizes string-keyed hashes (from JSON round-trips) to symbol-keyed hashes.
Constant Summary collapse
- SYMBOLIZERS =
{ current_step_id: ->(v) { v&.to_sym }, history: ->(v) { Array(v).map { |e| e&.to_sym } }, answers: ->(v) { symbolize_answers(v) }, totals: ->(v) { symbolize_answers(v) } }.freeze
Class Method Summary collapse
-
.symbolize_answers(answers) ⇒ Hash
Same map with symbol keys.
-
.symbolize_state(hash) ⇒ Hash
Normalizes a state hash so step ids and history entries are symbols.
Class Method Details
.symbolize_answers(answers) ⇒ Hash
Returns same map with symbol keys.
30 31 32 33 34 |
# File 'lib/inquirex/engine/state_serializer.rb', line 30 def self.symbolize_answers(answers) return {} unless answers.is_a?(Hash) answers.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } end |
.symbolize_state(hash) ⇒ Hash
Normalizes a state hash so step ids and history entries are symbols.
19 20 21 22 23 24 25 26 |
# File 'lib/inquirex/engine/state_serializer.rb', line 19 def self.symbolize_state(hash) return hash unless hash.is_a?(Hash) hash.each_with_object({}) do |(key, value), result| sym_key = key.to_sym result[sym_key] = SYMBOLIZERS.fetch(sym_key, ->(v) { v }).call(value) end end |