summaryrefslogtreecommitdiff
path: root/lib/mime/types/columnar.rb
blob: 594a72722a40691a79b3b827a73c10272e215a1e (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module MIME
  class Types
  end
end

require 'mime/type/columnar'

# MIME::Types::Columnar is used to extend a MIME::Types container to load data
# by columns instead of from JSON or YAML. Column loads of MIME types loaded
# through the columnar store are synchronized with a Mutex.
#
# MIME::Types::Columnar is not intended to be used directly, but will be added
# to an instance of MIME::Types when it is loaded with
# MIME::Types::Loader#load_columnar.
module MIME::Types::Columnar
  LOAD_MUTEX = Mutex.new # :nodoc:

  def self.extended(obj) # :nodoc:
    super
    obj.instance_variable_set(:@__mime_data__, [])
    obj.instance_variable_set(:@__attributes__, [])
  end

  # Load the first column data file (type and extensions).
  def load_base_data(path) #:nodoc:
    @__root__ = path

    each_file_line('content_type', false) do |line|
      content_type, extensions = line.split

      type = MIME::Type::Columnar.new(self, content_type, extensions)
      @__mime_data__ << type
      add(type)
    end

    self
  end

  private

  def each_file_line(name, lookup = true)
    LOAD_MUTEX.synchronize do
      next if @__attributes__.include?(name)

      File.open(File.join(@__root__, "mime.#{name}.column"), 'r:UTF-8') do |f|
        i = -1

        f.each_line do |line|
          line.chomp!

          if lookup
            type = @__mime_data__[i += 1] or next
            yield type, line
          else
            yield line
          end
        end
      end

      @__attributes__ << name
    end
  end

  def load_encoding
    pool = {}
    each_file_line('encoding') do |type, line|
      line.freeze
      type.encoding = (pool[line] ||= line)
    end
  end

  def load_docs
    each_file_line('docs') do |type, line|
      type.docs = arr(line)
    end
  end

  def load_obsolete
    each_file_line('obsolete') do |type, line|
      type.obsolete = bool(line)
    end
  end

  def load_references
    each_file_line('references') do |type, line|
      type.instance_variable_set(:@references, arr(line))
    end
  end

  def load_registered
    each_file_line('registered') do |type, line|
      type.registered = bool(line)
    end
  end

  def load_signature
    each_file_line('signature') do |type, line|
      type.signature = bool(line)
    end
  end

  def load_system
    each_file_line('system') do |type, line|
      type.system = (line unless line == '-'.freeze)
    end
  end

  def load_xrefs
    each_file_line('xrefs') { |type, line| type.xrefs = dict(line) }
  end

  def load_friendly
    each_file_line('friendly') { |type, line|
      v = dict(line)
      type.friendly = v.empty? ? nil : v
    }
  end

  def load_use_instead
    each_file_line('use_instead') do |type, line|
      type.use_instad = (line unless line == '-'.freeze)
    end
  end

  def dict(line)
    if line == '-'.freeze
      {}
    else
      line.split('|'.freeze).each_with_object({}) { |h, l|
        k, v = l.split('^'.freeze)
        v = [ nil ] if v.empty?
        h[k] = v
      }
    end
  end

  def arr(line)
    if line == '-'.freeze
      []
    else
      line.split('|'.freeze).flatten.compact.uniq
    end
  end

  def bool(line)
    line == '1'.freeze ? true : false
  end
end

unless MIME::Types.private_method_defined?(:load_mode)
  class << MIME::Types
    private

    def load_mode
      { columnar: true }
    end
  end

  require 'mime/types'
end