Class: PuppetStrings::Yard::Parsers::JSON::Parser

Inherits:
YARD::Parser::Base
  • Object
show all
Defined in:
lib/puppet-strings/yard/parsers/json/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename) ⇒ void

Initializes the parser.

Parameters:

  • source (String)

    The source being parsed.

  • filename (String)

    The file name of the file being parsed.



10
11
12
13
14
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 10

def initialize(source, filename)
  @file = filename
  @source = source
  @statements = []
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 4

def file
  @file
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 4

def source
  @source
end

Instance Method Details

#enumeratorObject



16
17
18
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 16

def enumerator
  @statements
end

#parsevoid

This method returns an undefined value.

Parses the source



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 22

def parse
  begin
    json = JSON.parse(source)
    # TODO: this should compare json to a Task metadata json-schema or perform some other hueristics
    # to determine what type of statement it represents
    @statements.push(PuppetStrings::Yard::Parsers::JSON::TaskStatement.new(json, @source, @file)) unless json.empty?
  rescue
    log.error "Failed to parse #{@file}: "
    @statements = []
  end
  @statements.freeze
  self
end