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.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #replace(other) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ HeaderHash
Returns a new instance of HeaderHash.
270 271 272 273 274 |
# File 'lib/rack/utils.rb', line 270 def initialize(hash={}) super() @names = {} hash.each { |k, v| self[k] = v } end |
Class Method Details
.new(hash = {}) ⇒ Object
266 267 268 |
# File 'lib/rack/utils.rb', line 266 def self.new(hash={}) HeaderHash === hash ? hash : super(hash) end |
Instance Method Details
#[](k) ⇒ Object
293 294 295 |
# File 'lib/rack/utils.rb', line 293 def [](k) super(@names[k] ||= @names[k.downcase]) end |
#[]=(k, v) ⇒ Object
297 298 299 300 301 |
# File 'lib/rack/utils.rb', line 297 def []=(k, v) delete k @names[k] = @names[k.downcase] = k super k, v end |
#delete(k) ⇒ Object
303 304 305 306 307 308 |
# File 'lib/rack/utils.rb', line 303 def delete(k) canonical = k.downcase result = super @names.delete(canonical) @names.delete_if { |name,| name.downcase == canonical } result end |
#each ⇒ Object
276 277 278 279 280 |
# File 'lib/rack/utils.rb', line 276 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?
310 311 312 |
# File 'lib/rack/utils.rb', line 310 def include?(k) @names.include?(k) || @names.include?(k.downcase) end |
#merge(other) ⇒ Object
323 324 325 326 |
# File 'lib/rack/utils.rb', line 323 def merge(other) hash = dup hash.merge! other end |
#merge!(other) ⇒ Object
318 319 320 321 |
# File 'lib/rack/utils.rb', line 318 def merge!(other) other.each { |k, v| self[k] = v } self end |
#replace(other) ⇒ Object
328 329 330 331 332 |
# File 'lib/rack/utils.rb', line 328 def replace(other) clear other.each { |k, v| self[k] = v } self end |
#to_hash ⇒ Object
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/rack/utils.rb', line 282 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 |