Class: Caf::Chunk::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/caf/chunk/base.rb

Direct Known Subclasses

AudioDescription, Data, Free, MagicCookie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk_header) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/caf/chunk/base.rb', line 16

def initialize(chunk_header)
  @chunk_type = chunk_header[:chunk_type]
  @chunk_size = chunk_header[:chunk_size]
end

Instance Attribute Details

#chunk_sizeObject (readonly)

Returns the value of attribute chunk_size.



14
15
16
# File 'lib/caf/chunk/base.rb', line 14

def chunk_size
  @chunk_size
end

#chunk_typeObject (readonly)

Returns the value of attribute chunk_type.



14
15
16
# File 'lib/caf/chunk/base.rb', line 14

def chunk_type
  @chunk_type
end

Class Method Details

.implements(chunk_type) ⇒ Object



10
11
12
# File 'lib/caf/chunk/base.rb', line 10

def self.implements(chunk_type)
  Caf::Chunk.available_chunk_types[chunk_type] = self
end

Instance Method Details

#check_type_and_size(expected_klass, expected_chunk_type) ⇒ Object

Raises:



38
39
40
41
# File 'lib/caf/chunk/base.rb', line 38

def check_type_and_size(expected_klass, expected_chunk_type)
  raise(Caf::Error, "expected an #{expected_chunk_type} chunk") unless self.instance_of?(expected_klass)
  raise(Caf::Error, "corrupted chunk: wrong size (expected #{fields_size}, got #{chunk_size}") if fields_size != chunk_size
end

#fields_sizeObject

Raises:



30
31
32
# File 'lib/caf/chunk/base.rb', line 30

def fields_size
  raise(Caf::Error, "chunk must implement fields_size method: #{chunk_type}") unless self.instance_of?(Base)
end

#read_data(file) ⇒ Object



34
35
36
# File 'lib/caf/chunk/base.rb', line 34

def read_data(file)
  file.seek(chunk_size, IO::SEEK_CUR) unless chunk_size == -1
end

#to_sObject



21
22
23
# File 'lib/caf/chunk/base.rb', line 21

def to_s
  "#{chunk_type} (unknown chunk)"
end

#validateObject

Raises:



25
26
27
28
# File 'lib/caf/chunk/base.rb', line 25

def validate
  raise(Caf::Error, "chunk must implement validate method: #{chunk_type}") unless self.instance_of?(Base)
  true
end