Class: PuppetStrings::Yard::Tags::OverloadTag

Inherits:
YARD::Tags::Tag
  • Object
show all
Defined in:
lib/puppet-strings/yard/tags/overload_tag.rb

Overview

Implements an overload tag for Puppet functions

This differs from Yard’s overload tag in that the signatures are formatted according to Puppet language rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, docstring) ⇒ void

Initializes the overload tag.

Parameters:

  • name (String, Symbol)

    The name of the function being overloaded.

  • docstring (String)

    The docstring for the overload.



13
14
15
16
17
18
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 13

def initialize(name, docstring)
  super(:overload, nil)
  @name = name.to_s
  @parameters = []
  @docstring = YARD::Docstring.new(docstring)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Responsible for forwarding method calls to the associated object.

Parameters:

  • method_name (Symbol)

    The method being invoked.

  • args (Array)

    The args passed to the method.

  • block

    The block passed to the method.

Returns:

  • Returns what the method call on the object would return.



78
79
80
81
82
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 78

def method_missing(method_name, ...)
  return object.send(method_name, ...) if object.respond_to? method_name

  super
end

Instance Attribute Details

#docstringObject (readonly)

Returns the value of attribute docstring.



7
8
9
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 7

def docstring
  @docstring
end

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 7

def parameters
  @parameters
end

Instance Method Details

#add_tag(tag) ⇒ void

This method returns an undefined value.

Adds a tag to the overload’s docstring.

Parameters:

  • tag (YARD::Tag)

    The tag to add to the overload’s docstring.



39
40
41
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 39

def add_tag(tag)
  @docstring.add_tag(tag)
end

#has_tag?(name) ⇒ Boolean

Determines if a tag with the given name is present.

Parameters:

  • name (String, Symbol)

    The tag name.

Returns:

  • (Boolean)

    Returns true if there is at least one tag with the given name or false if not.



60
61
62
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 60

def has_tag?(name) # rubocop:disable Naming/PredicateName
  @docstring.has_tag?(name)
end

#object=(value) ⇒ void

This method returns an undefined value.

Sets the object associated with this tag.

Parameters:

  • value (Object)

    The object to associate with this tag.



67
68
69
70
71
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 67

def object=(value)
  super
  @docstring.object = value
  @docstring.tags.each { |tag| tag.object = value }
end

#respond_to_missing?(method_name, include_all = false) ⇒ Boolean

Determines if the associated object responds to the give missing method name.

Parameters:

  • method_name (Symbol, String)

    The name of the method to check.

  • include_all (Boolean) (defaults to: false)

    True to include all methods in the check or false for only public methods.

Returns:

  • (Boolean)

    Returns true if the object responds to the method or false if not.



88
89
90
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 88

def respond_to_missing?(method_name, include_all = false)
  object.respond_to?(method_name, include_all) || super
end

#signatureString

Gets the signature of the overload.

Returns:

  • (String)

    Returns the signature of the overload.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 22

def signature
  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

#tag(name) ⇒ YARD::Tag

Gets the first tag of the given name.

Parameters:

  • name (String, Symbol)

    The name of the tag.

Returns:

  • (YARD::Tag)

    Returns the first tag if found or nil if not found.



46
47
48
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 46

def tag(name)
  @docstring.tag(name)
end

#tags(name = nil) ⇒ Array<Yard::Tag>

Gets all tags or tags of a given name.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    The name of the tag to get or nil for all tags.

Returns:

  • (Array<Yard::Tag>)

    Returns an array of tags.



53
54
55
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 53

def tags(name = nil)
  @docstring.tags(name)
end

#to_hashHash

Converts the overload tag to a hash representation.

Returns:

  • (Hash)

    Returns a hash representation of the overload.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 100

def to_hash
  hash = {}
  hash[:tag_name] = tag_name
  hash[:text] = text if text
  hash[:signature] = signature
  hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring) unless docstring.blank?
  defaults = Hash[*parameters.reject { |p| p[1].nil? }.flatten]
  hash[:defaults] = defaults unless defaults.empty?
  hash[:types] = types if types
  hash[:name] = name if name
  hash
end

#typeSymbol

Gets the type of the object associated with this tag.

Returns:

  • (Symbol)

    Returns the type of the object associated with this tag.



94
95
96
# File 'lib/puppet-strings/yard/tags/overload_tag.rb', line 94

def type
  object.type
end