1
2
3
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
require 'rdoc/code_object'
##
# Abstract class representing either a method or an attribute.
class RDoc::MethodAttr < RDoc::CodeObject
include Comparable
##
# Name of this method/attribute.
attr_accessor :name
##
# public, protected, private
attr_accessor :visibility
##
# Is this a singleton method/attribute?
attr_accessor :singleton
##
# Source file token stream
attr_reader :text
##
# Array of other names for this method/attribute
attr_reader :aliases
##
# The method/attribute we're aliasing
attr_accessor :is_alias_for
#--
# The attributes below are for AnyMethod only.
# They are left here for the time being to
# allow ri to operate.
# TODO modify ri to avoid calling these on attributes.
#++
##
# Parameters yielded by the called block
attr_reader :block_params
##
# Parameters for this method
attr_accessor :params
##
# Different ways to call this method
attr_accessor :call_seq
##
# The call_seq or the param_seq with method name, if there is no call_seq.
attr_reader :arglists
##
# Pretty parameter list for this method
attr_reader :param_seq
##
# Creates a new MethodAttr from token stream +text+ and method or attribute
# name +name+.
#
# Usually this is called by super from a subclass.
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
##
# Order by #singleton then #name
def <=>(other)
[@singleton ? 0 : 1, name] <=> [other.singleton ? 0 : 1, other.name]
end
##
# 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.
def documented?
super or
(is_alias_for and is_alias_for.documented?) or
(see and see.documented?)
end
##
# 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.
def see
@see = find_see if @see == false
@see
end
def find_see # :nodoc:
return nil if singleton || is_alias_for
# look for the method
other = find_method_or_attribute name
return other if other
# if it is a setter, look for a getter
return nil unless name =~ /[a-z_]=$/i # avoid == or ===
return find_method_or_attribute name[0..-2]
end
def find_method_or_attribute name # :nodoc:
return nil unless parent.respond_to? :ancestors
searched = parent.ancestors
kernel = RDoc::TopLevel.all_modules_hash['Kernel']
searched << kernel if kernel &&
parent != kernel && !searched.include?(kernel)
searched.each do |ancestor|
next if parent == ancestor
next if String === ancestor
other = ancestor.find_method_named('#' << name) ||
ancestor.find_attribute_named(name)
return other if other
end
nil
end
##
# 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+.
def add_alias(an_alias, context)
raise NotImplementedError
end
##
# HTML fragment reference for this method
def aref
type = singleton ? 'c' : 'i'
# % characters are not allowed in html names => dash instead
"#{aref_prefix}-#{type}-#{html_name}"
end
##
# Prefix for +aref+, defined by subclasses.
def aref_prefix
raise NotImplementedError
end
##
# Attempts to sanitize the content passed by the ruby parser:
# remove outer parentheses, etc.
def block_params=(value)
# 'yield.to_s' or 'assert yield, msg'
return @block_params = '' if value =~ /^[\.,]/
# remove trailing 'if/unless ...'
return @block_params = '' if value =~ /^(if|unless)\s/
value = $1.strip if value =~ /^(.+)\s(if|unless)\s/
# outer parentheses
value = $1 if value =~ /^\s*\((.*)\)\s*$/
value = value.strip
# proc/lambda
return @block_params = $1 if value =~ /^(proc|lambda)(\s*\{|\sdo)/
# surrounding +...+ or [...]
value = $1.strip if value =~ /^\+(.*)\+$/
value = $1.strip if value =~ /^\[(.*)\]$/
return @block_params = '' if value.empty?
# global variable
return @block_params = 'str' if value =~ /^\$[&0-9]$/
# wipe out array/hash indices
value.gsub!(/(\w)\[[^\[]+\]/, '\1')
# remove @ from class/instance variables
value.gsub!(/@@?([a-z0-9_]+)/, '\1')
# method calls => method name
value.gsub!(/([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(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
downcase
else
$2
end
end
# class prefixes
value.gsub!(/[A-Za-z0-9_:]+::/, '')
# simple expressions
value = $1 if value =~ /^([a-z0-9_]+)\s*[-*+\/]/
@block_params = value.strip
end
##
# HTML id-friendly method/attribute name
def html_name
CGI.escape(@name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
end
##
# Full method/attribute name including namespace
def full_name
@full_name || "#{parent_name}#{pretty_name}"
end
##
# '::' for a class method/attribute, '#' for an instance method.
def name_prefix
singleton ? '::' : '#'
end
##
# Method/attribute name with class/instance indicator
def pretty_name
"#{name_prefix}#{@name}"
end
##
# Type of method/attribute (class or instance)
def type
singleton ? 'class' : 'instance'
end
##
# Path to this method
def path
"#{@parent.path}##{aref}"
end
##
# Name of our parent with special handling for un-marshaled methods
def parent_name
@parent_name || super
end
def pretty_print q # :nodoc:
alias_for = @is_alias_for ? "alias for #{@is_alias_for.name}" : nil
q.group 2, "[#{self.class.name} #{full_name} #{visibility}", "]" do
if alias_for then
q.breakable
q.text alias_for
end
if text then
q.breakable
q.text "text:"
q.breakable
q.pp @text
end
unless comment.empty? then
q.breakable
q.text "comment:"
q.breakable
q.pp @comment
end
end
end
def inspect # :nodoc:
alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
visibility = self.visibility
visibility = "forced #{visibility}" if force_documentation
"#<%s:0x%x %s (%s)%s>" % [
self.class, object_id,
full_name,
visibility,
alias_for,
]
end
def to_s # :nodoc:
if @is_alias_for
"#{self.class.name}: #{full_name} -> #{is_alias_for}"
else
"#{self.class.name}: #{full_name}"
end
end
end
|