2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# AnyMethod is the base class for objects representing methods
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
class RDoc::AnyMethod < RDoc::MethodAttr
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# 2::
|
|
|
|
# RDoc 4
|
|
|
|
# Added calls_super
|
|
|
|
# Added parent name and class
|
|
|
|
# Added section title
|
|
|
|
|
|
|
|
MARSHAL_VERSION = 2 # :nodoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Don't rename \#initialize to \::new
|
|
|
|
|
|
|
|
attr_accessor :dont_rename_initialize
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# The C function that implements this method (if it was defined in a C file)
|
|
|
|
|
|
|
|
attr_accessor :c_function
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Different ways to call this method
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
attr_accessor :call_seq
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Parameters for this method
|
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
attr_accessor :params
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# If true this method uses +super+ to call a superclass version
|
|
|
|
|
|
|
|
attr_accessor :calls_super
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
include RDoc::TokenStream
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Creates a new AnyMethod with a token stream +text+ and +name+
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def initialize text, name
|
|
|
|
super
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
@c_function = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
@dont_rename_initialize = false
|
2011-02-01 19:32:30 -05:00
|
|
|
@token_stream = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@calls_super = false
|
|
|
|
@superclass_method = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Adds +an_alias+ as an alias for this method in +context+.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def add_alias an_alias, context = nil
|
2010-12-19 22:22:49 -05:00
|
|
|
method = self.class.new an_alias.text, an_alias.new_name
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
method.record_location an_alias.file
|
|
|
|
method.singleton = self.singleton
|
|
|
|
method.params = self.params
|
|
|
|
method.visibility = self.visibility
|
|
|
|
method.comment = an_alias.comment
|
|
|
|
method.is_alias_for = self
|
2010-04-01 03:45:16 -04:00
|
|
|
@aliases << method
|
2011-06-16 00:59:24 -04:00
|
|
|
context.add_method method if context
|
2010-12-19 22:22:49 -05:00
|
|
|
method
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-19 01:08:28 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Prefix for +aref+ is 'method'.
|
2010-04-19 01:08:28 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def aref_prefix
|
|
|
|
'method'
|
2010-04-19 01:08:28 -04:00
|
|
|
end
|
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
##
|
|
|
|
# The call_seq or the param_seq with method name, if there is no call_seq.
|
|
|
|
#
|
|
|
|
# Use this for displaying a method's argument lists.
|
|
|
|
|
|
|
|
def arglists
|
|
|
|
if @call_seq then
|
|
|
|
@call_seq
|
|
|
|
elsif @params then
|
|
|
|
"#{name}#{param_seq}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Dumps this AnyMethod for use by ri. See also #marshal_load
|
|
|
|
|
|
|
|
def marshal_dump
|
|
|
|
aliases = @aliases.map do |a|
|
2011-06-16 00:59:24 -04:00
|
|
|
[a.name, parse(a.comment)]
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
[ MARSHAL_VERSION,
|
|
|
|
@name,
|
|
|
|
full_name,
|
|
|
|
@singleton,
|
|
|
|
@visibility,
|
|
|
|
parse(@comment),
|
|
|
|
@call_seq,
|
|
|
|
@block_params,
|
|
|
|
aliases,
|
2010-04-10 02:36:13 -04:00
|
|
|
@params,
|
2012-11-27 03:54:03 -05:00
|
|
|
@file.relative_name,
|
2012-11-26 23:28:14 -05:00
|
|
|
@calls_super,
|
|
|
|
@parent.name,
|
|
|
|
@parent.class,
|
|
|
|
@section.title,
|
2010-04-01 03:45:16 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Loads this AnyMethod from +array+. For a loaded AnyMethod the following
|
|
|
|
# methods will return cached values:
|
|
|
|
#
|
|
|
|
# * #full_name
|
|
|
|
# * #parent_name
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def marshal_load array
|
2013-01-22 20:02:24 -05:00
|
|
|
initialize_visibility
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@dont_rename_initialize = nil
|
|
|
|
@is_alias_for = nil
|
|
|
|
@token_stream = nil
|
2010-04-20 23:10:03 -04:00
|
|
|
@aliases = []
|
2012-11-26 23:28:14 -05:00
|
|
|
@parent = nil
|
|
|
|
@parent_name = nil
|
|
|
|
@parent_class = nil
|
|
|
|
@section = nil
|
|
|
|
@file = nil
|
|
|
|
|
|
|
|
version = array[0]
|
|
|
|
@name = array[1]
|
|
|
|
@full_name = array[2]
|
|
|
|
@singleton = array[3]
|
|
|
|
@visibility = array[4]
|
|
|
|
@comment = array[5]
|
|
|
|
@call_seq = array[6]
|
|
|
|
@block_params = array[7]
|
|
|
|
# 8 handled below
|
|
|
|
@params = array[9]
|
|
|
|
# 10 handled below
|
|
|
|
@calls_super = array[11]
|
|
|
|
@parent_name = array[12]
|
|
|
|
@parent_title = array[13]
|
|
|
|
@section_title = array[14]
|
2011-06-16 00:59:24 -04:00
|
|
|
|
|
|
|
array[8].each do |new_name, comment|
|
|
|
|
add_alias RDoc::Alias.new(nil, @name, new_name, comment, @singleton)
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@parent_name ||= if @full_name =~ /#/ then
|
|
|
|
$`
|
|
|
|
else
|
|
|
|
name = @full_name.split('::')
|
|
|
|
name.pop
|
|
|
|
name.join '::'
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
@file = RDoc::TopLevel.new array[10] if version > 0
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Method name
|
2010-12-19 22:22:49 -05:00
|
|
|
#
|
|
|
|
# If the method has no assigned name, it extracts it from #call_seq.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def name
|
|
|
|
return @name if @name
|
|
|
|
|
|
|
|
@name = @call_seq[/^.*?\.(\w+)/, 1] || @call_seq if @call_seq
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-02-01 19:32:30 -05:00
|
|
|
# A list of this method's method and yield parameters. +call-seq+ params
|
|
|
|
# are preferred over parsed method and block params.
|
|
|
|
|
|
|
|
def param_list
|
|
|
|
if @call_seq then
|
|
|
|
params = @call_seq.split("\n").last
|
|
|
|
params = params.sub(/.*?\((.*)\)/, '\1')
|
|
|
|
params = params.sub(/(\{|do)\s*\|([^|]*)\|.*/, ',\2')
|
|
|
|
elsif @params then
|
|
|
|
params = @params.sub(/\((.*)\)/, '\1')
|
|
|
|
|
|
|
|
params << ",#{@block_params}" if @block_params
|
|
|
|
elsif @block_params then
|
|
|
|
params = @block_params
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
params = params.gsub(/\s+/, '').split ','
|
|
|
|
|
|
|
|
params.map { |param| param.sub(/=.*/, '') }
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Pretty parameter list for this method. If the method's parameters were
|
|
|
|
# given by +call-seq+ it is preferred over the parsed values.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def param_seq
|
2011-02-01 19:32:30 -05:00
|
|
|
if @call_seq then
|
|
|
|
params = @call_seq.split("\n").last
|
|
|
|
params = params.sub(/[^( ]+/, '')
|
|
|
|
params = params.sub(/(\|[^|]+\|)\s*\.\.\.\s*(end|\})/, '\1 \2')
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif @params then
|
2011-02-01 19:32:30 -05:00
|
|
|
params = @params.gsub(/\s*\#.*/, '')
|
|
|
|
params = params.tr("\n", " ").squeeze(" ")
|
|
|
|
params = "(#{params})" unless params[0] == ?(
|
2012-11-26 23:28:14 -05:00
|
|
|
else
|
|
|
|
params = ''
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
if @block_params then
|
2010-04-01 03:45:16 -04:00
|
|
|
# If this method has explicit block parameters, remove any explicit
|
|
|
|
# &block
|
2010-04-10 02:36:13 -04:00
|
|
|
params.sub!(/,?\s*&\w+/, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
block = @block_params.gsub(/\s*\#.*/, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
block = block.tr("\n", " ").squeeze(" ")
|
|
|
|
if block[0] == ?(
|
|
|
|
block.sub!(/^\(/, '').sub!(/\)/, '')
|
|
|
|
end
|
|
|
|
params << " { |#{block}| ... }"
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Sets the store for this method and its referenced code objects.
|
|
|
|
|
|
|
|
def store= store
|
|
|
|
super
|
|
|
|
|
|
|
|
@file = @store.add_file @file.full_name if @file
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# For methods that +super+, find the superclass method that would be called.
|
|
|
|
|
|
|
|
def superclass_method
|
|
|
|
return unless @calls_super
|
|
|
|
return @superclass_method if @superclass_method
|
|
|
|
|
|
|
|
parent.each_ancestor do |ancestor|
|
|
|
|
if method = ancestor.method_list.find { |m| m.name == @name } then
|
|
|
|
@superclass_method = method
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@superclass_method
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|