Class: HereDocHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb

Constant Summary collapse

HEREDOC_START =

Sometimes the YARD parser returns Heredoc strings that start with ‘<-` instead of `<<-`.

/^<?<-/

Instance Method Summary collapse

Instance Method Details

#is_heredoc?(str) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb', line 41

def is_heredoc?(str)
  HEREDOC_START.match(str)
end

#process_element(ele) ⇒ String

Turns an entry in the method parameter list into a string.

Parameters:

  • ele (YARD::Parser::Ruby::AstNode)

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb', line 49

def process_element(ele)
  ele = ele.jump(:ident, :string_content)

  case ele.type
  when :ident
    ele.source
  when :string_content
    source = ele.source
    if is_heredoc? source
      process_heredoc(source)
    else
      source
    end
  end
end

#process_heredoc(source) ⇒ String

Cleans up and formats Heredoc contents parsed by YARD.

Parameters:

  • source (String)

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb', line 69

def process_heredoc(source)
  source = source.lines.to_a

  # YARD adds a line of source context on either side of the Heredoc
  # contents.
  source.shift
  source.pop

  # This utility method normalizes indentation and trims whitespace.
  Puppet::Util::Docs.scrub(source.join)
end

#process_parameters(statement) ⇒ (String, Hash{String => String})

Extracts the Puppet function name and options hash from the parsed definition.

Returns:

  • ((String, Hash{String => String}))


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb', line 14

def process_parameters(statement)
  # Passing `false` to prameters excludes the block param from the returned
  # list.
  name, opts = statement.parameters(false).compact

  name = process_element(name)

  # Don't try to process options if we don't have any
  if !opts.nil?
    opts = opts.map do |tuple|
      # Jump down into the S-Expression that represents a hashrocket, `=>`,
      # and the values on either side of it.
      tuple.jump(:assoc).map{|e| process_element(e)}
    end

    options = Hash[opts]
  else
    options = {}
  end

  [name, options]
end