Class: PuppetStrings::Yard::CodeObjects::Function
- Inherits:
-
Base
- Object
- YARD::CodeObjects::NamespaceObject
- Base
- PuppetStrings::Yard::CodeObjects::Function
- 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
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#function_type ⇒ Object
Gets the function type display string.
-
#initialize(name, function_type) ⇒ void
constructor
Initializes a Puppet function code object.
-
#signature ⇒ String
Gets the Puppet signature of the function (single overload only).
-
#to_hash ⇒ Hash
Converts the code object to a hash representation.
-
#type ⇒ Object
Gets the type of the code object.
Methods inherited from Base
Constructor Details
#initialize(name, function_type) ⇒ void
Initializes a Puppet function code object.
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
#parameters ⇒ Object
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_type ⇒ Object
Gets 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 |
#signature ⇒ String
Gets the Puppet signature of the function (single overload only).
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 = self.(:param) args = @parameters.map do |parameter| name, default = parameter tag = .find { |t| t.name == name } if 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 ⇒ Hash
Converts the code object to a hash representation.
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.(: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 |
#type ⇒ Object
Gets 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 |