Sha256: de0b880fbdb94668b9629fdd0074cd2de9a6619bb1f8cf4b87050537c72a4f57

Contents?: true

Size: 1 KB

Versions: 17

Compression:

Stored size: 1 KB

Contents

# encoding: utf-8
# This file is distributed under Ting Yun's license terms.

module TingYun
  module Configuration
    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)
        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

      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

17 entries across 17 versions & 1 rubygems

Version Path
tingyun_rpm-3.2.0 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.6.1 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.5.0 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.4.2 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.4.1 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.3.1 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.3.0 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.2.0 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.4.2 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.4.1 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.4 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.3 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.2 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.1 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.1.0 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.0.7 lib/ting_yun/configuration/dotted_hash.rb
tingyun_rpm-1.0.6 lib/ting_yun/configuration/dotted_hash.rb