Sha256: b64386df627297340f7a31d36369a308db7712f961d3eee0c14f542c19f7c811

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module Excon
  class Headers < Hash

    SENTINEL = {}

    alias_method :raw_writer, :[]=
    alias_method :raw_reader, :[]
    if SENTINEL.respond_to?(:assoc)
      alias_method :raw_assoc, :assoc
    end
    alias_method :raw_delete, :delete
    alias_method :raw_fetch, :fetch
    alias_method :raw_has_key?, :has_key?
    alias_method :raw_include?, :include?
    alias_method :raw_key?, :key?
    alias_method :raw_member?, :member?
    alias_method :raw_merge, :merge
    alias_method :raw_merge!, :merge!
    alias_method :raw_rehash, :rehash
    alias_method :raw_store, :store
    alias_method :raw_values_at, :values_at

    def initialize
      @downcased = {}
    end

    def [](key)
      @downcased[key.downcase]
    end

    alias_method :[]=, :store
    def []=(key, value)
      raw_writer(key, value)
      @downcased[key.downcase] = value
    end

    if SENTINEL.respond_to? :assoc
      def assoc(obj)
        @downcased.assoc(obj.downcase)
      end
    end

    def delete(key, &proc)
      raw_delete(key, &proc)
      @downcased.delete(key.downcase, &proc)
    end

    def fetch(key, default = nil, &proc)
      if proc
        @downcased.fetch(key.downcase, &proc)
      else
        @downcased.fetch(key.downcase, default)
      end
    end

    alias_method :has_key?, :key?
    alias_method :has_key?, :member?
    def has_key?(key)
      raw_key?(key) || @downcased.has_key?(key.downcase)
    end

    def merge(other_hash)
      self.dup.merge!(other_hash)
    end

    def merge!(other_hash)
      other_hash.each do |key, value|
        self[key] = value
      end
      raw_merge!(other_hash)
    end

    def rehash
      @downcased.rehash
      raw_rehash
    end

    def values_at(*keys)
      @downcased.values_at(*keys.map {|key| key.downcase})
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
excon-0.39.4 lib/excon/headers.rb