Class: DRG::FileReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/drg/file_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileReader

Returns a new instance of FileReader.



11
12
13
# File 'lib/drg/file_reader.rb', line 11

def initialize(file_path)
  @file_path = file_path
end

Instance Method Details

#contextObject



40
41
42
# File 'lib/drg/file_reader.rb', line 40

def context
  @context ||= FileContext.new
end

#eachObject



15
16
17
18
19
20
21
22
# File 'lib/drg/file_reader.rb', line 15

def each
  return to_enum unless block_given?
  File.open(@file_path) do |f|
    while line = f.gets
      yield line
    end
  end
end

#each_with_contextObject



24
25
26
27
28
29
30
31
# File 'lib/drg/file_reader.rb', line 24

def each_with_context
  return enum_for __method__ unless block_given?
  @context = nil
  each_with_index do |line, i|
    current_line = context.add(line, i)
    yield current_line, context
  end
end

#readObject



33
34
35
36
37
38
# File 'lib/drg/file_reader.rb', line 33

def read
  @context = nil
  each_with_index do |line, i|
    context.add(line, i)
  end
end