alias.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ##
  2. # Represent an alias, which is an old_name/new_name pair associated with a
  3. # particular context
  4. #--
  5. # TODO implement Alias as a proxy to a method/attribute, inheriting from
  6. # MethodAttr
  7. class RDoc::Alias < RDoc::CodeObject
  8. ##
  9. # Aliased method's name
  10. attr_reader :new_name
  11. alias name new_name
  12. ##
  13. # Aliasee method's name
  14. attr_reader :old_name
  15. ##
  16. # Is this an alias declared in a singleton context?
  17. attr_accessor :singleton
  18. ##
  19. # Source file token stream
  20. attr_reader :text
  21. ##
  22. # Creates a new Alias with a token stream of +text+ that aliases +old_name+
  23. # to +new_name+, has +comment+ and is a +singleton+ context.
  24. def initialize(text, old_name, new_name, comment, singleton = false)
  25. super()
  26. @text = text
  27. @singleton = singleton
  28. @old_name = old_name
  29. @new_name = new_name
  30. self.comment = comment
  31. end
  32. ##
  33. # Order by #singleton then #new_name
  34. def <=>(other)
  35. [@singleton ? 0 : 1, new_name] <=> [other.singleton ? 0 : 1, other.new_name]
  36. end
  37. ##
  38. # HTML fragment reference for this alias
  39. def aref
  40. type = singleton ? 'c' : 'i'
  41. "#alias-#{type}-#{html_name}"
  42. end
  43. ##
  44. # Full old name including namespace
  45. def full_old_name
  46. @full_name || "#{parent.name}#{pretty_old_name}"
  47. end
  48. ##
  49. # HTML id-friendly version of +#new_name+.
  50. def html_name
  51. CGI.escape(@new_name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
  52. end
  53. def inspect # :nodoc:
  54. parent_name = parent ? parent.name : '(unknown)'
  55. "#<%s:0x%x %s.alias_method %s, %s>" % [
  56. self.class, object_id,
  57. parent_name, @old_name, @new_name,
  58. ]
  59. end
  60. ##
  61. # '::' for the alias of a singleton method/attribute, '#' for instance-level.
  62. def name_prefix
  63. singleton ? '::' : '#'
  64. end
  65. ##
  66. # Old name with prefix '::' or '#'.
  67. def pretty_old_name
  68. "#{singleton ? '::' : '#'}#{@old_name}"
  69. end
  70. ##
  71. # New name with prefix '::' or '#'.
  72. def pretty_new_name
  73. "#{singleton ? '::' : '#'}#{@new_name}"
  74. end
  75. alias pretty_name pretty_new_name
  76. def to_s # :nodoc:
  77. "alias: #{self.new_name} -> #{self.pretty_old_name} in: #{parent}"
  78. end
  79. end