module Vivify
Mixin that gives a Hash dot-notation accessors with autovivification.
Not applied globally — only extended onto the validated config hash and its nested children so the rest of the world is unaffected.
h = .extend(Vivify)
h.foo.bar.baz = 42
h # => foo: { bar: { baz: 42 } }
Definitions
def self.deep_vivify(obj)
Recursively symbolize keys and extend every nested Hash with Vivify.
Call this on the raw (string-keyed) hash that comes out of YAML.safe_load / JSON Schema validation before wrapping it in Config.
Implementation
def self.deep_vivify(obj)
case obj
when Hash
result = {}
obj.each { |k, v| result[k.to_s.to_sym] = deep_vivify(v) }
result.extend(Vivify)
when Array
obj.map { |v| deep_vivify(v) }
else
obj
end
end