Class: MIME::Types::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/mime/types/container.rb

Overview

MIME::Types requires a serializable keyed container that returns an empty Set on a key miss. Hash#default_value cannot be used because, while it traverses the Marshal format correctly, it will not survive any other serialization format (plus, a default of a mutable object resuls in a shared mess). Hash#default_proc cannot be used without a wrapper because it prevents Marshal serialization (and does not survive the round-trip).

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Container

:nodoc:



12
13
14
15
# File 'lib/mime/types/container.rb', line 12

def initialize(hash = {})
  @container = {}
  merge!(hash)
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
# File 'lib/mime/types/container.rb', line 47

def ==(other)
  container == other
end

#[](key) ⇒ Object



17
18
19
# File 'lib/mime/types/container.rb', line 17

def [](key)
  container[key] || EMPTY_SET
end

#[]=(key, value) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/mime/types/container.rb', line 21

def []=(key, value)
  container[key] =
    case value
    when Set
      value
    else
      Set[*value]
    end
end

#add(key, value) ⇒ Object



91
92
93
# File 'lib/mime/types/container.rb', line 91

def add(key, value)
  (container[key] ||= Set.new).add(value)
end

#count(*args, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/mime/types/container.rb', line 51

def count(*args, &block)
  if args.size == 0
    container.count
  elsif block
    container.count(&block)
  else
    container.count(args.first)
  end
end

#each_pair(&block) ⇒ Object Also known as: each



61
62
63
# File 'lib/mime/types/container.rb', line 61

def each_pair(&block)
  container.each_pair(&block)
end

#each_value(&block) ⇒ Object



67
68
69
# File 'lib/mime/types/container.rb', line 67

def each_value(&block)
  container.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mime/types/container.rb', line 71

def empty?
  container.empty?
end

#encode_with(coder) ⇒ Object



103
104
105
# File 'lib/mime/types/container.rb', line 103

def encode_with(coder)
  container.each { |k, v| coder[k] = v.to_a }
end

#flat_map(&block) ⇒ Object



75
76
77
# File 'lib/mime/types/container.rb', line 75

def flat_map(&block)
  container.flat_map(&block)
end

#init_with(coder) ⇒ Object



107
108
109
110
# File 'lib/mime/types/container.rb', line 107

def init_with(coder)
  @container = {}
  coder.map.each { |k, v| container[k] = Set[*v] }
end

#keysObject



79
80
81
# File 'lib/mime/types/container.rb', line 79

def keys
  container.keys
end

#marshal_dumpObject



95
96
97
# File 'lib/mime/types/container.rb', line 95

def marshal_dump
  {}.merge(container)
end

#marshal_load(hash) ⇒ Object



99
100
101
# File 'lib/mime/types/container.rb', line 99

def marshal_load(hash)
  @container = hash
end

#merge(other) ⇒ Object



31
32
33
# File 'lib/mime/types/container.rb', line 31

def merge(other)
  self.class.new(other)
end

#merge!(other) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/mime/types/container.rb', line 35

def merge!(other)
  tap {
    other = other.is_a?(MIME::Types::Container) ? other.container : other
    container.merge!(other)
    normalize
  }
end

#select(&block) ⇒ Object



87
88
89
# File 'lib/mime/types/container.rb', line 87

def select(&block)
  container.select(&block)
end

#to_hashObject



43
44
45
# File 'lib/mime/types/container.rb', line 43

def to_hash
  container
end

#valuesObject



83
84
85
# File 'lib/mime/types/container.rb', line 83

def values
  container.values
end