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. Currently uses a fixed default mapping. If the mime type is not known, it will use a fallback default handler.

The mapping can be configured in Rails’ initializer files. Example:

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

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 the class name, this will take the corresponding class from TaliaCore::DataTypes.

The loader method can be a symbol, if given it must correspond to a class method that can be called on type_class and which accepts exactly 4 parameters, which will be passed in during record creation

  • Mime type of the record

  • ‘location’ field of the record

  • The record source - this is a descriptive string with either the url or the file name from which the data should be fetched

  • The is_file flag. This will be true if the source is the name of a regular file. Otherwise, the source field should be interpreted as a URL.

Example handler method

def data_loader(mime_type, location, source, is_file)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/talia_core/data_types/mime_mapping.rb', line 43

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



15
16
17
# File 'lib/talia_core/data_types/mime_mapping.rb', line 15

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

.loader_type_from(mime_type) ⇒ Object



19
20
21
22
# File 'lib/talia_core/data_types/mime_mapping.rb', line 19

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