Class: Bashcov::FieldStream

Inherits:
Object
  • Object
show all
Defined in:
lib/bashcov/field_stream.rb

Overview

Classes for streaming token-delimited fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read = nil) ⇒ FieldStream

Returns a new instance of FieldStream.

Parameters:

  • read (IO) (defaults to: nil)

    an IO object opened for reading



9
10
11
# File 'lib/bashcov/field_stream.rb', line 9

def initialize(read = nil)
  @read = read
end

Instance Attribute Details

#readObject

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

Parameters:

  • delimiter (String, nil)

    the field separator

  • field_count (Integer)

    the number of fields to extract

  • start_match (Regexp)

    a Regexp that, when matched against the input stream, signifies the beginning of the next series of fields to yield

Yield Parameters:

  • field (String)

    each field extracted from the stream. If start_match is matched with fewer than field_count fields yielded since the last match, yields empty strings until field_count is reached.



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.

Parameters:

  • delimiter (String, nil)

    the field separator for the stream

Yield Parameters:

  • field (String)

    each chomped 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