class Puppet4xFunctionHandler

Constants

HEREDOC_START

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

Public Instance Methods

extract_type_from_command(command) click to toggle source

Given a command node which represents code like this: param 'Optional', :value_type Extract the type name and type signature and return them as a array.

# File lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb, line 29
def extract_type_from_command command
  return [] if command.children.length < 2 or command.children[1].children.length < 2
  type_specifier = command.children[1]
  # the parameter signature is the first child of the specifier and an
  # identifier. Jump to the content inside the quotes and convert it to a
  # string.
  param_signature = type_specifier.children[0].jump(:tstring_content).source
  # The parameter name is the second child of the specifier and a symbol.
  # convert it to a string.
  param_name_ident = type_specifier.jump :ident
  return [] if param_name_ident == type_specifier
  param_name = param_name_ident.source
  [param_name, param_signature]
end

Private Instance Methods

function_namespace() click to toggle source

Returns a {PuppetNamespaceObject} for holding functions. Creates this object if necessary.

@return [PuppetNamespaceObject]

# File lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb, line 150
def function_namespace
  # NOTE: This tricky. If there is ever a Ruby class or module with the
  # name ::Puppet4xFunctions, then there will be a clash. Hopefully the name
  # is sufficiently uncommon.
  obj = P(:root, 'Puppet4xFunctions')
  if obj.is_a? Proxy
    namespace_obj = PuppetNamespaceObject.new(:root, 'Puppet4xFunctions')

    register namespace_obj
    # FIXME: The docstring has to be cleared. Otherwise, the namespace
    # object will be registered using the docstring of the
    # `create_function` call that is currently being processed.
    #
    # Figure out how to properly register the namespace without using the
    # function handler object.
    register_docstring(namespace_obj, '', nil)
    namespace_obj.add_tag YARD::Tags::Tag.new(:api, 'public')
  end

  obj
end
process_element(ele) click to toggle source

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

@param ele [YARD::Parser::Ruby::AstNode] @return [String]

# File lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb, line 203
def process_element(ele)
  ele = ele.jump(:ident, :string_content, :tstring_content)

  case ele.type
  when :ident
    ele.source
  when :string_content, :tstring_content
    source = ele.source
    if HEREDOC_START.match(source)
      process_heredoc(source)
    else
      source
    end
  end
end
process_heredoc(source) click to toggle source

Cleans up and formats Heredoc contents parsed by YARD.

@param source [String] @return [String]

# File lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb, line 223
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() click to toggle source

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

@return [(String, Hash{String => String})]

# File lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb, line 184
def process_parameters
  # Passing `false` to parameters excludes the block param from the returned
  # array.
  name, _ = statement.parameters(false).compact

  name = process_element(name)


  name
end