Class: ShopifyCLI::Theme::DevServer::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/shopify_cli/theme/dev_server/header_hash.rb

Overview

Based on Rack::HeaderHash

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HeaderHash

Returns a new instance of HeaderHash.



16
17
18
19
20
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 16

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

Class Method Details

.[](headers) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 8

def self.[](headers)
  if headers.is_a?(HeaderHash) && !headers.frozen?
    headers
  else
    new(headers)
  end
end

Instance Method Details

#[](k) ⇒ Object



46
47
48
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 46

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

#[]=(k, v) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 54

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

#clearObject

on clear, we need to clear @names hash



29
30
31
32
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 29

def clear
  super
  @names.clear
end

#delete(k) ⇒ Object



62
63
64
65
66
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 62

def delete(k)
  canonical = k.downcase
  result = super(@names.delete(canonical))
  result
end

#eachObject



34
35
36
37
38
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 34

def each
  super do |k, v|
    yield(k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v)
  end
end

#fetch(k, default = nil) ⇒ Object



50
51
52
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 50

def fetch(k, default = nil)
  self[k] || super(@names[k.downcase], default)
end

#include?(k) ⇒ Boolean Also known as: has_key?, member?, key?

Returns:

  • (Boolean)


68
69
70
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 68

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

#initialize_copy(other) ⇒ Object

on dup/clone, we need to duplicate @names hash



23
24
25
26
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 23

def initialize_copy(other)
  super
  @names = other.names.dup
end

#merge(other) ⇒ Object



81
82
83
84
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 81

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

#merge!(other) ⇒ Object



76
77
78
79
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 76

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

#replace(other) ⇒ Object



86
87
88
89
90
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 86

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

#to_hashObject



40
41
42
43
44
# File 'lib/shopify_cli/theme/dev_server/header_hash.rb', line 40

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