module MIME::Types::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.

Private Instance Methods

arr(line) click to toggle source
# File lib/mime/types/_columnar.rb, line 121
def arr(line)
  if line == "-"
    []
  else
    line.split("|").flatten.compact.uniq
  end
end
dict(line, array: false) click to toggle source
# File lib/mime/types/_columnar.rb, line 109
def dict(line, array: false)
  if line == "-"
    {}
  else
    line.split("|").each_with_object({}) { |l, h|
      k, v = l.split("^")
      v = nil if v.empty?
      h[k] = array ? Array(v) : v
    }
  end
end
each_file_line(name, lookup = true) { |type, line| ... } click to toggle source
# File lib/mime/types/_columnar.rb, line 40
def each_file_line(name, lookup = true)
  LOAD_MUTEX.synchronize do
    next if @__files__.include?(name)

    i = -1
    column = File.join(@__root__, "mime.#{name}.column")

    IO.readlines(column, encoding: "UTF-8").each do |line|
      line.chomp!

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

    @__files__ << name
  end
end
flag(line) click to toggle source
# File lib/mime/types/_columnar.rb, line 133
def flag(line)
  line == "1"
end
load_docs() click to toggle source
# File lib/mime/types/_columnar.rb, line 69
def load_docs
  each_file_line("docs") do |type, line|
    type.instance_variable_set(:@docs, opt(line))
  end
end
load_encoding() click to toggle source
# File lib/mime/types/_columnar.rb, line 62
def load_encoding
  each_file_line("encoding") do |type, line|
    pool ||= {}
    type.instance_variable_set(:@encoding, (pool[line] ||= line))
  end
end
load_flags() click to toggle source
# File lib/mime/types/_columnar.rb, line 81
def load_flags
  each_file_line("flags") do |type, line|
    line = line.split
    type.instance_variable_set(:@obsolete, flag(line.shift))
    type.instance_variable_set(:@registered, flag(line.shift))
    type.instance_variable_set(:@signature, flag(line.shift))
    type.instance_variable_set(:@provisional, flag(line.shift))
  end
end
load_friendly() click to toggle source
# File lib/mime/types/_columnar.rb, line 97
def load_friendly
  each_file_line("friendly") { |type, line|
    type.instance_variable_set(:@friendly, dict(line))
  }
end
load_preferred_extension() click to toggle source
# File lib/mime/types/_columnar.rb, line 75
def load_preferred_extension
  each_file_line("pext") do |type, line|
    type.instance_variable_set(:@preferred_extension, opt(line))
  end
end
load_use_instead() click to toggle source
# File lib/mime/types/_columnar.rb, line 103
def load_use_instead
  each_file_line("use_instead") do |type, line|
    type.instance_variable_set(:@use_instead, opt(line))
  end
end
load_xrefs() click to toggle source
# File lib/mime/types/_columnar.rb, line 91
def load_xrefs
  each_file_line("xrefs") { |type, line|
    type.instance_variable_set(:@xrefs, dict(line, array: true))
  }
end
opt(line) click to toggle source
# File lib/mime/types/_columnar.rb, line 129
def opt(line)
  line unless line == "-"
end