Class: Qwirk::Batch::ParseFileStrategy
- Inherits:
-
Object
- Object
- Qwirk::Batch::ParseFileStrategy
- Defined in:
- lib/qwirk/batch/parse_file_strategy.rb
Overview
Default strategy for parsing a file
Instance Method Summary collapse
-
#close ⇒ Object
Close the file.
-
#file_position ⇒ Object
Return the current position in the file.
-
#file_position=(line_count) ⇒ Object
Goto a specific position in the file.
-
#initialize(file_options) ⇒ ParseFileStrategy
constructor
A new instance of ParseFileStrategy.
-
#next_record ⇒ Object
Read the next record from the file.
-
#open(file) ⇒ Object
Open the file for processing.
-
#record_total ⇒ Object
Return an estimate of the total records in the file.
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() end |
Instance Method Details
#close ⇒ Object
Close the file
43 44 45 |
# File 'lib/qwirk/batch/parse_file_strategy.rb', line 43 def close @fin.close end |
#file_position ⇒ Object
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_record ⇒ Object
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_total ⇒ Object
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 |