Module: SwarmSDK::Utils
- Defined in:
- lib/swarm_sdk/utils.rb
Overview
Shared utility methods for SwarmSDK
Class Method Summary collapse
-
.hash_to_yaml(hash) ⇒ String
Convert hash to YAML string.
-
.stringify_keys(obj) ⇒ Object
Recursively convert all hash keys to strings.
-
.symbolize_keys(obj) ⇒ Object
Recursively convert all hash keys to symbols.
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.
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.
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.
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 |