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

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

Constant Summary

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

EXPR_ARG, EXPR_BEG, EXPR_CLASS, EXPR_DOT, EXPR_END, EXPR_FNAME, EXPR_MID, NEWLINE_TOKEN, TkReading2Token, TkSymbol2Token, TokenDefinitions

Constants included from YARD::CodeObjects

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

Instance Method Summary

Methods inherited from Base

handles?, #parse_block

Methods included from YARD::Parser::Ruby::Legacy::RubyToken

#Token, def_token, #set_token_position

Methods inherited from YARD::Handlers::Base

clear_subclasses, #ensure_loaded!, handlers, handles, handles?, #initialize, namespace_only, namespace_only?, #parse_block, #push_state, #register, subclasses

Constructor Details

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

Instance Method Details

- (Object) process



4
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
# File 'lib/yard/handlers/ruby/legacy/method_handler.rb', line 4

def process
  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! {|a| k, v = *a.split('=', 2); [k.strip, (v ? v.strip : nil)] } 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 = :class, $1
    nobj = P(namespace, $`) unless $` == "self"
  end
  
  obj = register MethodObject.new(nobj, meth, mscope) do |o| 
    o.visibility = visibility 
    o.source = statement
    o.explicit = true
    o.parameters = args
  end
  if mscope == :instance && meth == "initialize"
    unless obj.has_tag?(:return)
      obj.docstring.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.docstring.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?
      obj.docstring.add_tag(YARD::Tags::Tag.new(:return, "", "Boolean"))
    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