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.


297
298
299
300
301
# File 'lib/rack/utils.rb', line 297

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

Class Method Details

.new(hash = {}) ⇒ Object


293
294
295
# File 'lib/rack/utils.rb', line 293

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

Instance Method Details

#[](k) ⇒ Object


320
321
322
323
# File 'lib/rack/utils.rb', line 320

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

#[]=(k, v) ⇒ Object


325
326
327
328
329
# File 'lib/rack/utils.rb', line 325

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

#delete(k) ⇒ Object


331
332
333
334
335
336
# File 'lib/rack/utils.rb', line 331

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

#eachObject


303
304
305
306
307
# File 'lib/rack/utils.rb', line 303

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)

338
339
340
# File 'lib/rack/utils.rb', line 338

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

#merge(other) ⇒ Object


351
352
353
354
# File 'lib/rack/utils.rb', line 351

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

#merge!(other) ⇒ Object


346
347
348
349
# File 'lib/rack/utils.rb', line 346

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

#replace(other) ⇒ Object


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

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

#to_hashObject


309
310
311
312
313
314
315
316
317
318
# File 'lib/rack/utils.rb', line 309

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