Sha256: 7aa76d914cc4b13c9cb1a87faa343fa9f321c4a2fcb847f3de657a2f0073d7fd

Contents?: true

Size: 757 Bytes

Versions: 5

Compression:

Stored size: 757 Bytes

Contents

# frozen_string_literal: true

module Darthjee
  module CoreExt
    module Hash
      module Squasher
        class Builder
          attr_reader :key, :value

          def initialize(key, value)
            @value = value
            @key = key
          end

          def to_h
            if value.is_a? Hash
              value.squash.inject({}) do |hash, (k, v)|
                new_key = [key, k].join('.')
                hash.merge!(new_key => v)
              end
            else
              { key => value }
            end
          end
        end

        def self.squash(origin)
          origin.inject({}) do |hash, (key, value)|
            hash.merge!(Builder.new(key, value).to_h)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
darthjee-core_ext-1.7.4 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.7.3 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.7.2 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.7.1 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.7.0 lib/darthjee/core_ext/hash/squasher.rb