Sha256: 3252f3d3c5a7f1c0aa06a799511c7495268aa0c67f2ef16201247b1aac069e31

Contents?: true

Size: 832 Bytes

Versions: 2

Compression:

Stored size: 832 Bytes

Contents

module Morfo
  module Tools
    class FlattenHashKeys
      attr_reader :input_hash

      def initialize(input_hash)
        @input_hash = input_hash.dup.freeze
      end

      def flatten
        input_hash.inject({}) do |result_hash, (key, value)|
          inner_hash = false
          if value.is_a?(Hash)
            inner_hash = true
            value.each do |inner_key, inner_value|
              if inner_value.is_a?(Hash)
                inner_hash = true
              end
              result_hash.merge!("#{key}.#{inner_key}".to_sym => inner_value)
            end
          else
            result_hash.merge!(key.to_sym => value)
          end

          if inner_hash
            FlattenHashKeys.new(result_hash).flatten
          else
            result_hash
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morfo-0.5.0 lib/morfo/tools.rb
morfo-0.4.0 lib/morfo/tools.rb