~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/rdoc/attr.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'rdoc/code_object'
 
1
require 'rdoc/method_attr'
2
2
 
3
3
##
4
4
# An attribute created by \#attr, \#attr_reader, \#attr_writer or
5
5
# \#attr_accessor
6
6
 
7
 
class RDoc::Attr < RDoc::CodeObject
8
 
 
9
 
  MARSHAL_VERSION = 0 # :nodoc:
10
 
 
11
 
  ##
12
 
  # Name of the attribute
13
 
 
14
 
  attr_accessor :name
15
 
 
16
 
  ##
17
 
  # Is the attribute readable, writable or both?
 
7
class RDoc::Attr < RDoc::MethodAttr
 
8
 
 
9
  MARSHAL_VERSION = 2 # :nodoc:
 
10
 
 
11
  ##
 
12
  # Is the attribute readable ('R'), writable ('W') or both ('RW')?
18
13
 
19
14
  attr_accessor :rw
20
15
 
21
16
  ##
22
 
  # Source file token stream
23
 
 
24
 
  attr_accessor :text
25
 
 
26
 
  ##
27
 
  # public, protected, private
28
 
 
29
 
  attr_accessor :visibility
30
 
 
31
 
  def initialize(text, name, rw, comment)
32
 
    super()
33
 
    @text = text
34
 
    @name = name
 
17
  # Creates a new Attr with body +text+, +name+, read/write status +rw+ and
 
18
  # +comment+.  +singleton+ marks this as a class attribute.
 
19
 
 
20
  def initialize(text, name, rw, comment, singleton = false)
 
21
    super text, name
 
22
 
35
23
    @rw = rw
36
 
    @visibility = :public
 
24
    @singleton = singleton
37
25
    self.comment = comment
38
26
  end
39
27
 
40
28
  ##
41
 
  # Attributes are ordered by name
42
 
 
43
 
  def <=>(other)
44
 
    self.name <=> other.name
45
 
  end
46
 
 
47
 
  ##
48
 
  # Attributes are equal when their names and rw is identical
 
29
  # Attributes are equal when their names, singleton and rw are identical
49
30
 
50
31
  def == other
51
32
    self.class == other.class and
52
33
      self.name == other.name and
53
 
      self.rw == other.rw
54
 
  end
55
 
 
56
 
  ##
57
 
  # Returns nil, for duck typing with RDoc::AnyMethod
58
 
 
59
 
  def arglists
60
 
  end
61
 
 
62
 
  ##
63
 
  # Returns nil, for duck typing with RDoc::AnyMethod
64
 
 
65
 
  def block_params
66
 
  end
67
 
 
68
 
  ##
69
 
  # Returns nil, for duck typing with RDoc::AnyMethod
70
 
 
71
 
  def call_seq
72
 
  end
73
 
 
74
 
  ##
75
 
  # Partially bogus as Attr has no parent.  For duck typing with
76
 
  # RDoc::AnyMethod.
77
 
 
78
 
  def full_name
79
 
    @full_name ||= "#{@parent ? @parent.full_name : '(unknown)'}##{name}"
80
 
  end
81
 
 
82
 
  ##
83
 
  # An HTML id-friendly representation of #name
84
 
 
85
 
  def html_name
86
 
    @name.gsub(/[^a-z]+/, '-')
 
34
      self.rw == other.rw and
 
35
      self.singleton == other.singleton
 
36
  end
 
37
 
 
38
  ##
 
39
  # Add +an_alias+ as an attribute in +context+.
 
40
 
 
41
  def add_alias(an_alias, context)
 
42
    new_attr = self.class.new(self.text, an_alias.new_name, self.rw,
 
43
                              self.comment, self.singleton)
 
44
 
 
45
    new_attr.record_location an_alias.file
 
46
    new_attr.visibility = self.visibility
 
47
    new_attr.is_alias_for = self
 
48
    @aliases << new_attr
 
49
    context.add_attribute new_attr
 
50
    new_attr
 
51
  end
 
52
 
 
53
  ##
 
54
  # The #aref prefix for attributes
 
55
 
 
56
  def aref_prefix
 
57
    'attribute'
 
58
  end
 
59
 
 
60
  ##
 
61
  # Returns attr_reader, attr_writer or attr_accessor as appropriate.
 
62
 
 
63
  def definition
 
64
    case @rw
 
65
    when 'RW' then 'attr_accessor'
 
66
    when 'R'  then 'attr_reader'
 
67
    when 'W'  then 'attr_writer'
 
68
    end
87
69
  end
88
70
 
89
71
  def inspect # :nodoc:
90
 
    attr = case rw
91
 
           when 'RW' then :attr_accessor
92
 
           when 'R'  then :attr_reader
93
 
           when 'W'  then :attr_writer
94
 
           else
95
 
               " (#{rw})"
96
 
           end
97
 
 
98
 
      "#<%s:0x%x %s.%s :%s>" % [
99
 
        self.class, object_id,
100
 
        parent_name, attr, @name,
101
 
      ]
 
72
    alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
 
73
    visibility = self.visibility
 
74
    visibility = "forced #{visibility}" if force_documentation
 
75
    "#<%s:0x%x %s %s (%s)%s>" % [
 
76
      self.class, object_id,
 
77
      full_name,
 
78
      rw,
 
79
      visibility,
 
80
      alias_for,
 
81
    ]
102
82
  end
103
83
 
104
84
  ##
111
91
      @rw,
112
92
      @visibility,
113
93
      parse(@comment),
 
94
      singleton,
 
95
      @file.absolute_name,
114
96
    ]
115
97
  end
116
98
 
117
99
  ##
118
 
  # Loads this AnyMethod from +array+.  For a loaded AnyMethod the following
 
100
  # Loads this Attr from +array+.  For a loaded Attr the following
119
101
  # methods will return cached values:
120
102
  #
121
103
  # * #full_name
122
104
  # * #parent_name
123
105
 
124
106
  def marshal_load array
 
107
    version     = array[0]
125
108
    @name       = array[1]
126
109
    @full_name  = array[2]
127
110
    @rw         = array[3]
128
111
    @visibility = array[4]
129
112
    @comment    = array[5]
 
113
    @singleton  = array[6] || false # MARSHAL_VERSION == 0
 
114
 
 
115
    @file = RDoc::TopLevel.new array[7] if version > 1
130
116
 
131
117
    @parent_name = @full_name
132
118
  end
133
119
 
134
 
  ##
135
 
  # Name of our parent with special handling for un-marshaled methods
136
 
 
137
 
  def parent_name
138
 
    @parent_name || super
139
 
  end
140
 
 
141
 
  ##
142
 
  # For duck typing with RDoc::AnyMethod, returns nil
143
 
 
144
 
  def params
145
 
    nil
146
 
  end
147
 
 
148
 
  ##
149
 
  # URL path for this attribute
150
 
 
151
 
  def path
152
 
    "#{@parent.path}##{@name}"
153
 
  end
154
 
 
155
 
  ##
156
 
  # For duck typing with RDoc::AnyMethod
157
 
 
158
 
  def singleton
159
 
    false
 
120
  def pretty_print q # :nodoc:
 
121
    q.group 2, "[#{self.class.name} #{full_name} #{rw} #{visibility}", "]" do
 
122
      unless comment.empty? then
 
123
        q.breakable
 
124
        q.text "comment:"
 
125
        q.breakable
 
126
        q.pp @comment
 
127
      end
 
128
    end
160
129
  end
161
130
 
162
131
  def to_s # :nodoc:
163
 
    "#{type} #{name}\n#{comment}"
164
 
  end
165
 
 
166
 
  ##
167
 
  # Returns attr_reader, attr_writer or attr_accessor as appropriate
168
 
 
169
 
  def type
170
 
    case @rw
171
 
    when 'RW' then 'attr_accessor'
172
 
    when 'R'  then 'attr_reader'
173
 
    when 'W'  then 'attr_writer'
174
 
    end
 
132
    "#{definition} #{name} in: #{parent}"
175
133
  end
176
134
 
177
135
end