Module: YamlNormalizer::Ext::Namespaced
- Defined in:
- lib/yaml_normalizer/ext/namespaced.rb
Overview
YamlNormalizer::Ext::Namespaced extends instances of Hash to provide the additional public helper method namespaced. The approach of extending Hash instances avoids monkey-patching a Ruby Core class and using refinements.
Instance Method Summary collapse
-
#namespaced(namespace = [], tree = {}) ⇒ Hash
Transforms a tree-shaped Hash into a flat key-value pair Hash, separating tree levels with a dot.
Instance Method Details
#namespaced(namespace = [], tree = {}) ⇒ Hash
Transforms a tree-shaped Hash into a flat key-value pair Hash, separating tree levels with a dot. namespaced does not modify the instance of Hash it’s called on.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/yaml_normalizer/ext/namespaced.rb', line 23 def namespaced(namespace = [], tree = {}) each do |key, value| child_ns = namespace.dup << key if value.instance_of?(Hash) value.extend(Namespaced).namespaced child_ns, tree else tree[child_ns.join('.')] = value end end tree end |