Module: Promptcraft::Helpers

Included in:
Command::RechatConversationCommand, Conversation, Conversation
Defined in:
lib/promptcraft/helpers.rb

Instance Method Summary collapse

Instance Method Details

#deep_stringify_keys(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/promptcraft/helpers.rb', line 15

def deep_stringify_keys(value)
  case value
  when Hash
    value.map { |k, v| [k.to_s, deep_stringify_keys(v)] }.to_h
  when Array
    value.map { |v| deep_stringify_keys(v) }
  else
    value
  end
end

#deep_symbolize_keys(value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/promptcraft/helpers.rb', line 2

def deep_symbolize_keys(value)
  case value
  when Hash
    value.each_with_object({}) do |(key, v), result|
      result[key.to_sym] = deep_symbolize_keys(v)  # Convert keys to symbols and recursively handle values
    end
  when Array
    value.map { |v| deep_symbolize_keys(v) }  # Apply symbolization to each element in the array
  else
    value  # Return the value as is if it is neither a hash nor an array
  end
end