Sha256: caca8e6b4b999c8fb024b9fc6dbc14495a62fc8e0fb85fbe8905301f54e7d225

Contents?: true

Size: 1.43 KB

Versions: 13

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require 'new_relic/agent/configuration'

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

13 entries across 13 versions & 1 rubygems

Version Path
newrelic_rpm-3.12.0.288 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.11.2.286 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.11.1.284 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.11.0.283 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.10.0.279 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.9.275 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.8.273 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.7.266 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.6.257 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.5.251 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.4.245 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.3.241 lib/new_relic/agent/configuration/dotted_hash.rb
newrelic_rpm-3.9.2.239 lib/new_relic/agent/configuration/dotted_hash.rb