Module: SwarmMemory::Utils

Defined in:
lib/swarm_memory/utils.rb

Overview

Shared utility methods for SwarmMemory

Class Method Summary collapse

Class Method Details

.stringify_keys(obj) ⇒ Object

Recursively convert all hash keys to strings

Handles nested hashes and arrays containing hashes.

Examples:

Utils.stringify_keys({ name: "test", config: { key: "value" } })
# => { "name" => "test", "config" => { "key" => "value" } }

Parameters:

  • Object to stringify (Hash, Array, or other)

Returns:

  • Object with stringified keys (if applicable)



17
18
19
20
21
22
23
24
25
26
# File 'lib/swarm_memory/utils.rb', line 17

def stringify_keys(obj)
  case obj
  when Hash
    obj.transform_keys(&:to_s).transform_values { |v| stringify_keys(v) }
  when Array
    obj.map { |item| stringify_keys(item) }
  else
    obj
  end
end

.symbolize_keys(obj) ⇒ Object

Recursively convert all hash keys to symbols

Handles nested hashes and arrays containing hashes.

Examples:

Utils.symbolize_keys({ "name" => "test", "config" => { "key" => "value" } })
# => { name: "test", config: { key: "value" } }

Parameters:

  • Object to symbolize (Hash, Array, or other)

Returns:

  • Object with symbolized keys (if applicable)



38
39
40
41
42
43
44
45
46
47
# File 'lib/swarm_memory/utils.rb', line 38

def symbolize_keys(obj)
  case obj
  when Hash
    obj.transform_keys(&:to_sym).transform_values { |v| symbolize_keys(v) }
  when Array
    obj.map { |item| symbolize_keys(item) }
  else
    obj
  end
end