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.



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

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

Class Method Details

.new(hash = {}) ⇒ Object



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

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

Instance Method Details

#[](k) ⇒ Object



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

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

#[]=(k, v) ⇒ Object



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

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

#delete(k) ⇒ Object



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

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

#eachObject



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

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)


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

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

#merge(other) ⇒ Object



505
506
507
508
# File 'lib/rack/utils.rb', line 505

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

#merge!(other) ⇒ Object



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

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

#replace(other) ⇒ Object



510
511
512
513
514
# File 'lib/rack/utils.rb', line 510

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

#to_hashObject



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

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