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 a 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 a input stream
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bashcov/field_stream.rb', line 36 def each(delimiter, field_count, start_match) return enum_for(__method__, delimiter, field_count, start_match) unless block_given? # Whether the current field is the start-of-fields match matched_start = nil # The number of fields processed since passing the last start-of-fields # match seen_fields = 0 fields = each_field(delimiter) # Close over +field_count+ and +seen_fields+ to yield empty strings to # the caller when we've already hit the next start-of-fields match yield_remaining = -> { (field_count - seen_fields).times { yield "" } } # Advance until the first start-of-fields match loop { break if fields.next =~ start_match } fields.each do |field| # If the current field is the start-of-fields match... if field =~ start_match # Fill out any remaining (unparseable) fields with empty strings yield_remaining.call matched_start = nil seen_fields = 0 elsif seen_fields < field_count yield field seen_fields += 1 end end # One last filling-out of empty fields if we're at the end of the stream yield_remaining.call read.close unless read.closed? 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 |