Class: IOSegmenter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/io-segmenter.rb

Overview

IO iterator for parsing segments of streams

Constant Summary collapse

DEFAULT_READ_SIZE =
8192

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, starting_char, ending_char, quote_char, escape_char, max_read_size = DEFAULT_READ_SIZE) ⇒ IOSegmenter

Returns a new instance of IOSegmenter.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/io-segmenter.rb', line 9

def initialize(io, starting_char, ending_char, quote_char, escape_char, max_read_size=DEFAULT_READ_SIZE)
  @io = io
  @starting_char = starting_char
  @ending_char = ending_char
  @quote_char = quote_char
  @escape_char = escape_char
  @max_read_size = max_read_size

  terms = [
    @starting_char,
    @ending_char,
    @quote_char,
    @escape_char
  ]
  terms.compact!
  terms.map! { |str| Regexp.escape(str) }

  @search = Regexp.new('(:?' + terms.join('|') + ')')
end

Class Method Details

.foreach(*args, &block) ⇒ Object



40
41
42
# File 'lib/io-segmenter.rb', line 40

def self.foreach(*args, &block)
  new(*args).each(&block)
end

Instance Method Details

#eachObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/io-segmenter.rb', line 29

def each
  buffer = String.new

  until @io.eof?
    buffer << @io.read(@max_read_size)
    each_segment(buffer) do |segment|
      yield segment
    end
  end
end