Module: FlowEngine::Engine::StateSerializer
- Defined in:
- lib/flowengine/engine/state_serializer.rb
Overview
Handles state serialization and deserialization for Engine persistence. Normalizes string-keyed hashes (from JSON) to symbol-keyed hashes.
Constant Summary collapse
- SYMBOLIZERS =
{ current_step_id: ->(v) { v&.to_sym }, active_intake_step_id: ->(v) { v&.to_sym }, history: ->(v) { Array(v).map { |e| e&.to_sym } }, answers: ->(v) { symbolize_answers(v) }, conversation_history: ->(v) { symbolize_conversation_history(v) } }.freeze
Class Method Summary collapse
-
.symbolize_answers(answers) ⇒ Hash
Same map with symbol keys.
-
.symbolize_conversation_history(history) ⇒ Array<Hash>
Same entries with symbolized keys and role.
-
.symbolize_state(hash) ⇒ Object
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.
28 29 30 31 32 |
# File 'lib/flowengine/engine/state_serializer.rb', line 28 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_conversation_history(history) ⇒ Array<Hash>
Returns same entries with symbolized keys and role.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/flowengine/engine/state_serializer.rb', line 36 def self.symbolize_conversation_history(history) return [] unless history.is_a?(Array) history.map do |entry| next entry unless entry.is_a?(Hash) entry.each_with_object({}) do |(k, v), h| sym_key = k.to_sym h[sym_key] = sym_key == :role ? v.to_sym : v end end end |
.symbolize_state(hash) ⇒ Object
Normalizes a state hash so step ids and history entries are symbols.
17 18 19 20 21 22 23 24 |
# File 'lib/flowengine/engine/state_serializer.rb', line 17 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 |