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.
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 |
#each ⇒ Object
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?
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_hash ⇒ Object
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 |