Class: Rack::Utils::HeaderHash Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



416
417
418
419
420
# File 'lib/rack/utils.rb', line 416

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

Instance Method Details

#[](k) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#[]=(k, v) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



444
445
446
447
448
449
# File 'lib/rack/utils.rb', line 444

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#eachObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



428
429
430
431
432
# File 'lib/rack/utils.rb', line 428

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?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#initialize_copy(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



423
424
425
426
# File 'lib/rack/utils.rb', line 423

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

#merge(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#merge!(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#replace(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#to_hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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