Class: Stupidedi::Reader::FileInput
- Inherits:
-
AbstractInput
- Object
- AbstractInput
- Stupidedi::Reader::FileInput
- Defined in:
- lib/stupidedi/reader/input/file_input.rb
Overview
This class is not thread-safe. If more than one ‘Thread` has access to the same instance, and they simultaneously call methods on that instance, the methods may produce incorrect results and the object might be left in an inconsistent state.
Querying the Position collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#line ⇒ Integer
readonly
(see AbstractInput#line).
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Attribute Summary collapse
- #io ⇒ IO readonly
Querying the Position collapse
-
#position ⇒ String
The file name, URI, etc that identifies the input stream.
Reading the Input collapse
- #at(n) ⇒ String
-
#index(value) ⇒ Integer
Returns the smallest ‘n`, where #at`(n)` == `element`.
- #take(n) ⇒ String
Advancing the Cursor collapse
-
#drop(n) ⇒ AbstractInput
Advance the cursor forward ‘n` elements.
Testing the Input collapse
Instance Method Summary collapse
-
#initialize(io, offset = 0, line = 1, column = 1, size = io.stat.size) ⇒ FileInput
constructor
A new instance of FileInput.
- #pretty_print(q) ⇒ void
Methods included from Inspect
Constructor Details
#initialize(io, offset = 0, line = 1, column = 1, size = io.stat.size) ⇒ FileInput
Returns a new instance of FileInput.
17 18 19 20 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 17 def initialize(io, offset = 0, line = 1, column = 1, size = io.stat.size) @io, @offset, @line, @column, @size = io, offset, line, column, size end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
33 34 35 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 33 def column @column end |
#io ⇒ IO (readonly)
15 16 17 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 15 def io @io end |
#line ⇒ Integer (readonly)
(see AbstractInput#line)
30 31 32 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 30 def line @line end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
26 27 28 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 26 def offset @offset end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
36 37 38 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 36 def path @path end |
Instance Method Details
#at(n) ⇒ String
58 59 60 61 62 63 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 58 def at(n) raise ArgumentError, "n must be positive" unless n >= 0 @io.seek(@offset + n) @io.read(1) end |
#defined_at?(n) ⇒ Boolean
113 114 115 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 113 def defined_at?(n) n < @size end |
#drop(n) ⇒ AbstractInput
Advance the cursor forward ‘n` elements
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 87 def drop(n) raise ArgumentError, "n must be positive" unless n >= 0 @io.seek(@offset) prefix = @io.read(n) suffix = @io length = prefix.length count = prefix.count("\n") column = unless count.zero? length - prefix.rindex("\n") else @column + length end copy(:offset => @offset + length, :line => @line + count, :column => column, :size => @size - length) end |
#empty? ⇒ Boolean
118 119 120 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 118 def empty? @io.eof? end |
#index(value) ⇒ Integer
Returns the smallest ‘n`, where #at`(n)` == `element`
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 66 def index(value) @io.seek(@offset) length = value.length # We need to start with value != buffer, and this is a reasonable guess buffer = "\377" * length until @io.eof? buffer.slice!(0) buffer << @io.read(1) if buffer == value return @io.tell - @offset - length end end end |
#position ⇒ String
The file name, URI, etc that identifies the input stream
39 40 41 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 39 def position Position.new(@offset, @line, @column, @io.path) end |
#pretty_print(q) ⇒ void
This method returns an undefined value.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 126 def pretty_print(q) q.text("FileInput") q.group(2, "(", ")") do preview = take(4) preview = if preview.empty? "EOF" elsif preview.length <= 3 preview.inspect else (preview.take(3) << "...").inspect end q.text preview q.text " at line #{@line}, column #{@column}, offset #{@offset}, file #{File.basename(@io.path)}" end end |
#take(n) ⇒ String
48 49 50 51 52 53 54 |
# File 'lib/stupidedi/reader/input/file_input.rb', line 48 def take(n) @io.seek(@offset) # Calling @io.read with more than the number of available bytes will # return nil, so we have to calculate how many bytes remain @io.read((n <= @size) ? n : @size) end |