Class: MIME::Types::Container
- Inherits:
-
Object
- Object
- MIME::Types::Container
- Extended by:
- Forwardable
- 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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #add(key, value) ⇒ Object
- #encode_with(coder) ⇒ Object
- #init_with(coder) ⇒ Object
-
#initialize(hash = {}) ⇒ Container
constructor
A new instance of Container.
- #marshal_dump ⇒ Object
- #marshal_load(hash) ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Container
Returns a new instance of Container.
15 16 17 18 |
# File 'lib/mime/types/container.rb', line 15 def initialize(hash = {}) @container = {} merge!(hash) end |
Instance Method Details
#[](key) ⇒ Object
20 21 22 |
# File 'lib/mime/types/container.rb', line 20 def [](key) container[key] || EMPTY_SET end |
#[]=(key, value) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/mime/types/container.rb', line 24 def []=(key, value) container[key] = case value when Set value else Set[*value] end end |
#add(key, value) ⇒ Object
61 62 63 |
# File 'lib/mime/types/container.rb', line 61 def add(key, value) (container[key] ||= Set.new).add(value) end |
#encode_with(coder) ⇒ Object
73 74 75 |
# File 'lib/mime/types/container.rb', line 73 def encode_with(coder) container.each { |k, v| coder[k] = v.to_a } end |
#init_with(coder) ⇒ Object
77 78 79 80 |
# File 'lib/mime/types/container.rb', line 77 def init_with(coder) @container = {} coder.map.each { |k, v| container[k] = Set[*v] } end |
#marshal_dump ⇒ Object
65 66 67 |
# File 'lib/mime/types/container.rb', line 65 def marshal_dump {}.merge(container) end |
#marshal_load(hash) ⇒ Object
69 70 71 |
# File 'lib/mime/types/container.rb', line 69 def marshal_load(hash) @container = hash end |
#merge(other) ⇒ Object
34 35 36 |
# File 'lib/mime/types/container.rb', line 34 def merge(other) self.class.new(other) end |
#merge!(other) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/mime/types/container.rb', line 38 def merge!(other) tap { other = other.is_a?(MIME::Types::Container) ? other.container : other container.merge!(other) normalize } end |
#to_hash ⇒ Object
46 47 48 |
# File 'lib/mime/types/container.rb', line 46 def to_hash container end |