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.



334
335
336
337
338
# File 'lib/rack/utils.rb', line 334

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

Class Method Details

.new(hash = {}) ⇒ Object



330
331
332
# File 'lib/rack/utils.rb', line 330

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

Instance Method Details

#[](k) ⇒ Object



357
358
359
360
# File 'lib/rack/utils.rb', line 357

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

#[]=(k, v) ⇒ Object



362
363
364
365
366
# File 'lib/rack/utils.rb', line 362

def []=(k, v)
  delete k
  @names[k] = @names[k.downcase] = k
  super k, v
end

#delete(k) ⇒ Object



368
369
370
371
372
373
# File 'lib/rack/utils.rb', line 368

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

#eachObject



340
341
342
343
344
# File 'lib/rack/utils.rb', line 340

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)


375
376
377
# File 'lib/rack/utils.rb', line 375

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

#merge(other) ⇒ Object



388
389
390
391
# File 'lib/rack/utils.rb', line 388

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

#merge!(other) ⇒ Object



383
384
385
386
# File 'lib/rack/utils.rb', line 383

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

#replace(other) ⇒ Object



393
394
395
396
397
# File 'lib/rack/utils.rb', line 393

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

#to_hashObject



346
347
348
349
350
351
352
353
354
355
# File 'lib/rack/utils.rb', line 346

def to_hash
  inject({}) do |hash, (k,v)|
    if v.respond_to? :to_ary
      hash[k] = v.to_ary.join("\n")
    else
      hash[k] = v
    end
    hash
  end
end