Class: Bashcov::FieldStream
- Inherits:
-
Object
- Object
- Bashcov::FieldStream
- Defined in:
- lib/bashcov/field_stream.rb
Overview
Classes for streaming token-delimited fields
Instance Attribute Summary collapse
-
#read ⇒ Object
Returns the value of attribute read.
Instance Method Summary collapse
-
#each(delimiter, field_count, start_match) {|field| ... } ⇒ Object
Yields fields extracted from an input stream.
-
#each_field(delimiter) {|field| ... } ⇒ void
A convenience wrapper around each_line(delimiter) that also does chomp(delimiter) on the yielded line.
-
#initialize(read = nil) ⇒ FieldStream
constructor
A new instance of FieldStream.
Constructor Details
#initialize(read = nil) ⇒ FieldStream
Returns a new instance of FieldStream.
9 10 11 |
# File 'lib/bashcov/field_stream.rb', line 9 def initialize(read = nil) @read = read end |
Instance Attribute Details
#read ⇒ Object
Returns the value of attribute read.
6 7 8 |
# File 'lib/bashcov/field_stream.rb', line 6 def read @read end |
Instance Method Details
#each(delimiter, field_count, start_match) {|field| ... } ⇒ Object
Yields fields extracted from an input stream
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bashcov/field_stream.rb', line 36 def each(delimiter, field_count, start_match, &block) return enum_for(__method__, delimiter, field_count, start_match) unless block_given? chunked = each_field(delimiter).chunk(&chunk_matches(start_match)) yield_fields = lambda do |(_, chunk)| chunk.each(&block) (field_count - chunk.size).times { yield "" } end # Skip junk that might appear before the first start-of-fields match begin n, chunk = chunked.next yield_fields.call([n, chunk]) unless n.zero? rescue StopIteration return end chunked.each(&yield_fields) end |
#each_field(delimiter) {|field| ... } ⇒ void
This method returns an undefined value.
A convenience wrapper around each_line(delimiter) that also does chomp(delimiter) on the yielded line.
18 19 20 21 22 23 24 |
# File 'lib/bashcov/field_stream.rb', line 18 def each_field(delimiter) return enum_for(__method__, delimiter) unless block_given? read.each_line(delimiter) do |line| yield line.chomp(delimiter).encode("utf-8", invalid: :replace) end end |