Class: TaliaCore::DataTypes::MimeMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_core/data_types/mime_mapping.rb

Overview

Mapping from Mime types to data classes and importing methods for DataRecord.

See the DataTypes::DataLoader module to see how the import works. In a nutshell, each MIME type can be connected either to a DataTypes::FileRecord type that will be used for new data records, or the MIME type can be connected to a handler method that will do the creation.

Default Mappings

See the source code of the mapping_hash method for the detailed default mapping. It goes something like this:

  • All image types (:jpeg, :tiff, :png, :gif, …) use DataTypes::ImageData

  • HTML, XML and all “transcription” types (:html, :xml, :tei, …) use DataTypes::XmlData

  • :text uses DataTypes::SimpleText

  • :pdf uses DataTypes::PdfData

  • The default (for unknow types) is to use DataTypes::FileRecord

Configure the MIME mappings for Talia

The mapping can be configured in Rails’ initializer files (e.g. config/initializers/talia.rb):

TaliaCore::DataTypes::MimeMapping.add_mapping(:tiff, :image_data, :create_iip)

Add a mapping for each MIME type that you need, or where you want to change the default mapping

Class Method Summary collapse

Class Method Details

.add_mapping(mime_type, data_class, handler = nil) ⇒ Object

Set a new mapping for the given MIME type. If only a class is given, this will use the class to create new data records from. If a symbol is given for data_class, this will take the corresponding class from TaliaCore::DataTypes.

Examples

# Uses DataTypes::ImageData#create_iip to create new records
TaliaCore::DataTypes::MimeMapping.add_mapping(:tiff, :image_data, :create_iip)

# Use the DataTypes::ImageData class for new data records, and create records
# in the default way (using create_with_file or similar)
TaliaCore::DataTypes::MimeMapping.add_mapping(:png, DataTypes::ImageData)


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/talia_core/data_types/mime_mapping.rb', line 64

def add_mapping(mime_type, data_class, handler = nil)
  mapping = {}
  if(!data_class.is_a?(Class))
    data_class = TaliaCore::DataTypes.const_get(data_class.to_s.camelize)
  end
  
  raise("Error: #{data_class} is not a valid data class.") unless(data_class.is_a?(Class) && (data_class <= DataRecord))
  
  mapping[:type] = data_class
  mapping[:loader] = handler.to_sym if(handler)
  mapping_hash[symbol_for(mime_type)] = mapping
  true
end

.class_type_from(mime_type) ⇒ Object

Gets the data class for the given mime type. For loaders configured through add_mapping, this will always return the class corresponding to data_class. (Otherwise it will return the data_class configured in the default mapping)



39
40
41
# File 'lib/talia_core/data_types/mime_mapping.rb', line 39

def class_type_from(mime_type)
  mapping_for(mime_type)[:type]
end

.loader_type_from(mime_type) ⇒ Object

Return the “loader type” for the given MIME type. This will return the handler (as a symbol, see add_mapping) if set. If no handler is set, it will return the data_class (as class_type_from).



46
47
48
49
# File 'lib/talia_core/data_types/mime_mapping.rb', line 46

def loader_type_from(mime_type)
  map = mapping_for(mime_type)
  map[:loader] || map[:type]
end

.mapping_hashObject

Returns the current mapping. This will be automatically initialized with the default mappings.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/talia_core/data_types/mime_mapping.rb', line 80

def mapping_hash
  @mapping ||= {
    :xml => { :type => DataTypes::XmlData },
    :html =>{ :type => DataTypes::XmlData },
    :tei => { :type => DataTypes::XmlData },
    :tei_p5 => { :type => DataTypes::XmlData },
    :tei_p4 => { :type => DataTypes::XmlData }, 
    :gml => { :type => DataTypes::XmlData },
    :wittei => { :type => DataTypes::XmlData },
    :hnml => { :type => DataTypes::XmlData },
    :jpeg => { :type => DataTypes::ImageData },
    :tiff => { :type => DataTypes::ImageData },
    :png => { :type => DataTypes::ImageData },
    :gif => { :type => DataTypes::ImageData },
    :bmp => { :type => DataTypes::ImageData },
    :pdf => { :type => DataTypes::PdfData },
    :text => { :type => DataTypes::SimpleText },            
    # Default fallback handler
    :default => { :type => FileRecord }
  }
end