Class: M2Config::MimeType

Inherits:
Object
  • Object
show all
Defined in:
lib/m2config/mimetype.rb

Constant Summary collapse

MONGREL2_PREFS =
YAML.load File.read "#{File.dirname(__FILE__)}/mongrel2.mime.yml"
RECESSIVE =
YAML.load File.read "#{File.dirname(__FILE__)}/mongrel2.mime.yml"
SQL_FIND_DUPLICATES =
<<-SQL
select mimetype, extension, count(*)
from mimetype
group by extension
having count(*)>1
SQL

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ MimeType

Returns a new instance of MimeType.

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
# File 'lib/m2config/mimetype.rb', line 67

def initialize(fields)
  raise ArgumentError, "Extension must start with a ." unless fields[:extension] =~ /^\./
  type = M2Config::MimeType[extension:fields[:extension]]
  raise ArgumentError, "extension #{fields[:extension]} is already covered by #{type.mimetype} type" if type
  super(fields, false)
  save
end

Class Method Details

.not_dominant?(mtype) ⇒ Boolean

Is it reasonable to ignore this type ?

Returns:

  • (Boolean)


38
39
40
# File 'lib/m2config/mimetype.rb', line 38

def self.not_dominant?(mtype)
  mtype.obsolete? || superceded?(mtype)  || !simplest?(mtype)
end

.populate_table(types = nil, ignoreDoubles = false) ⇒ Object

Raises:

  • (RuntimeError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/m2config/mimetype.rb', line 16

def self.populate_table(types=nil, ignoreDoubles=false)
  raise RuntimeError, "Table must be empty" if db[:mimetype].count > 0
  types ||= MIME::Types
  rows = [] # Will collect ext<->type rows
  types.each {
    |type|
    next if not_dominant?(type)
    type.extensions.each {
      |ext|
      ext = "."+ext
      clashingType = M2Config::MimeType[extension:ext]
      raise ArgumentError, "extension #{ext} is already covered by #{clashingType.mimetype} type" if clashingType
      rows << [type.content_type, ext]
    }
  }
  db.transaction {
    db[:mimetype].import([:mimetype, :extension], rows)
  }
  remove_duplicates unless ignoreDoubles
end

.remove_duplicatesObject

Raises:

  • (RuntimeError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/m2config/mimetype.rb', line 50

def self.remove_duplicates
  randomChoices = []
  db[SQL_FIND_DUPLICATES].all.each {
    |r|
    ext = r[:extension]
    preferred = MONGREL2_PREFS[ext]
    if preferred
      db[:mimetype].where(extension:ext).delete
      db[:mimetype].insert(extension:ext, mimetype:preferred)
    else
      raise ArgumentError, "#{ext} (#{r[:mimetype]}) has multiple content types but no Mongrel2 preference"
    end
  }
  raise RuntimeError, "Still duplicates after removing duplicates!" if
    db[SQL_FIND_DUPLICATES].all.size > 0
end

.simplest?(mtype) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/m2config/mimetype.rb', line 46

def self.simplest?(mtype)
  mtype.content_type == mtype.simplified
end

.superceded?(mtype) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/m2config/mimetype.rb', line 42

def self.superceded?(mtype)
  mtype.docs =~ /instead/
end