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
8class RDoc::Alias < RDoc::CodeObject
9
10  ##
11  # Aliased method's name
12
13  attr_reader :new_name
14
15  alias name new_name
16
17  ##
18  # Aliasee method's name
19
20  attr_reader :old_name
21
22  ##
23  # Is this an alias declared in a singleton context?
24
25  attr_accessor :singleton
26
27  ##
28  # Source file token stream
29
30  attr_reader :text
31
32  ##
33  # Creates a new Alias with a token stream of +text+ that aliases +old_name+
34  # to +new_name+, has +comment+ and is a +singleton+ context.
35
36  def initialize(text, old_name, new_name, comment, singleton = false)
37    super()
38
39    @text = text
40    @singleton = singleton
41    @old_name = old_name
42    @new_name = new_name
43    self.comment = comment
44  end
45
46  ##
47  # Order by #singleton then #new_name
48
49  def <=>(other)
50    [@singleton ? 0 : 1, new_name] <=> [other.singleton ? 0 : 1, other.new_name]
51  end
52
53  ##
54  # HTML fragment reference for this alias
55
56  def aref
57    type = singleton ? 'c' : 'i'
58    "#alias-#{type}-#{html_name}"
59  end
60
61  ##
62  # Full old name including namespace
63
64  def full_old_name
65    @full_name || "#{parent.name}#{pretty_old_name}"
66  end
67
68  ##
69  # HTML id-friendly version of +#new_name+.
70
71  def html_name
72    CGI.escape(@new_name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
73  end
74
75  def inspect # :nodoc:
76    parent_name = parent ? parent.name : '(unknown)'
77    "#<%s:0x%x %s.alias_method %s, %s>" % [
78      self.class, object_id,
79      parent_name, @old_name, @new_name,
80    ]
81  end
82
83  ##
84  # '::' for the alias of a singleton method/attribute, '#' for instance-level.
85
86  def name_prefix
87    singleton ? '::' : '#'
88  end
89
90  ##
91  # Old name with prefix '::' or '#'.
92
93  def pretty_old_name
94    "#{singleton ? '::' : '#'}#{@old_name}"
95  end
96
97  ##
98  # New name with prefix '::' or '#'.
99
100  def pretty_new_name
101    "#{singleton ? '::' : '#'}#{@new_name}"
102  end
103
104  alias pretty_name pretty_new_name
105
106  def to_s # :nodoc:
107    "alias: #{self.new_name} -> #{self.pretty_old_name} in: #{parent}"
108  end
109
110end
111
112