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)



35
36
37
38
39
# 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

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

Instance Method Details

#function_typeObject

Gets the function type display string.

Returns:

  • Returns the function type display string.



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

#signatureString

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

Returns:

  • (String)

    Returns the Puppet signature of the function.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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 { |t| t.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_hashHash

Converts the code object to a hash representation.

Returns:

  • (Hash)

    Returns a hash representation of the code object.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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
  hash[:signatures] = []

  if self.has_tag? :overload
    # loop over overloads and append onto the signatures array
    self.tags(:overload).each do |o|
      hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option return example]) }
    end
  else
    hash[:signatures] << { :signature => self.signature, :docstring =>  PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option 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.empty?
  hash[:source] = source unless source && source.empty?
  hash
end

#typeObject

Gets the type of the code object.

Returns:

  • Returns the type of the code object.



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

def type
  :puppet_function
end