Sha256: ca59286e45807d32c85bb63f5b4fa1fe603e8f6dc667c42d5239795b4917b541

Contents?: true

Size: 852 Bytes

Versions: 3

Compression:

Stored size: 852 Bytes

Contents

module Longleaf
  # Hash subclass which provides case insensitive keys, where keys are always downcased.
  class CaseInsensitiveHash < Hash
    def [](key)
      super _insensitive(key)
    end

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

    def delete(key)
      super _insensitive(key)
    end

    def has_key?(key)
      super _insensitive(key)
    end

    def merge(other_hash)
      super other_hash.map {|k, v| [_insensitive(k), v] }.to_h
    end

    def merge!(other_hash)
      super other_hash.map {|k, v| [_insensitive(k), v] }.to_h
    end

    # Cause this hash to serialize as a regular hash to avoid deserialization failures
    def encode_with coder
      coder.represent_map nil, self
    end

    protected
    def _insensitive(key)
      key.respond_to?(:downcase) ? key.downcase : key
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
longleaf-1.1.1 lib/longleaf/helpers/case_insensitive_hash.rb
longleaf-1.1.0 lib/longleaf/helpers/case_insensitive_hash.rb
longleaf-1.0.0 lib/longleaf/helpers/case_insensitive_hash.rb