Class: EYAML::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/eyaml/util.rb

Class Method Summary collapse

Class Method Details

.pretty_yaml(some_hash) ⇒ Object



6
7
8
# File 'lib/eyaml/util.rb', line 6

def pretty_yaml(some_hash)
  some_hash.to_yaml.delete_prefix("---\n")
end

.with_deep_deundescored_keys(hash) ⇒ Object

This will look for any keys that starts with an underscore and duplicates that key-value pair but without the starting underscore. So “abab” will become “abab”, a: “abab” This so we can easilly access our unencrypted secrets without having to add an underscore



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/eyaml/util.rb', line 14

def with_deep_deundescored_keys(hash)
  hash.each_with_object({}) do |(key, value), total|
    value = with_deep_deundescored_keys(value) if value.is_a?(Hash)

    if key.start_with?("_")
      deunderscored_key = key[1..]
      # We don't want to have an underscored and de-underscored key with the same name, so raise. This could be a security issue
      raise KeyError, "De-underscored key '#{key[1..]}' already exists." if total.key?(deunderscored_key)

      total[deunderscored_key] = value unless total.key?(deunderscored_key)
    end

    total[key] = value
  end
end