Class: Lab419::Config::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/lab419/config/source.rb

Overview

Source is a wrapper arround an array of lines, adding the ability to store the current line_number #line_nb and the name of the source #name. It implements a minimal interface, allowing to advance line by line via the #next method, accessing the current element via #current and testing for the end of the array via the #eos? method

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/lab419/config/source.rb', line 13

def name
  @name
end

Instance Method Details

#currentObject

accesses the current element



21
22
23
# File 'lib/lab419/config/source.rb', line 21

def current
  @lines[ @cursor ]
end

#eos?Boolean

true iff #next has advanced over the last line

Returns:

  • (Boolean)


26
27
28
# File 'lib/lab419/config/source.rb', line 26

def eos?
  @lines[ @cursor ].nil?
end

#line_nbObject

current line number, starting with 1, incremented by each call of #next, up to a maximum of the size of the underlying array.



33
34
35
# File 'lib/lab419/config/source.rb', line 33

def line_nb
  [ @cursor + 1, @lines.length ].min
end

#nextObject

This is the only modifier of Source, it advances to the next line of the underlying array. The operation is irreversible.



40
41
42
43
# File 'lib/lab419/config/source.rb', line 40

def next
  @cursor += 1
  current
end

#to_debugObject



45
46
47
# File 'lib/lab419/config/source.rb', line 45

def to_debug
  "#{@lines[@cursor-1]}:>#{current}<:#{@lines[@cursor+1]}"
end