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

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

Overview

Implementas a JSON parser.

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.



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

def initialize(source, filename) # rubocop:disable Lint/MissingSuper
  @file = filename
  @source = source
  @statements = []
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 7

def file
  @file
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 7

def source
  @source
end

Instance Method Details

#enumeratorObject



19
20
21
# File 'lib/puppet-strings/yard/parsers/json/parser.rb', line 19

def enumerator
  @statements
end

#parsevoid

This method returns an undefined value.

Parses the source



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

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 StandardError
    log.error "Failed to parse #{@file}: "
    @statements = []
  end
  @statements.freeze
  self
end