Class: PuppetStrings::Yard::CodeObjects::Function

Inherits:
Base
  • Object
show all
Defined in:
lib/puppet-strings/yard/code_objects/function.rb

Overview

Implements the Puppet function code object.

Constant Summary collapse

RUBY_3X =

Identifier for 3.x Ruby API functions

:ruby3x
RUBY_4X =

Identifier for 4.x Ruby API functions

:ruby4x
PUPPET =

Identifier for Puppet language functions

:puppet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

new

Constructor Details

#initialize(name, function_type) ⇒ void

Initializes a Puppet function code object.

Parameters:

  • name (String)

    The name of the function.

  • function_type (Symbol)

    The type of function (e.g. :ruby3x, :ruby4x, :puppet)



37
38
39
40
41
# 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

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



31
32
33
# File 'lib/puppet-strings/yard/code_objects/function.rb', line 31

def parameters
  @parameters
end

Instance Method Details

#function_typeObject

Gets the function type display string.

Returns:

  • Returns the function type display string.



51
52
53
54
55
56
57
58
59
60
# 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

#signatureString

Gets the Puppet signature of the function (single overload only).

Returns:

  • (String)

    Returns the Puppet signature of the function.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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_hashHash

Converts the code object to a hash representation.

Returns:

  • (Hash)

    Returns a hash representation of the code object.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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:, 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

#typeObject

Gets the type of the code object.

Returns:

  • Returns the type of the code object.



45
46
47
# File 'lib/puppet-strings/yard/code_objects/function.rb', line 45

def type
  :puppet_function
end