1
require 'rdoc/code_object'
1
require 'rdoc/method_attr'
4
4
# An attribute created by \#attr, \#attr_reader, \#attr_writer or
7
class RDoc::Attr < RDoc::CodeObject
9
MARSHAL_VERSION = 0 # :nodoc:
12
# Name of the attribute
17
# Is the attribute readable, writable or both?
7
class RDoc::Attr < RDoc::MethodAttr
9
MARSHAL_VERSION = 2 # :nodoc:
12
# Is the attribute readable ('R'), writable ('W') or both ('RW')?
22
# Source file token stream
27
# public, protected, private
29
attr_accessor :visibility
31
def initialize(text, name, rw, comment)
17
# Creates a new Attr with body +text+, +name+, read/write status +rw+ and
18
# +comment+. +singleton+ marks this as a class attribute.
20
def initialize(text, name, rw, comment, singleton = false)
24
@singleton = singleton
37
25
self.comment = comment
41
# Attributes are ordered by name
44
self.name <=> other.name
48
# Attributes are equal when their names and rw is identical
29
# Attributes are equal when their names, singleton and rw are identical
51
32
self.class == other.class and
52
33
self.name == other.name and
57
# Returns nil, for duck typing with RDoc::AnyMethod
63
# Returns nil, for duck typing with RDoc::AnyMethod
69
# Returns nil, for duck typing with RDoc::AnyMethod
75
# Partially bogus as Attr has no parent. For duck typing with
79
@full_name ||= "#{@parent ? @parent.full_name : '(unknown)'}##{name}"
83
# An HTML id-friendly representation of #name
86
@name.gsub(/[^a-z]+/, '-')
34
self.rw == other.rw and
35
self.singleton == other.singleton
39
# Add +an_alias+ as an attribute in +context+.
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)
45
new_attr.record_location an_alias.file
46
new_attr.visibility = self.visibility
47
new_attr.is_alias_for = self
49
context.add_attribute new_attr
54
# The #aref prefix for attributes
61
# Returns attr_reader, attr_writer or attr_accessor as appropriate.
65
when 'RW' then 'attr_accessor'
66
when 'R' then 'attr_reader'
67
when 'W' then 'attr_writer'
89
71
def inspect # :nodoc:
91
when 'RW' then :attr_accessor
92
when 'R' then :attr_reader
93
when 'W' then :attr_writer
98
"#<%s:0x%x %s.%s :%s>" % [
99
self.class, object_id,
100
parent_name, attr, @name,
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,
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:
124
106
def marshal_load array
126
109
@full_name = array[2]
128
111
@visibility = array[4]
129
112
@comment = array[5]
113
@singleton = array[6] || false # MARSHAL_VERSION == 0
115
@file = RDoc::TopLevel.new array[7] if version > 1
131
117
@parent_name = @full_name
135
# Name of our parent with special handling for un-marshaled methods
138
@parent_name || super
142
# For duck typing with RDoc::AnyMethod, returns nil
149
# URL path for this attribute
152
"#{@parent.path}##{@name}"
156
# For duck typing with RDoc::AnyMethod
120
def pretty_print q # :nodoc:
121
q.group 2, "[#{self.class.name} #{full_name} #{rw} #{visibility}", "]" do
122
unless comment.empty? then
162
131
def to_s # :nodoc:
163
"#{type} #{name}\n#{comment}"
167
# Returns attr_reader, attr_writer or attr_accessor as appropriate
171
when 'RW' then 'attr_accessor'
172
when 'R' then 'attr_reader'
173
when 'W' then 'attr_writer'
132
"#{definition} #{name} in: #{parent}"