Sha256: 7ba907b5dc1cfae670a494a7d079b10c5498ad56a1e635b6ee026a45429aa2c8

Contents?: true

Size: 768 Bytes

Versions: 2

Compression:

Stored size: 768 Bytes

Contents

module Buckaruby
  module Support
    # The case insensitive Hash is a Hash with case insensitive keys that
    # can also be accessed by using a Symbol.
    class CaseInsensitiveHash < Hash
      def initialize(constructor = {})
        if constructor.is_a?(Hash)
          super()
          update(constructor)
        else
          super(constructor)
        end
      end

      def [](key)
        super(convert_key(key))
      end

      def []=(key, value)
        super(convert_key(key), value)
      end

      protected

      def update(hash)
        hash.each_pair { |key, value| self[convert_key(key)] = value }
      end

      def convert_key(key)
        string = key.is_a?(Symbol) ? key.to_s : key
        string.downcase
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
buckaruby-1.0.2 lib/buckaruby/support/case_insensitive_hash.rb
buckaruby-1.0.1 lib/buckaruby/support/case_insensitive_hash.rb