| Class | TaliaCore::DataTypes::MimeMapping |
| In: |
lib/talia_core/data_types/mime_mapping.rb
|
| Parent: | Object |
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.
See the source code of the mapping_hash method for the detailed default mapping. It goes something like this:
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
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.
# 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)
# File lib/talia_core/data_types/mime_mapping.rb, line 64
64: def add_mapping(mime_type, data_class, handler = nil)
65: mapping = {}
66: if(!data_class.is_a?(Class))
67: data_class = TaliaCore::DataTypes.const_get(data_class.to_s.camelize)
68: end
69:
70: raise("Error: #{data_class} is not a valid data class.") unless(data_class.is_a?(Class) && (data_class <= DataRecord))
71:
72: mapping[:type] = data_class
73: mapping[:loader] = handler.to_sym if(handler)
74: mapping_hash[symbol_for(mime_type)] = mapping
75: true
76: end
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)
# File lib/talia_core/data_types/mime_mapping.rb, line 39
39: def class_type_from(mime_type)
40: mapping_for(mime_type)[:type]
41: end
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).
# File lib/talia_core/data_types/mime_mapping.rb, line 46
46: def loader_type_from(mime_type)
47: map = mapping_for(mime_type)
48: map[:loader] || map[:type]
49: end
Returns the current mapping. This will be automatically initialized with the default mappings.
# File lib/talia_core/data_types/mime_mapping.rb, line 80
80: def mapping_hash
81: @mapping ||= {
82: :xml => { :type => DataTypes::XmlData },
83: :html =>{ :type => DataTypes::XmlData },
84: :tei => { :type => DataTypes::XmlData },
85: :tei_p5 => { :type => DataTypes::XmlData },
86: :tei_p4 => { :type => DataTypes::XmlData },
87: :gml => { :type => DataTypes::XmlData },
88: :wittei => { :type => DataTypes::XmlData },
89: :hnml => { :type => DataTypes::XmlData },
90: :jpeg => { :type => DataTypes::ImageData },
91: :tiff => { :type => DataTypes::ImageData },
92: :png => { :type => DataTypes::ImageData },
93: :gif => { :type => DataTypes::ImageData },
94: :bmp => { :type => DataTypes::ImageData },
95: :pdf => { :type => DataTypes::PdfData },
96: :text => { :type => DataTypes::SimpleText },
97: # Default fallback handler
98: :default => { :type => FileRecord }
99: }
100: end