Class: Warc::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/warc/utils/header_hash.rb

Direct Known Subclasses

Record::Header

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



3
4
5
6
7
# File 'lib/warc/utils/header_hash.rb', line 3

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

Instance Method Details

#[](k) ⇒ Object



21
22
23
# File 'lib/warc/utils/header_hash.rb', line 21

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

#[]=(k, v) ⇒ Object



25
26
27
28
29
30
# File 'lib/warc/utils/header_hash.rb', line 25

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



32
33
34
35
36
37
# File 'lib/warc/utils/header_hash.rb', line 32

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

#eachObject



9
10
11
12
13
# File 'lib/warc/utils/header_hash.rb', line 9

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)


39
40
41
# File 'lib/warc/utils/header_hash.rb', line 39

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

#merge(other) ⇒ Object



52
53
54
55
# File 'lib/warc/utils/header_hash.rb', line 52

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

#merge!(other) ⇒ Object



47
48
49
50
# File 'lib/warc/utils/header_hash.rb', line 47

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

#replace(other) ⇒ Object



57
58
59
60
61
# File 'lib/warc/utils/header_hash.rb', line 57

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

#to_hashObject



15
16
17
18
19
# File 'lib/warc/utils/header_hash.rb', line 15

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