Sha256: 15af719a11766fb8046c1a3540420ad464c94e201ff1047df2b56512df1f9041

Contents?: true

Size: 609 Bytes

Versions: 3

Compression:

Stored size: 609 Bytes

Contents

# frozen_string_literal: true

class 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
darthjee-core_ext-1.6.2 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.6.1 lib/darthjee/core_ext/hash/squasher.rb
darthjee-core_ext-1.6.0 lib/darthjee/core_ext/hash/squasher.rb