Module: Axn::Internal::GlobalIdSerialization

Defined in:
lib/axn/internal/global_id_serialization.rb

Overview

Utilities for serializing/deserializing objects with GlobalID support. Used by async adapters to convert ActiveRecord objects to GlobalID strings for job serialization, and back to objects when the job runs.

Constant Summary collapse

GLOBAL_ID_SUFFIX =
"_as_global_id"

Class Method Summary collapse

Class Method Details

.deserialize(params) ⇒ Hash

Deserialize a hash from background job processing:

  • Convert GlobalID strings back to objects

  • Symbolize keys for use with kwargs

Parameters:

  • params (Hash)

    The serialized parameters

Returns:

  • (Hash)

    Deserialized hash with symbol keys and resolved objects



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/axn/internal/global_id_serialization.rb', line 37

def deserialize(params)
  return {} if params.nil? || params.empty?

  params.each_with_object({}) do |(key, value), hash|
    if key.end_with?(GLOBAL_ID_SUFFIX)
      original_key = key.delete_suffix(GLOBAL_ID_SUFFIX).to_sym
      hash[original_key] = GlobalID::Locator.locate(value)
    else
      hash[key.to_sym] = value
    end
  end
end

.serialize(params) ⇒ Hash

Serialize a hash for background job processing:

  • Convert GlobalID-able objects (e.g., ActiveRecord models) to GlobalID strings

  • Stringify keys for JSON compatibility

Parameters:

  • params (Hash)

    The parameters to serialize

Returns:

  • (Hash)

    Serialized hash with string keys and GlobalID strings



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/axn/internal/global_id_serialization.rb', line 18

def serialize(params)
  return {} if params.nil? || params.empty?

  params.each_with_object({}) do |(key, value), hash|
    string_key = key.to_s
    if value.respond_to?(:to_global_id)
      hash["#{string_key}#{GLOBAL_ID_SUFFIX}"] = value.to_global_id.to_s
    else
      hash[string_key] = value
    end
  end
end