Sha256: 28bf7f5a8f97f1435e9706c353bf719061ada06f9f42f0f001030341efcfb5b5

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module MadFlatter
  # Flattens the provided Hash and assigns the results to the #hash_info
  # attribute. If a namespace is provided, the namespace is prepended to
  # the Hash key name.
  module HashInformable
    def load_hash_info(hash:, namespace: nil, dig: [], hash_info: {})
      hash.each do |key, value|
        if value.is_a? Hash
          load_hash_info(hash: value,
                         namespace: namespace,
                         dig: dig << key,
                         hash_info: hash_info)
          dig.pop
        else
          assign_hash_info(hash_info: hash_info,
                           key: key,
                           value: value,
                           namespace: namespace,
                           dig: dig)
        end

        next
      end

      hash_info
    end

    private

    def assign_hash_info(hash_info:, key:, value:, namespace:, dig:)
      hash_key = [namespace, *dig, key].compact.join('_').to_sym

      hash_info[hash_key] = {
        value: value,
        metadata: {
          key: key,
          dig: dig.dup
        }
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mad_flatter-1.0.1.pre.beta lib/mad_flatter/hash_informable.rb
mad_flatter-1.0.0.pre.beta lib/mad_flatter/hash_informable.rb