Class: Puma::Util::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/puma/util.rb

Overview

A case-insensitive Hash that preserves the original case of a header when set.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



90
91
92
93
94
# File 'lib/puma/util.rb', line 90

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

Instance Attribute Details

#to_hashObject (readonly)



103
104
105
106
107
# File 'lib/puma/util.rb', line 103

def to_hash
  hash = {}
  each { |k,v| hash[k] = v }
  hash
end

Class Method Details

.new(hash = {}) ⇒ Object



86
87
88
# File 'lib/puma/util.rb', line 86

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

Instance Method Details

#[](k) ⇒ Object



109
110
111
# File 'lib/puma/util.rb', line 109

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

#[]=(k, v) ⇒ Object



113
114
115
116
117
118
# File 'lib/puma/util.rb', line 113

def []=(k, v)
  canonical = k.downcase
  delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
  @names[k] = @names[canonical] = k
  super k, v
end

#delete(k) ⇒ Object



120
121
122
123
124
125
# File 'lib/puma/util.rb', line 120

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

#eachObject



96
97
98
99
100
# File 'lib/puma/util.rb', line 96

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)


127
128
129
# File 'lib/puma/util.rb', line 127

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

#merge(other) ⇒ Object



140
141
142
143
# File 'lib/puma/util.rb', line 140

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

#merge!(other) ⇒ Object



135
136
137
138
# File 'lib/puma/util.rb', line 135

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

#replace(other) ⇒ Object



145
146
147
148
149
# File 'lib/puma/util.rb', line 145

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