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.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #delete(k) ⇒ 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.
213 214 215 216 217 |
# File 'lib/rack/utils.rb', line 213 def initialize(hash={}) super() @names = {} hash.each { |k, v| self[k] = v } end |
Instance Method Details
#[](k) ⇒ Object
230 231 232 |
# File 'lib/rack/utils.rb', line 230 def [](k) super(@names[k] ||= @names[k.downcase]) end |
#[]=(k, v) ⇒ Object
234 235 236 237 238 |
# File 'lib/rack/utils.rb', line 234 def []=(k, v) delete k @names[k] = @names[k.downcase] = k super k, v end |
#delete(k) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/rack/utils.rb', line 240 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?
247 248 249 |
# File 'lib/rack/utils.rb', line 247 def include?(k) @names.include?(k) || @names.include?(k.downcase) end |
#merge(other) ⇒ Object
260 261 262 263 |
# File 'lib/rack/utils.rb', line 260 def merge(other) hash = dup hash.merge! other end |
#merge!(other) ⇒ Object
255 256 257 258 |
# File 'lib/rack/utils.rb', line 255 def merge!(other) other.each { |k, v| self[k] = v } self end |
#replace(other) ⇒ Object
265 266 267 268 269 |
# File 'lib/rack/utils.rb', line 265 def replace(other) clear other.each { |k, v| self[k] = v } self end |
#to_hash ⇒ Object
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rack/utils.rb', line 219 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 |