summaryrefslogtreecommitdiff
path: root/lib/mime/types/container.rb
blob: 176ae3e66297216c308ee90fceaf368b8f81cde4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

require 'set'

# MIME::Types requires a container Hash with a default values for keys
# resulting in an empty array (<tt>[]</tt>), but this cannot be dumped through
# Marshal because of the presence of that default Proc. This class exists
# solely to satisfy that need.
class MIME::Types::Container < Hash # :nodoc:
  def initialize
    super
    self.default_proc = ->(h, k) { h[k] = Set.new }
  end

  def marshal_dump
    {}.merge(self)
  end

  def marshal_load(hash)
    self.default_proc = ->(h, k) { h[k] = Set.new }
    merge!(hash)
  end

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

  def init_with(coder)
    self.default_proc = ->(h, k) { h[k] = Set.new }
    coder.map.each { |k, v| self[k] = Set[*v] }
  end
end