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

Constructor Details

#initialize(options = {}) ⇒ YamlParser

Returns a new instance of YamlParser.



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

def initialize(options={})
  format    = options[:format]
  input     = options[:input]

  @reporter = Reporters.factory(format).new
  @input    = input || $stdin

  @resume = NEW_DOCUMENT
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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tapout/parsers/yaml.rb', line 32

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

  entry = ''
  while line = @input.gets
    case line
    when PAUSE_DOCUMENT
      @resume = RESUME_DOCUMENT
      passthru
    when RESUME_DOCUMENT # (no effect)
    when END_DOCUMENT
      handle(entry)
      @resume = NEW_DOCUMENT
      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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tapout/parsers/yaml.rb', line 65

def handle(entry)
  return if entry == RESUME_DOCUMENT

  stripped = entry.strip
  return if stripped.empty?
  return if stripped == "---"

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

#passthru(doc = nil) ⇒ Object

Passthru incoming data directly to ‘$stdout`.



85
86
87
88
89
90
91
92
# File 'lib/tapout/parsers/yaml.rb', line 85

def passthru(doc=nil)
  $stdout << doc if doc
  while line = @input.gets
    return line if @resume === line
    $stdout << line
  end
  return ''
end