Class: YARD::Handlers::Ruby::Legacy::MethodHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/handlers/ruby/legacy/method_handler.rb

Overview

Handles a method definition

Constant Summary

Constant Summary

Constants included from Parser::Ruby::Legacy::RubyToken

Parser::Ruby::Legacy::RubyToken::EXPR_ARG, Parser::Ruby::Legacy::RubyToken::EXPR_BEG, Parser::Ruby::Legacy::RubyToken::EXPR_CLASS, Parser::Ruby::Legacy::RubyToken::EXPR_DOT, Parser::Ruby::Legacy::RubyToken::EXPR_END, Parser::Ruby::Legacy::RubyToken::EXPR_FNAME, Parser::Ruby::Legacy::RubyToken::EXPR_MID, Parser::Ruby::Legacy::RubyToken::NEWLINE_TOKEN, Parser::Ruby::Legacy::RubyToken::TkReading2Token, Parser::Ruby::Legacy::RubyToken::TkSymbol2Token

Constants included from CodeObjects

CodeObjects::BUILTIN_ALL, CodeObjects::BUILTIN_CLASSES, CodeObjects::BUILTIN_EXCEPTIONS, CodeObjects::BUILTIN_EXCEPTIONS_HASH, CodeObjects::BUILTIN_MODULES, CodeObjects::CONSTANTMATCH, CodeObjects::CSEP, CodeObjects::CSEPQ, CodeObjects::ISEP, CodeObjects::ISEPQ, CodeObjects::METHODMATCH, CodeObjects::METHODNAMEMATCH, CodeObjects::NAMESPACEMATCH, CodeObjects::NSEP, CodeObjects::NSEPQ

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

This class inherits a constructor from YARD::Handlers::Base

Instance Attribute Details

- (Object) extra_state (readonly) Originally defined in class Base

Returns the value of attribute extra_state

- (Object) globals (readonly) Originally defined in class Base

Returns the value of attribute globals

- (Object) namespace Originally defined in class Base

Returns the value of attribute namespace

- (Object) owner Originally defined in class Base

Returns the value of attribute owner

- (Processor) parser (readonly) Originally defined in class Base

Returns the processor object that manages all global state during handling.

Returns:

  • (Processor)

    the processor object that manages all global state during handling.

- (Object) scope Originally defined in class Base

Returns the value of attribute scope

- (Object) statement (readonly) Originally defined in class Base

Returns the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

Returns:

  • (Object)

    the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

- (Object) visibility Originally defined in class Base

Returns the value of attribute visibility

Instance Method Details

- (void) process

This method returns an undefined value.

Main processing callback



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yard/handlers/ruby/legacy/method_handler.rb', line 5

process do
  nobj = namespace
  mscope = scope

  if statement.tokens.to_s =~ /^def\s+(#{METHODMATCH})(?:(?:\s+|\s*\()(.*)(?:\)\s*$)?)?/m
    meth, args = $1, $2
    meth.gsub!(/\s+/,'')
    args = tokval_list(YARD::Parser::Ruby::Legacy::TokenList.new(args), :all)
    args.map! do |a|
      k, v, r = *a.split(/(:)|=/, 2)
      if r
        k += v
        v = r
      end
      [k.strip, (v ? v.strip : nil)]
    end if args
  else
    raise YARD::Parser::UndocumentableError, "method: invalid name"
  end

  # Class method if prefixed by self(::|.) or Module(::|.)
  if meth =~ /(?:#{NSEPQ}|#{CSEPQ})([^#{NSEP}#{CSEPQ}]+)$/
    mscope, meth, prefix = :class, $1, $`
    if prefix =~ /^[a-z]/ && prefix != "self"
      raise YARD::Parser::UndocumentableError, 'method defined on object instance'
    end
    nobj = P(namespace, prefix) unless prefix == "self"
  end

  nobj = P(namespace, nobj.value) while nobj.type == :constant
  obj = register MethodObject.new(nobj, meth, mscope) do |o|
    o.explicit = true
    o.parameters = args
  end

  # delete any aliases referencing old method
  nobj.aliases.each do |aobj, name|
    next unless name == obj.name
    nobj.aliases.delete(aobj)
  end if nobj.is_a?(NamespaceObject)

  if mscope == :instance && meth == "initialize"
    unless obj.has_tag?(:return)
      obj.add_tag(YARD::Tags::Tag.new(:return,
        "a new instance of #{namespace.name}", namespace.name.to_s))
    end
  elsif mscope == :class && obj.docstring.blank? && %w(inherited included
      extended method_added method_removed method_undefined).include?(meth)
    obj.add_tag(YARD::Tags::Tag.new(:private, nil))
  elsif meth.to_s =~ /\?$/
    if obj.tag(:return) && (obj.tag(:return).types || []).empty?
      obj.tag(:return).types = ['Boolean']
    elsif obj.tag(:return).nil?
      unless obj.tags(:overload).any? {|overload| overload.tag(:return) }
        obj.add_tag(YARD::Tags::Tag.new(:return, "", "Boolean"))
      end
    end
  end

  if obj.has_tag?(:option)
    # create the options parameter if its missing
    obj.tags(:option).each do |option|
      expected_param = option.name
      unless obj.tags(:param).find {|x| x.name == expected_param }
        new_tag = YARD::Tags::Tag.new(:param, "a customizable set of options", "Hash", expected_param)
        obj.add_tag(new_tag)
      end
    end
  end

  if info = obj.attr_info
    if meth.to_s =~ /=$/ # writer
      info[:write] = obj if info[:read]
    else
      info[:read] = obj if info[:write]
    end
  end

  parse_block(:owner => obj) # mainly for yield/exceptions
end