summaryrefslogtreecommitdiff
path: root/lib/mime/type/columnar.rb
blob: a51f9d9cbf92bdb1c345b58e586381cc1ef75084 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require "mime/type"

# A version of MIME::Type that works hand-in-hand with a MIME::Types::Columnar
# container to load data by columns.
#
# When a field is has not yet been loaded, that data will be loaded for all
# types in the container before forwarding the message to MIME::Type.
#
# More information can be found in MIME::Types::Columnar.
#
# MIME::Type::Columnar is *not* intended to be created except by
# MIME::Types::Columnar containers.
class MIME::Type::Columnar < MIME::Type
  def initialize(container, content_type, extensions) # :nodoc:
    @container = container
    self.content_type = content_type
    self.extensions = extensions
  end

  def self.column(*methods, file: nil) # :nodoc:
    file ||= methods.first

    file_method = :"load_#{file}"
    methods.each do |m|
      define_method m do |*args|
        @container.send(file_method)
        super(*args)
      end
    end
  end

  column :friendly
  column :encoding, :encoding=
  column :docs, :docs=
  column :preferred_extension, :preferred_extension=
  column :obsolete, :obsolete=, :obsolete?, :registered, :registered=, :registered?, :signature, :signature=,
    :signature?, :provisional, :provisional=, :provisional?, file: "flags"
  column :xrefs, :xrefs=, :xref_urls
  column :use_instead, :use_instead=
  column :extension_priorities, :extension_priorities=

  def encode_with(coder) # :nodoc:
    @container.send(:load_friendly)
    @container.send(:load_encoding)
    @container.send(:load_docs)
    @container.send(:load_flags)
    @container.send(:load_use_instead)
    @container.send(:load_xrefs)
    @container.send(:load_preferred_extension)
    @container.send(:load_extension_priorities)
    super
  end

  def update_sort_priority
    if @container.__fully_loaded?
      super
    else
      obsolete = (@__sort_priority & (1 << 7)) != 0
      registered = (@__sort_priority & (1 << 5)) == 0

      @__priority_penalty = (@obsolete ? 3 : 0) + (@registered ? 0 : 2)
    end
  end

  class << self
    undef column
  end
end