Class: Myasorubka::AOT::Dictionary::Section

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/myasorubka/aot/dictionary.rb

Overview

MRD file section handling helper class.

Each section is a set of records, one per line. The number of all records of the section is written in the very beginning of the section ata separate line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, offset, &block) ⇒ Section

:nodoc:



43
44
45
46
47
# File 'lib/myasorubka/aot/dictionary.rb', line 43

def initialize(lines, offset, &block)
  @lines, @offset = lines, offset
  @length = lines[offset].strip.to_i
  @parser = block || proc { |s| s }
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



40
41
42
# File 'lib/myasorubka/aot/dictionary.rb', line 40

def length
  @length
end

#linesObject (readonly)

Returns the value of attribute lines.



40
41
42
# File 'lib/myasorubka/aot/dictionary.rb', line 40

def lines
  @lines
end

#offsetObject (readonly)

Returns the value of attribute offset.



40
41
42
# File 'lib/myasorubka/aot/dictionary.rb', line 40

def offset
  @offset
end

#parserObject (readonly)

Returns the value of attribute parser.



40
41
42
# File 'lib/myasorubka/aot/dictionary.rb', line 40

def parser
  @parser
end

Instance Method Details

#[](id) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
# File 'lib/myasorubka/aot/dictionary.rb', line 50

def [] id
  if id < 0 or id >= length
    raise ArgumentError,
      'invalid id=%d when offset=%d and length=%d' %
      [id, offset, length]
  end

  parser.call(lines[offset + 1 + id].strip)
end

#each(&block) ⇒ Object

:nodoc:



61
62
63
# File 'lib/myasorubka/aot/dictionary.rb', line 61

def each(&block)
  length.times { |id| block.call self[id] }
end

#inspectObject

:nodoc:



66
67
68
# File 'lib/myasorubka/aot/dictionary.rb', line 66

def inspect
  '#<%s offset=%d length=%d>' % [self.class.name, offset, length]
end