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.



264
265
266
267
268
# File 'lib/rack/utils.rb', line 264

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

Class Method Details

.new(hash = {}) ⇒ Object



260
261
262
# File 'lib/rack/utils.rb', line 260

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

Instance Method Details

#[](k) ⇒ Object



287
288
289
# File 'lib/rack/utils.rb', line 287

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

#[]=(k, v) ⇒ Object



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

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

#delete(k) ⇒ Object



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

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

#eachObject



270
271
272
273
274
# File 'lib/rack/utils.rb', line 270

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)


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

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

#merge(other) ⇒ Object



317
318
319
320
# File 'lib/rack/utils.rb', line 317

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

#merge!(other) ⇒ Object



312
313
314
315
# File 'lib/rack/utils.rb', line 312

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

#replace(other) ⇒ Object



322
323
324
325
326
# File 'lib/rack/utils.rb', line 322

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

#to_hashObject



276
277
278
279
280
281
282
283
284
285
# File 'lib/rack/utils.rb', line 276

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