2010-12-20 06:22:49 +03:00
|
|
|
require 'rdoc/method_attr'
|
|
|
|
require 'rdoc/token_stream'
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# AnyMethod is the base class for objects representing methods
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
class RDoc::AnyMethod < RDoc::MethodAttr
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2011-06-16 08:59:24 +04:00
|
|
|
MARSHAL_VERSION = 1 # :nodoc:
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Don't rename \#initialize to \::new
|
|
|
|
|
|
|
|
attr_accessor :dont_rename_initialize
|
|
|
|
|
2011-02-02 03:32:30 +03:00
|
|
|
##
|
|
|
|
# The C function that implements this method (if it was defined in a C file)
|
|
|
|
|
|
|
|
attr_accessor :c_function
|
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
##
|
2010-12-20 06:22:49 +03:00
|
|
|
# Different ways to call this method
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
attr_accessor :call_seq
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Parameters for this method
|
|
|
|
|
2010-04-10 10:36:13 +04:00
|
|
|
attr_accessor :params
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
include RDoc::TokenStream
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
##
|
|
|
|
# Creates a new AnyMethod with a token stream +text+ and +name+
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
def initialize text, name
|
|
|
|
super
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2011-02-02 03:32:30 +03:00
|
|
|
@c_function = nil
|
2010-04-01 11:45:16 +04:00
|
|
|
@dont_rename_initialize = false
|
2011-02-02 03:32:30 +03:00
|
|
|
@token_stream = nil
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-20 06:22:49 +03:00
|
|
|
# Adds +an_alias+ as an alias for this method in +context+.
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2011-06-16 08:59:24 +04:00
|
|
|
def add_alias an_alias, context = nil
|
2010-12-20 06:22:49 +03:00
|
|
|
method = self.class.new an_alias.text, an_alias.new_name
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2010-12-20 06:22:49 +03: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 11:45:16 +04:00
|
|
|
@aliases << method
|
2011-06-16 08:59:24 +04:00
|
|
|
context.add_method method if context
|
2010-12-20 06:22:49 +03:00
|
|
|
method
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|
2010-04-19 09:08:28 +04:00
|
|
|
##
|
2010-12-20 06:22:49 +03:00
|
|
|
# Prefix for +aref+ is 'method'.
|
2010-04-19 09:08:28 +04:00
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
def aref_prefix
|
|
|
|
'method'
|
2010-04-19 09:08:28 +04:00
|
|
|
end
|
|
|
|
|
2010-04-10 10: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 11: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 08:59:24 +04:00
|
|
|
[a.name, parse(a.comment)]
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
[ MARSHAL_VERSION,
|
|
|
|
@name,
|
|
|
|
full_name,
|
|
|
|
@singleton,
|
|
|
|
@visibility,
|
|
|
|
parse(@comment),
|
|
|
|
@call_seq,
|
|
|
|
@block_params,
|
|
|
|
aliases,
|
2010-04-10 10:36:13 +04:00
|
|
|
@params,
|
2011-06-16 08:59:24 +04:00
|
|
|
@file.absolute_name,
|
2010-04-01 11: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
|
|
|
|
|
|
|
|
def marshal_load(array)
|
|
|
|
@dont_rename_initialize = nil
|
|
|
|
@is_alias_for = nil
|
|
|
|
@token_stream = nil
|
2010-04-21 07:10:03 +04:00
|
|
|
@aliases = []
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2011-06-16 08:59:24 +04:00
|
|
|
version = array[0]
|
2010-04-01 11:45:16 +04:00
|
|
|
@name = array[1]
|
|
|
|
@full_name = array[2]
|
|
|
|
@singleton = array[3]
|
|
|
|
@visibility = array[4]
|
|
|
|
@comment = array[5]
|
|
|
|
@call_seq = array[6]
|
|
|
|
@block_params = array[7]
|
2011-06-16 08:59:24 +04:00
|
|
|
|
|
|
|
array[8].each do |new_name, comment|
|
|
|
|
add_alias RDoc::Alias.new(nil, @name, new_name, comment, @singleton)
|
|
|
|
end
|
|
|
|
|
2010-04-10 10:36:13 +04:00
|
|
|
@params = array[9]
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
@parent_name = if @full_name =~ /#/ then
|
|
|
|
$`
|
|
|
|
else
|
|
|
|
name = @full_name.split('::')
|
|
|
|
name.pop
|
|
|
|
name.join '::'
|
|
|
|
end
|
|
|
|
|
2011-06-16 08:59:24 +04:00
|
|
|
@file = RDoc::TopLevel.new array[10] if version > 0
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Method name
|
2010-12-20 06:22:49 +03:00
|
|
|
#
|
|
|
|
# If the method has no assigned name, it extracts it from #call_seq.
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
def name
|
|
|
|
return @name if @name
|
|
|
|
|
|
|
|
@name = @call_seq[/^.*?\.(\w+)/, 1] || @call_seq if @call_seq
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-02-02 03:32:30 +03: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
|
|
|
|
|
|
|
|
params.gsub(/\s+/, '').split ','
|
|
|
|
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 11:45:16 +04:00
|
|
|
|
|
|
|
def param_seq
|
2011-02-02 03:32:30 +03:00
|
|
|
if @call_seq then
|
|
|
|
params = @call_seq.split("\n").last
|
|
|
|
params = params.sub(/[^( ]+/, '')
|
|
|
|
params = params.sub(/(\|[^|]+\|)\s*\.\.\.\s*(end|\})/, '\1 \2')
|
|
|
|
else
|
|
|
|
params = @params.gsub(/\s*\#.*/, '')
|
|
|
|
params = params.tr("\n", " ").squeeze(" ")
|
|
|
|
params = "(#{params})" unless params[0] == ?(
|
|
|
|
end
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2010-04-10 10:36:13 +04:00
|
|
|
if @block_params then
|
2010-04-01 11:45:16 +04:00
|
|
|
# If this method has explicit block parameters, remove any explicit
|
|
|
|
# &block
|
2010-04-10 10:36:13 +04:00
|
|
|
params.sub!(/,?\s*&\w+/, '')
|
2010-04-01 11:45:16 +04:00
|
|
|
|
2010-04-10 10:36:13 +04:00
|
|
|
block = @block_params.gsub(/\s*\#.*/, '')
|
2010-04-01 11:45:16 +04:00
|
|
|
block = block.tr("\n", " ").squeeze(" ")
|
|
|
|
if block[0] == ?(
|
|
|
|
block.sub!(/^\(/, '').sub!(/\)/, '')
|
|
|
|
end
|
|
|
|
params << " { |#{block}| ... }"
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|