[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / lib / rdoc / alias.rb
blob446cf9ccb4855f41d59573edc5571e01c7d8997c
1 # frozen_string_literal: true
2 ##
3 # Represent an alias, which is an old_name/new_name pair associated with a
4 # particular context
5 #--
6 # TODO implement Alias as a proxy to a method/attribute, inheriting from
7 #      MethodAttr
9 class RDoc::Alias < RDoc::CodeObject
11   ##
12   # Aliased method's name
14   attr_reader :new_name
16   alias name new_name
18   ##
19   # Aliasee method's name
21   attr_reader :old_name
23   ##
24   # Is this an alias declared in a singleton context?
26   attr_accessor :singleton
28   ##
29   # Source file token stream
31   attr_reader :text
33   ##
34   # Creates a new Alias with a token stream of +text+ that aliases +old_name+
35   # to +new_name+, has +comment+ and is a +singleton+ context.
37   def initialize(text, old_name, new_name, comment, singleton = false)
38     super()
40     @text = text
41     @singleton = singleton
42     @old_name = old_name
43     @new_name = new_name
44     self.comment = comment
45   end
47   ##
48   # Order by #singleton then #new_name
50   def <=>(other)
51     [@singleton ? 0 : 1, new_name] <=> [other.singleton ? 0 : 1, other.new_name]
52   end
54   ##
55   # HTML fragment reference for this alias
57   def aref
58     type = singleton ? 'c' : 'i'
59     "#alias-#{type}-#{html_name}"
60   end
62   ##
63   # Full old name including namespace
65   def full_old_name
66     @full_name || "#{parent.name}#{pretty_old_name}"
67   end
69   ##
70   # HTML id-friendly version of +#new_name+.
72   def html_name
73     CGI.escape(@new_name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
74   end
76   def inspect # :nodoc:
77     parent_name = parent ? parent.name : '(unknown)'
78     "#<%s:0x%x %s.alias_method %s, %s>" % [
79       self.class, object_id,
80       parent_name, @old_name, @new_name,
81     ]
82   end
84   ##
85   # '::' for the alias of a singleton method/attribute, '#' for instance-level.
87   def name_prefix
88     singleton ? '::' : '#'
89   end
91   ##
92   # Old name with prefix '::' or '#'.
94   def pretty_old_name
95     "#{singleton ? '::' : '#'}#{@old_name}"
96   end
98   ##
99   # New name with prefix '::' or '#'.
101   def pretty_new_name
102     "#{singleton ? '::' : '#'}#{@new_name}"
103   end
105   alias pretty_name pretty_new_name
107   def to_s # :nodoc:
108     "alias: #{self.new_name} -> #{self.pretty_old_name} in: #{parent}"
109   end