Class: Qwirk::Batch::ParseFileStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/qwirk/batch/parse_file_strategy.rb

Overview

Default strategy for parsing a file

Instance Method Summary collapse

Constructor Details

#initialize(file_options) ⇒ ParseFileStrategy

Returns a new instance of ParseFileStrategy.



5
6
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 5

def initialize(file_options)
end

Instance Method Details

#closeObject

Close the file



43
44
45
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 43

def close
  @fin.close
end

#file_positionObject

Return the current position in the file



26
27
28
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 26

def file_position
  @line_count
end

#file_position=(line_count) ⇒ Object

Goto a specific position in the file. This strategy uses line_counts. @fin.seek and @fin.tell would be faster but wouldn’t allow the file to be edited if it was formatted incorrectly.



17
18
19
20
21
22
23
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 17

def file_position=(line_count)
  if @line_count > line_count
    @fin.seek(0)
    @line_count = 0
  end
  next_record while @line_count < line_count
end

#next_recordObject

Read the next record from the file



31
32
33
34
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 31

def next_record
  @line_count += 1
  @fin.gets
end

#open(file) ⇒ Object

Open the file for processing



9
10
11
12
13
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 9

def open(file)
  @file = file
  @fin = File.open(@file, 'r')
  @line_count = 0
end

#record_totalObject

Return an estimate of the total records in the file



37
38
39
40
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 37

def record_total
  # Faster than reading file in ruby but won't count incomplete lines
  %x{wc -l #{@file}}.split.first.to_i
end