Class: Kotoshu::Readers::FileReader
- Inherits:
-
Object
- Object
- Kotoshu::Readers::FileReader
- Defined in:
- lib/kotoshu/readers/file_reader.rb
Overview
Base reader class for reading files line by line.
This class provides:
-
Line-by-line reading with line numbers
-
BOM (byte-order mark) handling
-
Comment stripping
-
Empty line filtering
Direct Known Subclasses
Constant Summary collapse
- UTF8_BOM =
BOM (byte-order mark) for UTF-8
"\xEF\xBB\xBF".freeze
Instance Attribute Summary collapse
-
#encoding ⇒ String
readonly
The encoding.
-
#line_no ⇒ Integer
readonly
Current line number.
-
#path ⇒ String
readonly
The file path.
Instance Method Summary collapse
-
#close ⇒ Object
Close the file.
-
#each {|Integer, String| ... } ⇒ Enumerator
Iterate over lines.
-
#has_next? ⇒ Boolean
Check if there are more lines.
-
#initialize(path, encoding = 'UTF-8') ⇒ FileReader
constructor
Create a new file reader.
-
#next ⇒ Array<Integer, String>
Get next line.
-
#peek ⇒ Array<Integer, String>
Peek at next line without consuming it.
-
#reset ⇒ Object
Reset the reader to the beginning.
-
#reset_encoding(new_encoding) ⇒ Object
Reset encoding and reopen file.
-
#to_a ⇒ Array<Array<Integer, String>>
Get all lines as an array.
Constructor Details
#initialize(path, encoding = 'UTF-8') ⇒ FileReader
Create a new file reader.
44 45 46 47 48 49 50 51 |
# File 'lib/kotoshu/readers/file_reader.rb', line 44 def initialize(path, encoding = 'UTF-8') @path = path @encoding = encoding @line_no = 0 @file = nil @iterator = nil reset_io end |
Instance Attribute Details
#encoding ⇒ String (readonly)
Returns The encoding.
32 33 34 |
# File 'lib/kotoshu/readers/file_reader.rb', line 32 def encoding @encoding end |
#line_no ⇒ Integer (readonly)
Returns Current line number.
35 36 37 |
# File 'lib/kotoshu/readers/file_reader.rb', line 35 def line_no @line_no end |
#path ⇒ String (readonly)
Returns The file path.
29 30 31 |
# File 'lib/kotoshu/readers/file_reader.rb', line 29 def path @path end |
Instance Method Details
#close ⇒ Object
Close the file.
111 112 113 114 |
# File 'lib/kotoshu/readers/file_reader.rb', line 111 def close @file&.close @file = nil end |
#each {|Integer, String| ... } ⇒ Enumerator
Iterate over lines.
67 68 69 70 71 |
# File 'lib/kotoshu/readers/file_reader.rb', line 67 def each return enum_for(:each) unless block_given? @iterator.each { |line_no, line| yield(line_no, line) } end |
#has_next? ⇒ Boolean
Check if there are more lines.
83 84 85 86 87 88 |
# File 'lib/kotoshu/readers/file_reader.rb', line 83 def has_next? peek true rescue StopIteration false end |
#next ⇒ Array<Integer, String>
Get next line.
100 101 102 |
# File 'lib/kotoshu/readers/file_reader.rb', line 100 def next @iterator.next end |
#peek ⇒ Array<Integer, String>
Peek at next line without consuming it.
93 94 95 |
# File 'lib/kotoshu/readers/file_reader.rb', line 93 def peek @iterator.peek end |
#reset ⇒ Object
Reset the reader to the beginning.
105 106 107 108 |
# File 'lib/kotoshu/readers/file_reader.rb', line 105 def reset @line_no = 0 reset_io end |
#reset_encoding(new_encoding) ⇒ Object
Reset encoding and reopen file.
56 57 58 59 60 61 |
# File 'lib/kotoshu/readers/file_reader.rb', line 56 def reset_encoding(new_encoding) @encoding = new_encoding @line_no = 0 @file&.close reset_io end |
#to_a ⇒ Array<Array<Integer, String>>
Get all lines as an array.
76 77 78 |
# File 'lib/kotoshu/readers/file_reader.rb', line 76 def to_a @iterator.to_a end |