Sha256: c5ca20cc40f0bfbdadd6984a1da6812ec8eebfdfbaf4a5294ebe8b8e79b04fef

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'set'
require 'forwardable'

# 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 won't 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 doesn't survive the round-trip).
class MIME::Types::Container #:nodoc:
  extend Forwardable

  def initialize
    @container = {}
  end

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

  def_delegators :@container,
    :count,
    :each_value,
    :keys,
    :merge,
    :merge!,
    :select,
    :values

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

  def marshal_dump
    {}.merge(container)
  end

  def marshal_load(hash)
    @container = hash
  end

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

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

  private

  attr_reader :container

  EMPTY_SET = Set.new.freeze
  private_constant :EMPTY_SET
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mime-types-3.2 lib/mime/types/container.rb