class RDoc::MethodAttr

Abstract class representing either a method or an attribute.

Attributes

Array of other names for this method/attribute

The call_seq or the param_seq with method name, if there is no call_seq.

Parameters yielded by the called block

Different ways to call this method

The method/attribute we’re aliasing

Name of this method/attribute.

Parameters for this method

Is this a singleton method/attribute?

RBS type signature lines from inline annotations or loaded .rbs files. Each entry is one overload or type expression.

public, protected, private

Public Class Methods

Creates a new MethodAttr with method or attribute name name.

Usually this is called by super from a subclass.

Calls superclass method RDoc::CodeObject::new
# File lib/rdoc/code_object/method_attr.rb, line 74
def initialize(name, singleton: false)
  super()

  @name = name

  @aliases      = []
  @is_alias_for = nil
  @parent_name  = nil
  @singleton    = singleton
  @visibility   = :public
  @see = false

  @arglists     = nil
  @block_params = nil
  @call_seq     = nil
  @params       = nil
  @type_signature_lines = nil
end

Public Instance Methods

Order by singleton then name

# File lib/rdoc/code_object/method_attr.rb, line 108
def <=>(other)
  return unless other.respond_to?(:singleton) &&
                other.respond_to?(:name)

  [@singleton      ? 0 : 1, name_ord_range,       name] <=>
  [other.singleton ? 0 : 1, other.name_ord_range, other.name]
end

Abstract method. Contexts in their building phase call this to register a new alias for this known method/attribute.

  • creates a new AnyMethod/Attribute named an_alias.new_name;

  • adds self as an alias for the new method or attribute

  • adds the method or attribute to aliases

  • adds the method or attribute to context.

# File lib/rdoc/code_object/method_attr.rb, line 204
def add_alias(an_alias, context)
  raise NotImplementedError
end

Prepend src with line numbers.

# File lib/rdoc/generator/markup.rb, line 117
def add_line_numbers(src)
  return if src.empty? || !line
  start_line = line
  end_line  = start_line + src.count("\n")
  number_digits = end_line.to_s.length

  current_line = start_line
  src.gsub!(/^/) do
    res = "<span class=\"line-num\">#{current_line.to_s.rjust(number_digits)}</span> "

    current_line += 1
    res
  end
end

Prepend src with a comment that declares its location in the source.

# File lib/rdoc/generator/markup.rb, line 135
def add_location_comment(src)
  path = CGI.escapeHTML(file.relative_name)
  if options.line_numbers && !src.empty?
    src.prepend("<span class=\"ruby-comment\"># File #{path}</span>\n")
  else
    src.prepend("<span class=\"ruby-comment\"># File #{path}, line #{line}</span>\n")
  end
end

HTML fragment reference for this method

# File lib/rdoc/code_object/method_attr.rb, line 211
def aref
  type = singleton ? 'c' : 'i'
  # % characters are not allowed in html names => dash instead
  "#{aref_prefix}-#{type}-#{html_name}"
end

Prefix for aref, defined by subclasses.

# File lib/rdoc/code_object/method_attr.rb, line 220
def aref_prefix
  raise NotImplementedError
end

Attempts to sanitize the content passed by the Ruby parser: remove outer parentheses, etc.

# File lib/rdoc/code_object/method_attr.rb, line 228
def block_params=(value)
  # 'yield.to_s' or 'assert yield, msg'
  return @block_params = '' if value =~ /^[\.,]/

  # remove trailing 'if/unless ...'
  return @block_params = '' if value =~ /^(if|unless)\s/

  value = $1.strip if value =~ /^(.+)\s(if|unless)\s/

  # outer parentheses
  value = $1 if value =~ /^\s*\((.*)\)\s*$/
  value = value.strip

  # proc/lambda
  return @block_params = $1 if value =~ /^(proc|lambda)(\s*\{|\sdo)/

  # surrounding +...+ or [...]
  value = $1.strip if value =~ /^\+(.*)\+$/
  value = $1.strip if value =~ /^\[(.*)\]$/

  return @block_params = '' if value.empty?

  # global variable
  return @block_params = 'str' if value =~ /^\$[&0-9]$/

  # wipe out array/hash indices
  value.gsub!(/(\w)\[[^\[]+\]/, '\1')

  # remove @ from class/instance variables
  value.gsub!(/@@?([a-z0-9_]+)/, '\1')

  # method calls => method name
  value.gsub!(/([A-Z:a-z0-9_]+)\.([a-z0-9_]+)(\s*\(\s*[a-z0-9_.,\s]*\s*\)\s*)?/) do
    case $2
    when 'to_s'      then $1
    when 'const_get' then 'const'
    when 'new'
      $1.split('::').last.  # ClassName => class_name
        gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
        gsub(/([a-z\d])([A-Z])/, '\1_\2').
        downcase
    else
      $2
    end
  end

  # class prefixes
  value.gsub!(/[A-Za-z0-9_:]+::/, '')

  # simple expressions
  value = $1 if value =~ /^([a-z0-9_]+)\s*[-*+\/]/

  @block_params = value.strip
end

A method/attribute is documented if any of the following is true:

  • it was marked with :nodoc:;

  • it has a comment;

  • it is an alias for a documented method;

  • it has a see method that is documented.

Calls superclass method RDoc::CodeObject#documented?
# File lib/rdoc/code_object/method_attr.rb, line 127
def documented?
  super or
    (is_alias_for and is_alias_for.documented?) or
    (see and see.documented?)
end

Full method/attribute name including namespace

# File lib/rdoc/code_object/method_attr.rb, line 296
def full_name
  @full_name ||= "#{parent_name}#{pretty_name}"
end

HTML id-friendly method/attribute name

# File lib/rdoc/code_object/method_attr.rb, line 286
def html_name
  require 'cgi/escape'
  require 'cgi/util' unless defined?(CGI::EscapeExt)

  CGI.escape(@name.gsub('-', '-2D')).gsub('%', '-').sub(/^-/, '')
end

Turns the method’s token stream into HTML.

Prepends line numbers if options.line_numbers is true.

# File lib/rdoc/generator/markup.rb, line 149
def markup_code
  return '' unless token_stream

  src = TokenStream.to_html token_stream

  # dedent the source
  common_indent = src.length
  src.scan(/^ *(?=\S)/) do |whitespace|
    common_indent = whitespace.length if whitespace.length < common_indent
    break if common_indent == 0
  end
  src.gsub!(/^#{' ' * common_indent}/, '') if common_indent > 0

  if source_language == 'ruby'
    add_line_numbers(src) if options.line_numbers
    add_location_comment(src)
  end

  src
end

‘::’ for a class method/attribute, ‘#’ for an instance method.

# File lib/rdoc/code_object/method_attr.rb, line 320
def name_prefix
  @singleton ? '::' : '#'
end

Name of our parent with special handling for un-marshaled methods

Calls superclass method RDoc::CodeObject#parent_name
# File lib/rdoc/code_object/method_attr.rb, line 348
def parent_name
  @parent_name || super
end

Path to this method for use with HTML generator output.

# File lib/rdoc/code_object/method_attr.rb, line 341
def path
  "#{@parent.path}##{aref}"
end

Method/attribute name with class/instance indicator

# File lib/rdoc/code_object/method_attr.rb, line 327
def pretty_name
  "#{name_prefix}#{@name}"
end

Used by RDoc::Generator::JsonIndex to create a record for the search engine.

TODO: Remove this method after dropping the darkfish theme and JsonIndex generator. Use search_snippet instead for getting documentation snippets.

# File lib/rdoc/code_object/method_attr.rb, line 382
def search_record
  [
    @name,
    full_name,
    @name,
    @parent.full_name,
    path,
    params,
    search_snippet,
  ]
end

Returns an HTML snippet of the comment for search results.

# File lib/rdoc/code_object/method_attr.rb, line 397
def search_snippet
  return '' if @comment.empty?

  snippet(@comment)
end

A method/attribute to look at, in particular if this method/attribute has no documentation.

It can be a method/attribute of the superclass or of an included module, including the Kernel module, which is always appended to the included modules.

Returns nil if there is no such method/attribute. The is_alias_for method/attribute, if any, is not included.

Templates may generate a “see also …” if this method/attribute has documentation, and “see …” if it does not.

# File lib/rdoc/code_object/method_attr.rb, line 147
def see
  @see = find_see if @see == false
  @see
end

Sets the store for this class or module and its contained code objects.

Calls superclass method RDoc::CodeObject#store=
# File lib/rdoc/code_object/method_attr.rb, line 155
def store=(store)
  super

  @file = @store.add_file @file.full_name if @file
end

Type of method/attribute (class or instance)

# File lib/rdoc/code_object/method_attr.rb, line 334
def type
  singleton ? 'class' : 'instance'
end