Module: LangExtract::Core::HashCoercion

Defined in:
lib/langextract/core/data.rb

Class Method Summary collapse

Class Method Details

.coerce_value(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/langextract/core/data.rb', line 18

def coerce_value(value)
  case value
  when Hash
    stringify_keys(value)
  when Array
    value.map { |entry| coerce_value(entry) }.freeze
  else
    value
  end
end

.read(hash, *keys) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/langextract/core/data.rb', line 29

def read(hash, *keys)
  keys.each do |key|
    return hash[key] if hash.key?(key)
    return hash[key.to_s] if hash.key?(key.to_s)
  end
  nil
end

.stringify_keys(hash) ⇒ Object



12
13
14
15
16
# File 'lib/langextract/core/data.rb', line 12

def stringify_keys(hash)
  (hash || {}).each_with_object({}) do |(key, value), result|
    result[key.to_s] = coerce_value(value)
  end
end