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



418
419
420
421
422
# File 'lib/rack/utils.rb', line 418

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

Class Method Details

.new(hash = {}) ⇒ Object



414
415
416
# File 'lib/rack/utils.rb', line 414

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

Instance Method Details

#[](k) ⇒ Object



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

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

#[]=(k, v) ⇒ Object



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

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



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

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

#eachObject



430
431
432
433
434
# File 'lib/rack/utils.rb', line 430

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)


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

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

#initialize_copy(other) ⇒ Object

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



425
426
427
428
# File 'lib/rack/utils.rb', line 425

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

#merge(other) ⇒ Object



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

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

#merge!(other) ⇒ Object



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

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

#replace(other) ⇒ Object



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

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

#to_hashObject



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

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