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.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



261
262
263
264
265
# File 'lib/rack/utils.rb', line 261

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

Instance Method Details

#[](k) ⇒ Object



278
279
280
# File 'lib/rack/utils.rb', line 278

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

#[]=(k, v) ⇒ Object



282
283
284
285
286
# File 'lib/rack/utils.rb', line 282

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

#delete(k) ⇒ Object



288
289
290
291
292
293
# File 'lib/rack/utils.rb', line 288

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

#include?(k) ⇒ Boolean Also known as: has_key?, member?, key?

Returns:

  • (Boolean)


295
296
297
# File 'lib/rack/utils.rb', line 295

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

#merge(other) ⇒ Object



308
309
310
311
# File 'lib/rack/utils.rb', line 308

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

#merge!(other) ⇒ Object



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

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

#replace(other) ⇒ Object



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

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

#to_hashObject



267
268
269
270
271
272
273
274
275
276
# File 'lib/rack/utils.rb', line 267

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