Module: YARD::Templates::Helpers::MethodHelper

Included in:
Template
Defined in:
lib/yard/templates/helpers/method_helper.rb

Overview

Helper methods for method objects.

Instance Method Summary collapse

Instance Method Details

#format_args(object) ⇒ String

Returns formatted arguments for a method.

Returns:

  • (String)

    formatted arguments for a method



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yard/templates/helpers/method_helper.rb', line 7

def format_args(object)
  return if object.parameters.nil?
  params = object.parameters
  if object.has_tag?(:yield) || object.has_tag?(:yieldparam)
    params.reject! do |param|
      param[0].to_s[0, 1] == "&" &&
        !object.tags(:param).any? {|t| t.name == param[0][1..-1] }
    end
  end

  if params.empty?
    ""
  else
    args = params.map do |n, v|
      v ? "#{n}#{n[-1, 1] == ':' ? '' : ' ='} #{v}" : n.to_s
    end.join(", ")
    h("(#{args})")
  end
end

#format_block(object) ⇒ String

Returns formatted block if one exists.

Returns:

  • (String)

    formatted block if one exists



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yard/templates/helpers/method_helper.rb', line 35

def format_block(object)
  if object.has_tag?(:yield) && object.tag(:yield).types
    params = object.tag(:yield).types
  elsif object.has_tag?(:yieldparam)
    params = object.tags(:yieldparam).map(&:name)
  elsif object.has_tag?(:yield)
    return "{ ... }"
  else
    params = nil
  end

  params ? h("{|" + params.join(", ") + "| ... }") : ""
end

#format_code(object, _show_lines = false) ⇒ String

Returns formats source of an object.

Returns:

  • (String)

    formats source of an object



57
58
59
60
61
62
63
64
65
# File 'lib/yard/templates/helpers/method_helper.rb', line 57

def format_code(object, _show_lines = false)
  i = -1
  lines = object.source.split(/\n/)
  longestline = (object.line + lines.size).to_s.length
  lines.map do |line|
    lineno = object.line + (i += 1)
    (" " * (longestline - lineno.to_s.length)) + lineno.to_s + "    " + line
  end.join("\n")
end

#format_constant(value) ⇒ String

Returns formats source code of a constant value.

Returns:

  • (String)

    formats source code of a constant value



68
69
70
71
72
# File 'lib/yard/templates/helpers/method_helper.rb', line 68

def format_constant(value)
  sp = value.split("\n").last[/^(\s+)/, 1]
  num = sp ? sp.size : 0
  html_syntax_highlight value.gsub(/^\s{#{num}}/, '')
end

#format_lines(object) ⇒ String

Returns formats line numbers for source code of an object.

Returns:

  • (String)

    formats line numbers for source code of an object



50
51
52
53
54
# File 'lib/yard/templates/helpers/method_helper.rb', line 50

def format_lines(object)
  return "" if object.source.nil? || object.line.nil?
  i = -1
  object.source.split(/\n/).map { object.line + (i += 1) }.join("\n")
end

#format_return_types(object) ⇒ String

Returns formatted and linked return types for a method.

Returns:

  • (String)

    formatted and linked return types for a method



28
29
30
31
32
# File 'lib/yard/templates/helpers/method_helper.rb', line 28

def format_return_types(object)
  return unless object.has_tag?(:return) && object.tag(:return).types
  return if object.tag(:return).types.empty?
  format_types [object.tag(:return).types.first], false
end