Module: SwarmSDK::Utils

Defined in:
lib/swarm_sdk/utils.rb

Overview

Shared utility methods for SwarmSDK

Class Method Summary collapse

Class Method Details

.hash_to_yaml(hash) ⇒ String

Convert hash to YAML string

Converts a Ruby hash to a YAML string. Useful for creating inline swarm definitions from hash configurations.

Examples:

config = { version: 2, swarm: { name: "Test" } }
Utils.hash_to_yaml(config)
# => "---\nversion: 2\nswarm:\n  name: Test\n"

Parameters:

  • hash (Hash)

    Hash to convert

Returns:

  • (String)

    YAML string representation



61
62
63
64
65
# File 'lib/swarm_sdk/utils.rb', line 61

def hash_to_yaml(hash)
  # Convert symbols to strings for valid YAML
  stringified = stringify_keys(hash)
  stringified.to_yaml
end

.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:

  • obj (Object)

    Object to stringify (Hash, Array, or other)

Returns:

  • (Object)

    Object with stringified keys (if applicable)



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

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:

  • obj (Object)

    Object to symbolize (Hash, Array, or other)

Returns:

  • (Object)

    Object with symbolized keys (if applicable)



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

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