class MIME::Types

#

def [](type_id, complete: false, registered: false)

6. Sort on name.
5. Obsolete definitions use-instead clauses are compared.
without;
4. Obsolete definitions with use-instead clauses sort before those
3. Current definitions sort before obsolete ones;
definitions.
2. IANA-registered definitions sort before LTSW-recorded
1. Complete definitions sort before incomplete ones;
follows:
If multiple type definitions are returned, returns them sorted as

end
puts t.to_a.join(", ")
MIME::Types[/^image/, :complete => true].each do |t|
puts "\nMIME::Types[/^image/, complete: true]"

MIME::Types['text/plain'].each { |t| puts t.to_a.join(", ") }
puts "\nMIME::Types['text/plain']"

one for the general case, and one for VMS systems).
either type (in the example below, 'text/plain' returns two values --
registered). It is possible for multiple matches to be returned for
objects) and :registered (finds only MIME::Types that are
flag parameters are :complete (finds only complete MIME::Type
Returns a list of MIME::Type objects, which may be empty. The optional
def [](type_id, complete: false, registered: false)
  matches =
    case type_id
    when MIME::Type
      @type_variants[type_id.simplified]
    when Regexp
      match(type_id)
    else
      @type_variants[MIME::Type.simplified(type_id)]
    end
  prune_matches(matches, complete, registered).sort { |a, b|
    a.priority_compare(b)
  }
end

def add(*types)

will suppress duplicate MIME type warnings.
The last parameter may be the value :silent or +true+ which

type is already known, a warning will be displayed.
Add one or more MIME::Type objects to the set of known types. If the
def add(*types)
  quiet = (types.last == :silent) || (types.last == true)
  types.each do |mime_type|
    case mime_type
    when true, false, nil, Symbol
      nil
    when MIME::Types
      variants = mime_type.instance_variable_get(:@type_variants)
      add(*variants.values.inject(Set.new, :+).to_a, quiet)
    when Array
      add(*mime_type, quiet)
    else
      add_type(mime_type, quiet)
    end
  end
end

def add_type(type, quiet = false)

truthy value to suppress that warning.
already known, a warning will be displayed. The +quiet+ parameter may be a
Add a single MIME::Type object to the set of known types. If the +type+ is
def add_type(type, quiet = false)
  if !quiet && @type_variants[type.simplified].include?(type)
    MIME::Types.logger.debug <<-WARNING.chomp.strip
      Type #{type} is already registered as a variant of #{type.simplified}.
    WARNING
  end
  add_type_variant!(type)
  index_extensions!(type)
end

def add_type_variant!(mime_type)

def add_type_variant!(mime_type)
  @type_variants.add(mime_type.simplified, mime_type)
end

def count

Returns the number of known type variants.
def count
  @type_variants.values.inject(0) { |a, e| a + e.size }
end

def each

Iterates through the type variants.
def each
  if block_given?
    @type_variants.each_value { |tv| tv.each { |t| yield t } }
  else
    enum_for(:each)
  end
end

def index_extensions!(mime_type)

def index_extensions!(mime_type)
  mime_type.extensions.each { |ext| @extension_index.add(ext, mime_type) }
end

def initialize

Creates a new MIME::Types registry.
def initialize
  @type_variants = Container.new
  @extension_index = Container.new
end

def inspect # :nodoc:

:nodoc:
def inspect # :nodoc:
  "#<#{self.class}: #{count} variants, #{@extension_index.count} extensions>"
end

def load_mode

def load_mode
  {columnar: false}
end

def match(pattern)

def match(pattern)
  @type_variants.select { |k, _|
    k =~ pattern
  }.values.inject(Set.new, :+)
end

def prune_matches(matches, complete, registered)

def prune_matches(matches, complete, registered)
  matches.delete_if { |e| !e.complete? } if complete
  matches.delete_if { |e| !e.registered? } if registered
  matches
end

def reindex_extensions!(mime_type)

def reindex_extensions!(mime_type)
  return unless @type_variants[mime_type.simplified].include?(mime_type)
  index_extensions!(mime_type)
end

def type_for(filename)

=> [application/xml, image/gif, text/xml]
puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif))
=> [image/gif]
puts MIME::Types.type_for('citydesk.gif')
=> [application/xml, text/xml]
puts MIME::Types.type_for('citydesk.xml')

This will always return a merged, flatten, priority sorted, unique array.

as the matching criteria on its own.
filename extension. If there is no extension, the filename will be used
Return the list of MIME::Types which belongs to the file based on its
def type_for(filename)
  Array(filename).flat_map { |fn|
    @extension_index[fn.chomp.downcase[/\.?([^.]*?)\z/m, 1]]
  }.compact.inject(Set.new, :+).sort { |a, b|
    a.priority_compare(b)
  }
end