Class: Rack::Utils::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/rack/utils.rb

Overview

A case-insensitive Hash that preserves the original case of a header when set.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



440
441
442
443
444
# File 'lib/rack/utils.rb', line 440

def initialize(hash={})
  super()
  @names = {}
  hash.each { |k, v| self[k] = v }
end

Class Method Details

.new(hash = {}) ⇒ Object



436
437
438
# File 'lib/rack/utils.rb', line 436

def self.new(hash={})
  HeaderHash === hash ? hash : super(hash)
end

Instance Method Details

#[](k) ⇒ Object



464
465
466
# File 'lib/rack/utils.rb', line 464

def [](k)
  super(k) || super(@names[k.downcase])
end

#[]=(k, v) ⇒ Object



468
469
470
471
472
473
# File 'lib/rack/utils.rb', line 468

def []=(k, v)
  canonical = k.downcase.freeze
  delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
  @names[canonical] = k
  super k, v
end

#delete(k) ⇒ Object



475
476
477
478
479
# File 'lib/rack/utils.rb', line 475

def delete(k)
  canonical = k.downcase
  result = super @names.delete(canonical)
  result
end

#eachObject



452
453
454
455
456
# File 'lib/rack/utils.rb', line 452

def each
  super do |k, v|
    yield(k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v)
  end
end

#include?(k) ⇒ Boolean Also known as: has_key?, member?, key?

Returns:

  • (Boolean)


481
482
483
# File 'lib/rack/utils.rb', line 481

def include?(k)
  super || @names.include?(k.downcase)
end

#initialize_copy(other) ⇒ Object

on dup/clone, we need to duplicate @names hash



447
448
449
450
# File 'lib/rack/utils.rb', line 447

def initialize_copy(other)
  super
  @names = other.names.dup
end

#merge(other) ⇒ Object



494
495
496
497
# File 'lib/rack/utils.rb', line 494

def merge(other)
  hash = dup
  hash.merge! other
end

#merge!(other) ⇒ Object



489
490
491
492
# File 'lib/rack/utils.rb', line 489

def merge!(other)
  other.each { |k, v| self[k] = v }
  self
end

#replace(other) ⇒ Object



499
500
501
502
503
# File 'lib/rack/utils.rb', line 499

def replace(other)
  clear
  other.each { |k, v| self[k] = v }
  self
end

#to_hashObject



458
459
460
461
462
# File 'lib/rack/utils.rb', line 458

def to_hash
  hash = {}
  each { |k,v| hash[k] = v }
  hash
end