class PuppetStrings::Yard::CodeObjects::Function

Implements the Puppet function code object.

Constants

PUPPET

Identifier for Puppet language functions

RUBY_3X

Identifier for 3.x Ruby API functions

RUBY_4X

Identifier for 4.x Ruby API functions

Attributes

parameters[RW]

Public Class Methods

new(name, function_type) click to toggle source

Initializes a Puppet function code object. @param [String] name The name of the function. @param [Symbol] #function_type The type of function (e.g. :ruby3x, :ruby4x, :puppet) @return [void]

# File lib/puppet-strings/yard/code_objects/function.rb, line 35
def initialize(name, function_type)
  super(PuppetStrings::Yard::CodeObjects::Functions.instance(function_type), name)
  @parameters = []
  @function_type = function_type
end

Public Instance Methods

function_type() click to toggle source

Gets the function type display string. @return Returns the function type display string.

# File lib/puppet-strings/yard/code_objects/function.rb, line 49
def function_type
  case @function_type
  when RUBY_3X
    'Ruby 3.x API'
  when RUBY_4X
    'Ruby 4.x API'
  else
    'Puppet Language'
  end
end
signature() click to toggle source

Gets the Puppet signature of the function (single overload only). @return [String] Returns the Puppet signature of the function.

# File lib/puppet-strings/yard/code_objects/function.rb, line 62
def signature
  return '' if self.has_tag? :overload
  tags = self.tags(:param)
  args = @parameters.map do |parameter|
    name, default = parameter
    tag = tags.find { |tag| tag.name == name } if tags
    type = tag && tag.types ? "#{tag.type} " : 'Any '
    prefix = "#{name[0]}" if name.start_with?('*', '&')
    name = name[1..-1] if prefix
    default = " = #{default}" if default
    "#{type}#{prefix}$#{name}#{default}"
  end.join(', ')
  @name.to_s + '(' + args + ')'
end
to_hash() click to toggle source

Converts the code object to a hash representation. @return [Hash] Returns a hash representation of the code object.

# File lib/puppet-strings/yard/code_objects/function.rb, line 79
def to_hash
  hash = {}
  hash[:name] = name
  hash[:file] = file
  hash[:line] = line
  hash[:type] = @function_type.to_s
  signature = self.signature
  hash[:signature] = signature unless signature.empty?
  hash[:docstring] = PuppetStrings::Json.docstring_to_hash(docstring)
  defaults = Hash[*parameters.select{ |p| !p[1].nil? }.flatten]
  hash[:defaults] = defaults unless defaults.empty?
  hash[:source] = source unless source && source.empty?
  hash
end
type() click to toggle source

Gets the type of the code object. @return Returns the type of the code object.

# File lib/puppet-strings/yard/code_objects/function.rb, line 43
def type
  :puppet_function
end