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 37
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 51
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 64
def signature
  return '' if has_tag? :overload

  tags = self.tags(:param)
  args = @parameters.map do |parameter|
    name, default = parameter
    tag = tags.find { |t| t.name == name } if tags
    type = tag&.types ? "#{tag.type} " : 'Any '
    prefix = (name[0]).to_s if name.start_with?('*', '&')
    name = name[1..] if prefix
    default = " = #{default}" if default
    "#{type}#{prefix}$#{name}#{default}"
  end.join(', ')
  "#{@name}(#{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 82
def to_hash
  hash = {}

  hash[:name] = name
  hash[:file] = file
  hash[:line] = line
  hash[:type] = @function_type.to_s
  hash[:signatures] = []

  if has_tag? :overload
    # loop over overloads and append onto the signatures array
    tags(:overload).each do |o|
      hash[:signatures] << { signature: o.signature, docstring: PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option enum return example]) }
    end
  else
    hash[:signatures] << { signature: signature, docstring: PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example]) }
  end

  hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
  defaults = Hash[*parameters.reject { |p| p[1].nil? }.flatten]
  hash[:defaults] = defaults unless defaults.nil? || defaults.empty?
  hash[:source] = source unless source.nil? || 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 45
def type
  :puppet_function
end