Sha256: f47faf063a492192243526fbb1c2229b46ac76e9ce2737ffc01a72ec978eafa9

Contents?: true

Size: 949 Bytes

Versions: 42

Compression:

Stored size: 949 Bytes

Contents

# encoding: utf-8

module OneApm
  module Support
    class DottedHash < ::Hash

      def self.symbolize(hash)
        hash.keys.each do |key|
          hash[key.to_sym] = hash.delete(key)
        end
      end

      def initialize(hash, keep_nesting = false)
        merge!(hash) if keep_nesting
        merge!(dot_flattened(hash))

        DottedHash.symbolize(self)
      end

      def inspect
        "#<#{self.class.name}:#{object_id} #{super}>"
      end

      def to_hash
        {}.replace(self)
      end

      protected

      # turns {'a' => {'b' => 'c'}} into {'a.b' => 'c'}
      def dot_flattened(nested_hash, names=[], result={})
        nested_hash.each do |key, val|
          next if val == nil
          if val.respond_to?(:has_key?)
            dot_flattened(val, names + [key], result)
          else
            result[(names + [key]).join('.')] = val
          end
        end
        result
      end
    end

  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.4.1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.4.0 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.7 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.6 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.5 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.4 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.3 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.3.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.2 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.2.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.1.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.0 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.3.0.rc1 lib/one_apm/support/dotted_hash.rb
oneapm_rpm-1.2.9 lib/one_apm/support/dotted_hash.rb