Class: Rack::Utils::HeaderHash

Inherits:
Hash show all
Defined in:
lib/vendor/rack-1.5.2/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

Methods inherited from Hash

[], #assoc, constructor_without_key_value_pair_form, #default_proc=, #default_proc_with_nil=, #eql?, #hash, #keep_if, #rassoc, #reverse_merge, #reverse_merge!, #select!, #select_with_hash_return, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_h, try_convert

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



443
444
445
446
447
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 443

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

Class Method Details

.new(hash = {}) ⇒ Object



439
440
441
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 439

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

Instance Method Details

#[](k) ⇒ Object



461
462
463
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 461

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

#[]=(k, v) ⇒ Object



465
466
467
468
469
470
# File 'lib/vendor/rack-1.5.2/lib/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

#delete(k) ⇒ Object



472
473
474
475
476
477
# File 'lib/vendor/rack-1.5.2/lib/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

#eachObject



449
450
451
452
453
# File 'lib/vendor/rack-1.5.2/lib/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

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

Returns:

  • (Boolean)


479
480
481
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 479

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

#merge(other) ⇒ Object



492
493
494
495
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 492

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

#merge!(other) ⇒ Object



487
488
489
490
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 487

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

#replace(other) ⇒ Object



497
498
499
500
501
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 497

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

#to_hashObject



455
456
457
458
459
# File 'lib/vendor/rack-1.5.2/lib/rack/utils.rb', line 455

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