Sha256: 35c32bae2c30e3bf5a0e6d7adb8c5207483de1e97fac6850b43e405ff294da57

Contents?: true

Size: 1.03 KB

Versions: 11

Compression:

Stored size: 1.03 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

11 entries across 11 versions & 1 rubygems

Version Path
mad_flatter-3.0.8 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.7 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.6 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.5 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.4 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.3 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.2 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.1 lib/mad_flatter/hash_informable.rb
mad_flatter-3.0.0 lib/mad_flatter/hash_informable.rb
mad_flatter-2.1.0 lib/mad_flatter/hash_informable.rb
mad_flatter-2.0.0 lib/mad_flatter/hash_informable.rb