Class | RDoc::Context |
In: |
code_objects.rb
|
Parent: | CodeObject |
aliases | [R] | |
attributes | [R] | |
constants | [R] | |
in_files | [R] | |
includes | [R] | |
method_list | [R] | |
name | [R] | |
requires | [R] | |
sections | [R] | |
visibility | [R] |
# File code_objects.rb, line 163 163: def initialize 164: super() 165: 166: @in_files = [] 167: 168: @name ||= "unknown" 169: @comment ||= "" 170: @parent = nil 171: @visibility = :public 172: 173: @current_section = Section.new(nil, nil) 174: @sections = [ @current_section ] 175: 176: initialize_methods_etc 177: initialize_classes_and_modules 178: end
# File code_objects.rb, line 251 251: def add_alias(an_alias, ignore_case=nil) 252: meth = find_instance_method_named(an_alias.old_name, ignore_case) 253: if meth 254: new_meth = AnyMethod.new(an_alias.text, an_alias.new_name) 255: new_meth.is_alias_for = meth 256: new_meth.singleton = meth.singleton 257: new_meth.params = meth.params 258: new_meth.comment = "Alias for \##{meth.name}" 259: meth.add_alias(new_meth) 260: add_method(new_meth) 261: else 262: add_to(@aliases, an_alias) 263: end 264: end
# File code_objects.rb, line 247 247: def add_attribute(an_attribute) 248: add_to(@attributes, an_attribute) 249: end
# File code_objects.rb, line 229 229: def add_class(class_type, name, superclass) 230: add_class_or_module(@classes, class_type, name, superclass) 231: end
# File code_objects.rb, line 283 283: def add_class_or_module(collection, class_type, name, superclass=nil) 284: cls = collection[name] 285: if cls 286: puts "Reusing class/module #{name}" if $DEBUG 287: else 288: cls = class_type.new(name, superclass) 289: puts "Adding class/module #{name} to #@name" if $DEBUG 290: # collection[name] = cls if @document_self && !@done_documenting 291: collection[name] = cls if !@done_documenting 292: cls.parent = self 293: cls.section = @current_section 294: end 295: cls 296: end
# File code_objects.rb, line 270 270: def add_constant(const) 271: add_to(@constants, const) 272: end
# File code_objects.rb, line 266 266: def add_include(an_include) 267: add_to(@includes, an_include) 268: end
# File code_objects.rb, line 237 237: def add_method(a_method) 238: if !(a_method.visibility == :public) && 239: !(a_method.visibility == :private) && 240: !(a_method.visibility == :protected) 241: a_method.visibility = @visibility 242: end 243: puts "Adding #{a_method.visibility} method #{a_method.name} to #@name" if $DEBUG 244: add_to(@method_list, a_method) 245: end
# File code_objects.rb, line 233 233: def add_module(class_type, name) 234: add_class_or_module(@modules, class_type, name, nil) 235: end
Requires always get added to the top-level (file) context
# File code_objects.rb, line 275 275: def add_require(a_require) 276: if self.kind_of? TopLevel 277: add_to(@requires, a_require) 278: else 279: parent.add_require(a_require) 280: end 281: end
# File code_objects.rb, line 298 298: def add_to(array, thing) 299: array << thing if @document_self && !@done_documenting 300: thing.parent = self 301: thing.section = @current_section 302: end
map the class hash to an array externally
# File code_objects.rb, line 181 181: def classes 182: @classes.values 183: end
Return true if at least part of this thing was defined in file
# File code_objects.rb, line 225 225: def defined_in?(file) 226: @in_files.include?(file) 227: end
# File code_objects.rb, line 374 374: def each_attribute 375: @attributes.each {|a| yield a} 376: end
Find a named attribute, or return nil
# File code_objects.rb, line 524 524: def find_attribute_named(name, ignore_case=nil) 525: if !ignore_case 526: @attributes.find {|m| m.name == name} 527: else 528: @attributes.find {|m| m.name.upcase == name.upcase} 529: end 530: end
Find a named constant, or return nil
# File code_objects.rb, line 515 515: def find_constant_named(name, ignore_case=nil) 516: if !ignore_case 517: @constants.find {|m| m.name == name} 518: else 519: @constants.find {|m| m.name.upcase == name.upcase} 520: end 521: end
find a module at a higher scope
# File code_objects.rb, line 358 358: def find_enclosing_module_named(name, ignore_case=nil) 359: parent && parent.find_module_named(name, ignore_case) 360: end
Look up the given filename.
# File code_objects.rb, line 401 401: def find_file(file, method=nil, ignore_case=nil) 402: find_file_named(file, method, ignore_case) 403: end
Find a named instance method, or return nil
# File code_objects.rb, line 504 504: def find_instance_method_named(name, ignore_case=nil) 505: if !ignore_case 506: @method_list.find {|meth| meth.name == name && !meth.singleton} 507: else 508: @method_list.find {|meth| 509: meth.name.upcase == name.upcase && !meth.singleton 510: } 511: end 512: end
# File code_objects.rb, line 454 454: def find_local_symbol(symbol, ignore_case=nil) 455: res = find_method_named(symbol, ignore_case) || 456: find_constant_named(symbol, ignore_case) || 457: find_attribute_named(symbol, ignore_case) || 458: find_module_named(symbol, ignore_case) 459: end
Find a named method, or return nil
# File code_objects.rb, line 495 495: def find_method_named(name, ignore_case=nil) 496: if !ignore_case 497: @method_list.find {|meth| meth.name == name} 498: else 499: @method_list.find {|meth| meth.name.upcase == name.upcase} 500: end 501: end
Find a named module
# File code_objects.rb, line 332 332: def find_module_named(name, ignore_case=nil) 333: res = nil 334: if !ignore_case 335: return self if self.name == name 336: else 337: return self if self.name.upcase == name.upcase 338: end 339: if !ignore_case 340: res = @modules[name] || @classes[name] 341: else 342: @modules.each{ |n, v| 343: if n.upcase == name.upcase 344: res = v ; break 345: end 346: } 347: @classes.each{ |n, v| 348: if n.upcase == name.upcase 349: res = v ; break 350: end 351: } if !res 352: end 353: return res if res 354: find_enclosing_module_named(name, ignore_case) 355: end
Look up the given symbol. If method is non-nil, then we assume the symbol references a module that contains that method
# File code_objects.rb, line 408 408: def find_symbol(symbol, method=nil, ignore_case=nil) 409: result = nil 410: case symbol 411: when /^::(.*)/ 412: result = toplevel.find_symbol($1, nil, ignore_case) 413: when /::/ 414: modules = symbol.split(/::/) 415: unless modules.empty? 416: module_name = modules.shift 417: result = find_module_named(module_name, ignore_case) 418: if result 419: modules.each do |module_name| 420: result = result.find_module_named(module_name, ignore_case) 421: break unless result 422: end 423: end 424: end 425: else 426: # if a method is specified, then we're definitely looking for 427: # a module, otherwise it could be any symbol 428: if method 429: result = find_module_named(symbol, ignore_case) 430: else 431: result = find_local_symbol(symbol, ignore_case) 432: if result.nil? 433: if symbol =~ /^[A-Z]/ || 434: symbol =~ /^[A-Za-z]/ && ignore_case 435: result = parent 436: while result && result.name != symbol 437: result = result.parent 438: end 439: end 440: end 441: end 442: end 443: if result && method 444: if !result.respond_to?(:find_local_symbol) 445: p result.name 446: p method 447: fail 448: end 449: result = result.find_local_symbol(method, ignore_case) 450: end 451: result 452: end
# File code_objects.rb, line 475 475: def include_includes?(name, ignore_case=nil) 476: self.includes.each{|i| 477: if i.name == name || 478: i.name.upcase == name.upcase && ignore_case 479: return true 480: end 481: } 482: return false 483: end
# File code_objects.rb, line 461 461: def include_requires?(name, ignore_case=nil) 462: if self.kind_of? TopLevel 463: self.requires.each{|r| 464: if r.name == name || 465: r.name.upcase == name.upcase && ignore_case 466: return true 467: end 468: } 469: return false 470: else 471: parent.include_requires?(name) 472: end 473: end
# File code_objects.rb, line 326 326: def initialize_classes_and_modules 327: @classes = {} 328: @modules = {} 329: end
# File code_objects.rb, line 312 312: def initialize_methods_etc 313: @method_list = [] 314: @attributes = [] 315: @aliases = [] 316: @requires = [] 317: @includes = [] 318: @constants = [] 319: end
map the module hash to an array externally
# File code_objects.rb, line 186 186: def modules 187: @modules.values 188: end
Record the file that we happen to find it in
# File code_objects.rb, line 220 220: def record_location(toplevel) 221: @in_files << toplevel unless @in_files.include?(toplevel) 222: end
If a class‘s documentation is turned off after we‘ve started collecting methods etc., we need to remove the ones we have
# File code_objects.rb, line 308 308: def remove_methods_etc 309: initialize_methods_etc 310: end
Handle sections
# File code_objects.rb, line 487 487: def set_current_section(title, comment) 488: @current_section = Section.new(title, comment) 489: @sections << @current_section 490: end
Given an array methods of method names, set the visibility of the corresponding AnyMethod object
# File code_objects.rb, line 198 198: def set_visibility_for(methods, vis, singleton=false) 199: count = 0 200: @method_list.each do |m| 201: if methods.include?(m.name) && m.singleton == singleton 202: m.visibility = vis 203: count += 1 204: end 205: end 206: 207: return if count == methods.size || singleton 208: 209: # perhaps we need to look at attributes 210: 211: @attributes.each do |a| 212: if methods.include?(a.name) 213: a.visibility = vis 214: count += 1 215: end 216: end 217: end