Class: Bparity::Corpus::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bparity/corpus.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Returns a new instance of Reader.



28
29
30
# File 'lib/bparity/corpus.rb', line 28

def initialize(path)
  @path = path
end

Instance Method Details

#eachObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bparity/corpus.rb', line 32

def each
  return enum_for(__method__) unless block_given?

  File.foreach(@path).with_index(1) do |line, number|
    yield JSON.parse(line)
  rescue JSON::ParserError => e
    raise Error, "Invalid JSONL at #{@path}:#{number}: #{e.message}. Record the corpus again."
  end
rescue Errno::ENOENT
  raise Error, "Cannot read corpus #{@path}. Run `bparity record` first."
end