Class: Fetch::MultiMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fetch/multi_map.rb

Direct Known Subclasses

FormData, URLSearchParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultiMap

Returns a new instance of MultiMap.



5
6
7
# File 'lib/fetch/multi_map.rb', line 5

def initialize
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



9
10
11
# File 'lib/fetch/multi_map.rb', line 9

def entries
  @entries
end

Instance Method Details

#append(key, value) ⇒ Object



11
12
13
# File 'lib/fetch/multi_map.rb', line 11

def append(key, value)
  @entries.push [to_key(key), to_value(value)]
end

#delete(key) ⇒ Object



15
16
17
# File 'lib/fetch/multi_map.rb', line 15

def delete(key)
  @entries.reject! {|k,| k == to_key(key) }
end

#eachObject



44
45
46
# File 'lib/fetch/multi_map.rb', line 44

def each(&)
  @entries.each(&)
end

#get(key) ⇒ Object



19
20
21
# File 'lib/fetch/multi_map.rb', line 19

def get(key)
  @entries.assoc(to_key(key))&.last
end

#get_all(key) ⇒ Object



23
24
25
# File 'lib/fetch/multi_map.rb', line 23

def get_all(key)
  @entries.select {|k,| k == to_key(key) }.map(&:last)
end

#has(key) ⇒ Object



27
28
29
# File 'lib/fetch/multi_map.rb', line 27

def has(key)
  @entries.any? {|k,| k == to_key(key) }
end

#keysObject



31
32
33
# File 'lib/fetch/multi_map.rb', line 31

def keys
  @entries.map(&:first)
end

#set(key, value) ⇒ Object



35
36
37
38
# File 'lib/fetch/multi_map.rb', line 35

def set(key, value)
  delete key
  append key, value
end

#valuesObject



40
41
42
# File 'lib/fetch/multi_map.rb', line 40

def values
  @entries.map(&:last)
end