Class: PuppetStrings::Yard::Parsers::Puppet::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-strings/yard/parsers/puppet/statement.rb

Overview

Represents the base Puppet language statement.

Direct Known Subclasses

ParameterizedStatement

Constant Summary collapse

COMMENT_REGEX =

The pattern for parsing docstring comments.

/^\s*#+\s?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, file) ⇒ Statement

Initializes the Puppet language statement.

Parameters:

  • object

    The Puppet parser model object for the statement.

  • file (String)

    The file name of the file containing the statement.



19
20
21
22
23
24
25
26
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 19

def initialize(object, file)
  @file = file

  adapter = ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(object)
  @source = adapter.extract_text
  @line = adapter.line
  @comments_range = nil
end

Instance Attribute Details

#comments_rangeObject (readonly)

Returns the value of attribute comments_range.



14
15
16
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 14

def comments_range
  @comments_range
end

#docstringObject (readonly)

Returns the value of attribute docstring.



13
14
15
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 13

def docstring
  @docstring
end

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 11

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



12
13
14
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 12

def line
  @line
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 10

def source
  @source
end

Instance Method Details

#commentsString

Gets the full comments of the statement.

Returns:

  • (String)

    Returns the full comments of the statement.



54
55
56
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 54

def comments
  @docstring.all
end

#comments_hash_flagBoolean

Determines if the comments have hash flag.

Returns:

  • (Boolean)

    Returns true if the comments have a hash flag or false if not.



60
61
62
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 60

def comments_hash_flag
  false
end

#extract_docstring(lines) ⇒ void

This method returns an undefined value.

Extracts the docstring for the statement given the source lines.

Parameters:

  • lines (Array<String>)

    The source lines for the file containing the statement.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 31

def extract_docstring(lines)
  comment = []
  (0..@line-2).reverse_each do |index|
    break unless index <= lines.count
    line = lines[index].strip
    count = line.size
    line.gsub!(COMMENT_REGEX, '')
    # Break out if nothing was removed (wasn't a comment line)
    break unless line.size < count
    comment << line
  end
  @comments_range = (@line - comment.size - 1..@line - 1)
  @docstring = YARD::Docstring.new(comment.reverse.join("\n"))
end

#showString

Shows the first line context for the statement.

Returns:

  • (String)

    Returns the first line context for the statement.



48
49
50
# File 'lib/puppet-strings/yard/parsers/puppet/statement.rb', line 48

def show
  "\t#{@line}: #{first_line}"
end