Class: Rack::Utils::HeaderHash
- Inherits:
-
Hash
- Object
- Hash
- Rack::Utils::HeaderHash
- 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
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #delete(k) ⇒ Object
- #each ⇒ Object
- #include?(k) ⇒ Boolean (also: #has_key?, #member?, #key?)
-
#initialize(hash = {}) ⇒ HeaderHash
constructor
A new instance of HeaderHash.
-
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #replace(other) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ HeaderHash
Returns a new instance of HeaderHash.
440 441 442 443 444 |
# File 'lib/rack/utils.rb', line 440 def initialize(hash={}) super() @names = {} hash.each { |k, v| self[k] = v } end |
Class Method Details
.new(hash = {}) ⇒ Object
436 437 438 |
# File 'lib/rack/utils.rb', line 436 def self.new(hash={}) HeaderHash === hash ? hash : super(hash) end |
Instance Method Details
#[](k) ⇒ Object
464 465 466 |
# File 'lib/rack/utils.rb', line 464 def [](k) super(k) || super(@names[k.downcase]) end |
#[]=(k, v) ⇒ Object
468 469 470 471 472 473 |
# File 'lib/rack/utils.rb', line 468 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
475 476 477 478 479 |
# File 'lib/rack/utils.rb', line 475 def delete(k) canonical = k.downcase result = super @names.delete(canonical) result end |
#each ⇒ Object
452 453 454 455 456 |
# File 'lib/rack/utils.rb', line 452 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?
481 482 483 |
# File 'lib/rack/utils.rb', line 481 def include?(k) super || @names.include?(k.downcase) end |
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash
447 448 449 450 |
# File 'lib/rack/utils.rb', line 447 def initialize_copy(other) super @names = other.names.dup end |
#merge(other) ⇒ Object
494 495 496 497 |
# File 'lib/rack/utils.rb', line 494 def merge(other) hash = dup hash.merge! other end |
#merge!(other) ⇒ Object
489 490 491 492 |
# File 'lib/rack/utils.rb', line 489 def merge!(other) other.each { |k, v| self[k] = v } self end |
#replace(other) ⇒ Object
499 500 501 502 503 |
# File 'lib/rack/utils.rb', line 499 def replace(other) clear other.each { |k, v| self[k] = v } self end |
#to_hash ⇒ Object
458 459 460 461 462 |
# File 'lib/rack/utils.rb', line 458 def to_hash hash = {} each { |k,v| hash[k] = v } hash end |