# class RDoc::MethodAttr

Abstract class representing either a method or an attribute.

### Attributes

aliases[R]

Array of other names for this method/attribute

arglists[R]

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

block_params[R]

Parameters yielded by the called block

call_seq[RW]

Different ways to call this method

is_alias_for[RW]

The method/attribute we’re aliasing

name[RW]

Name of this method/attribute.

param_seq[R]

Pretty parameter list for this method

params[RW]

Parameters for this method

singleton[RW]

Is this a singleton method/attribute?

text[R]

Source file token stream

visibility[RW]

public, protected, private

### Public Class Methods

new(text, name) click to toggle source

Creates a new MethodAttr from token stream `text` and method or attribute name `name`.

Usually this is called by super from a subclass.

    # File lib/rdoc/method_attr.rb, line 77
    def initialize text, name
      super()

      @text = text
      @name = name

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

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

### Public Instance Methods

<=>(other) click to toggle source

Order by singleton then name

    # File lib/rdoc/method_attr.rb, line 100
    def <=>(other)
      [@singleton ? 0 : 1, name] <=> [other.singleton ? 0 : 1, other.name]
    end

add_alias(an_alias, context) click to toggle source

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

- creates a new AnyMethod/Attribute `newa` named an_alias.new_name;

- adds `self` as `newa.is_alias_for`;

- adds `newa` to aliases

- adds `newa` to the methods/attributes of `context`.

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

aref() click to toggle source

HTML fragment reference for this method

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

aref_prefix() click to toggle source

Prefix for `aref`, defined by subclasses.

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

block_params=(value) click to toggle source

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

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

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

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

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

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

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

      return @block_params = '' if value.empty?

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

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

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

      # method calls => method name
      value.gsub!(%r([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' then
          $1.split('::').last.  # ClassName => class_name
            gsub(%r([A-Z]+)([A-Z][a-z])/,'\1_\2').
            gsub(%r([a-z\d])([A-Z])/,'\1_\2').
            downcase
        else
          $2
        end
      end

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

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

      @block_params = value.strip
    end

documented?() click to toggle source

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.

    # File lib/rdoc/method_attr.rb, line 111
    def documented?
      super or
        (is_alias_for and is_alias_for.documented?) or
        (see and see.documented?)
    end

full_name() click to toggle source

Full method/attribute name including namespace

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

html_name() click to toggle source

HTML id-friendly method/attribute name

    # File lib/rdoc/method_attr.rb, line 261
    def html_name
      require 'cgi'

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

name_prefix() click to toggle source

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

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

output_name(context) click to toggle source

Name for output to HTML. For class methods the full name with a “.” is used like `SomeClass.method_name`. For instance methods the class name is used if `context` does not match the parent.

This is to help prevent people from using

  to call class methods.

    # File lib/rdoc/method_attr.rb, line 288
    def output_name context
      return "#{name_prefix}#{@name}" if context == parent

      "#{parent_name}#{@singleton ? '.' : '#'}#{@name}"
    end

parent_name() click to toggle source

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

    # File lib/rdoc/method_attr.rb, line 318
    def parent_name
      @parent_name || super
    end

path() click to toggle source

Path to this method

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

pretty_name() click to toggle source

Method/attribute name with class/instance indicator

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

search_record() click to toggle source

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

    # File lib/rdoc/method_attr.rb, line 363
    def search_record
      [
        @name,
        full_name,
        @name,
        @parent.full_name,
        path,
        params,
        snippet(@comment),
      ]
    end

see() click to toggle source

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/method_attr.rb, line 131
    def see
      @see = find_see if @see == false
      @see
    end

type() click to toggle source

Type of method/attribute (class or instance)

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