Class: TaliaCore::DataTypes::DataRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/talia_core/data_types/data_record.rb

Overview

Base class for all data records in Talia. This only contains a basic interface, without much functionality. All data-related methods will return a NotImplementedError

The DataRecord provides an interface to access a generic array/buffer of bytes, with the base class not making any assumptions on how these bytes are stored.

Subclasses should usually provide the inferface of this class, which is more or less like the standard file interface.

Each data record has a “location” field, which is roughly equivalent to the file name, and a MIME type. The default behaviour is that, if not set manually, the MIME type is automatically set before saving. It will be determined by the “file extension” of the location field.

Each data record must belong to an ActiveSource. For more information on how to handle records with files, see the FileRecord class

Direct Known Subclasses

FileRecord, MediaLink

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#temp_pathObject

Returns the value of attribute temp_path.



83
84
85
# File 'lib/talia_core/data_types/data_record.rb', line 83

def temp_path
  @temp_path
end

Class Method Details

.find_by_type_and_location!(source_data_type, location) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/talia_core/data_types/data_record.rb', line 93

def find_by_type_and_location!(source_data_type, location)
  # TODO: Should it directly instantiate the STI sub-class?
  # In this case we should use the following line instead.
  #
  # source_data = source_data_type.classify.constantize.find_by_location(location, :limit => 1)
  #
  data_type = "TaliaCore::DataTypes::#{source_data_type.camelize}"
  source_data = self.find(:first, :conditions => ["type = ? AND location = ?", data_type, location])
  
  raise ActiveRecord::RecordNotFound if source_data.nil?
  source_data
end

.find_data_records(id) ⇒ Object

Find all data records about a specified source



89
90
91
# File 'lib/talia_core/data_types/data_record.rb', line 89

def find_data_records(id)
  find(:all, :conditions => { :source_id => id })
end

Instance Method Details

#all_bytesObject

returns all bytes in the object as an array of unsigned integers

Raises:

  • (NotImplementedError)


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

def all_bytes
  raise NotImplementedError
end

#content_stringObject

Returns all_bytes as an binary string



44
45
46
# File 'lib/talia_core/data_types/data_record.rb', line 44

def content_string
  all_bytes.pack('C*') if(all_bytes)
end

#extract_mime_type(location) ⇒ Object



72
73
74
75
76
77
# File 'lib/talia_core/data_types/data_record.rb', line 72

def extract_mime_type(location)
  # Lookup the mime type for the extension (removing the dot
  # in front of the file extension) Works only for the file
  # types supported by Rails' Mime class.
  Mime::Type.lookup_by_extension((File.extname(location).downcase)[1..-1]).to_s
end

#get_byte(close_after_single_read = false) ⇒ Object

returns the next byte from the object, or nil at EOS

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/talia_core/data_types/data_record.rb', line 49

def get_byte(close_after_single_read=false)
  raise NotImplementedError
end

#mime_typeObject



79
80
81
# File 'lib/talia_core/data_types/data_record.rb', line 79

def mime_type
  self.mime
end

#positionObject

returns the current position of the read cursor

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/talia_core/data_types/data_record.rb', line 54

def position
  raise NotImplementedError
end

#resetObject

reset the cursor to the initial state



69
70
# File 'lib/talia_core/data_types/data_record.rb', line 69

def reset
end

#seek(new_position) ⇒ Object

adjust the position of the read cursor

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/talia_core/data_types/data_record.rb', line 59

def seek(new_position)
  raise NotImplementedError
end

#sizeObject

returns the size of the object in bytes

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/talia_core/data_types/data_record.rb', line 64

def size
  raise NotImplementedError
end