Sha256: 939dfd9deb4c66f10af94ed30e92e3bc7bd4acfefcfb5061e25e5c21cdca76ed

Contents?: true

Size: 1.42 KB

Versions: 31

Compression:

Stored size: 1.42 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic
  module Agent
    module Configuration
      class DottedHash < ::Hash
        def initialize(hash, keep_nesting = false)
          # Add the hash keys to our collection explicitly so they survive the
          # dot flattening.  This is typical for full config source instances,
          # but not for uses of DottedHash serializing for transmission.
          self.merge!(hash) if keep_nesting

          self.merge!(dot_flattened(hash))
          DottedHash.symbolize(self)
        end

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

        def to_hash
          {}.replace(self)
        end

        def self.symbolize(hash)
          hash.keys.each do |key|
            hash[key.to_sym] = hash.delete(key)
          end
        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
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
newrelic_rpm-9.16.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.15.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.14.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.13.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.12.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.11.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.10.2 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.10.1 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.10.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.9.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.8.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.7.1 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.7.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.6.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.5.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.4.2 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.4.1 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.4.0 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.3.1 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-9.3.0 lib/new_relic/agent/configuration/dotted_hash.rb