Class: Tapout::YamlParser

Inherits:
AbstractParser show all
Defined in:
lib/tapout/parsers/yaml.rb

Overview

The TAP-Y Parser takes a TAP-Y stream and routes it through a tapout report format.

Constant Summary collapse

NEW_DOCUMENT =
/^\-\-\-/
END_DOCUMENT =
/^\.\.\.\s*$/

Constants inherited from AbstractParser

AbstractParser::PAUSE_DOCUMENT, AbstractParser::RESUME_DOCUMENT

Instance Method Summary collapse

Methods inherited from AbstractParser

#passthru

Constructor Details

#initialize(options = {}) ⇒ YamlParser

Returns a new instance of YamlParser.



16
17
18
19
20
# File 'lib/tapout/parsers/yaml.rb', line 16

def initialize(options={})
  format    = options[:format]
  @reporter = Reporters.factory(format).new
  @input    = options[:input] || $stdin
end

Instance Method Details

#consume(input = nil) ⇒ Object Also known as: read

Read from input using ‘gets` and parse, routing entries to reporter.

input - Input channel, defaults to $stdin. [#gets]

Returns reporter exit code.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tapout/parsers/yaml.rb', line 28

def consume(input=nil)
  @input = input if input

  entry = ''
  while line = @input.gets
    case line
    when PAUSE_DOCUMENT
      passthru
    when RESUME_DOCUMENT # (no effect)
    when END_DOCUMENT
      handle(entry)
      entry = passthru
    when NEW_DOCUMENT
      handle(entry)
      entry = line
    else
      entry << line
    end
  end
  handle(entry)  # in case final `...` was left out

  @reporter.finalize  #@reporter.exit_code
end

#handle(entry) ⇒ Object Also known as: <<

Handle document entry.

Returns nothing.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tapout/parsers/yaml.rb', line 58

def handle(entry)
  return if entry.empty?
  return if entry == NEW_DOCUMENT
  return if entry == RESUME_DOCUMENT

  begin
    data = YAML.load(entry)
    @reporter << data
  rescue Psych::SyntaxError
    passthru(entry)
  end
end