Class: Rack::Utils::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
rack/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

- (HeaderHash) initialize(hash = {})

Returns a new instance of HeaderHash



443
444
445
446
447
# File 'rack/rack/utils.rb', line 443

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

Class Method Details

+ (void) new(hash = {})



439
440
441
# File 'rack/rack/utils.rb', line 439

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

Instance Method Details

- (void) [](k)



461
462
463
# File 'rack/rack/utils.rb', line 461

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

- (void) []=(k, v)



465
466
467
468
469
470
# File 'rack/rack/utils.rb', line 465

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

- (void) delete(k)



472
473
474
475
476
477
# File 'rack/rack/utils.rb', line 472

def delete(k)
  canonical = k.downcase
  result = super @names.delete(canonical)
  @names.delete_if { |name,| name.downcase == canonical }
  result
end

- (void) each



449
450
451
452
453
# File 'rack/rack/utils.rb', line 449

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

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

Returns:

  • (Boolean)


479
480
481
# File 'rack/rack/utils.rb', line 479

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

- (void) merge(other)



492
493
494
495
# File 'rack/rack/utils.rb', line 492

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

- (void) merge!(other)



487
488
489
490
# File 'rack/rack/utils.rb', line 487

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

- (void) replace(other)



497
498
499
500
501
# File 'rack/rack/utils.rb', line 497

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

- (void) to_hash



455
456
457
458
459
# File 'rack/rack/utils.rb', line 455

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