Sha256: 949ddb478285ace299be299679251f259c3fb5c58473ec44e3b9c3aecf34bf09

Contents?: true

Size: 1.84 KB

Versions: 52

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true
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
      super
      @downcased = {}
    end

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

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

    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.to_s.downcase, &proc)
    end

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

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

    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.to_s.downcase})
    end

  end
end

Version data entries

52 entries across 51 versions & 2 rubygems

Version Path
excon-1.2.1 lib/excon/headers.rb
excon-1.2.0 lib/excon/headers.rb
excon-1.1.1 lib/excon/headers.rb
excon-1.1.0 lib/excon/headers.rb
excon-1.0.0 lib/excon/headers.rb
excon-0.112.0 lib/excon/headers.rb
excon-0.111.0 lib/excon/headers.rb
excon-0.110.0 lib/excon/headers.rb
excon-0.109.0 lib/excon/headers.rb
excon-0.108.0 lib/excon/headers.rb
excon-0.107.0 lib/excon/headers.rb
excon-0.106.0 lib/excon/headers.rb
excon-0.105.0 lib/excon/headers.rb
excon-0.104.0 lib/excon/headers.rb
excon-0.103.0 lib/excon/headers.rb
excon-0.102.0 lib/excon/headers.rb
excon-0.101.0 lib/excon/headers.rb
excon-0.100.0 lib/excon/headers.rb
excon-0.99.0 lib/excon/headers.rb
excon-0.98.0 lib/excon/headers.rb