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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.


63
64
65
66
67
# File 'lib/puma/util.rb', line 63

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

Class Method Details

.new(hash = {}) ⇒ Object


59
60
61
# File 'lib/puma/util.rb', line 59

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

Instance Method Details

#[](k) ⇒ Object


81
82
83
# File 'lib/puma/util.rb', line 81

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

#[]=(k, v) ⇒ Object


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

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


92
93
94
95
96
97
# File 'lib/puma/util.rb', line 92

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

#eachObject


69
70
71
72
73
# File 'lib/puma/util.rb', line 69

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)

99
100
101
# File 'lib/puma/util.rb', line 99

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

#merge(other) ⇒ Object


112
113
114
115
# File 'lib/puma/util.rb', line 112

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

#merge!(other) ⇒ Object


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

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

#replace(other) ⇒ Object


117
118
119
120
121
# File 'lib/puma/util.rb', line 117

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

#to_hashObject


75
76
77
78
79
# File 'lib/puma/util.rb', line 75

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