attr.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ##
  2. # An attribute created by \#attr, \#attr_reader, \#attr_writer or
  3. # \#attr_accessor
  4. class RDoc::Attr < RDoc::MethodAttr
  5. MARSHAL_VERSION = 2 # :nodoc:
  6. ##
  7. # Is the attribute readable ('R'), writable ('W') or both ('RW')?
  8. attr_accessor :rw
  9. ##
  10. # Creates a new Attr with body +text+, +name+, read/write status +rw+ and
  11. # +comment+. +singleton+ marks this as a class attribute.
  12. def initialize(text, name, rw, comment, singleton = false)
  13. super text, name
  14. @rw = rw
  15. @singleton = singleton
  16. self.comment = comment
  17. end
  18. ##
  19. # Attributes are equal when their names, singleton and rw are identical
  20. def == other
  21. self.class == other.class and
  22. self.name == other.name and
  23. self.rw == other.rw and
  24. self.singleton == other.singleton
  25. end
  26. ##
  27. # Add +an_alias+ as an attribute in +context+.
  28. def add_alias(an_alias, context)
  29. new_attr = self.class.new(self.text, an_alias.new_name, self.rw,
  30. self.comment, self.singleton)
  31. new_attr.record_location an_alias.file
  32. new_attr.visibility = self.visibility
  33. new_attr.is_alias_for = self
  34. @aliases << new_attr
  35. context.add_attribute new_attr
  36. new_attr
  37. end
  38. ##
  39. # The #aref prefix for attributes
  40. def aref_prefix
  41. 'attribute'
  42. end
  43. ##
  44. # Returns attr_reader, attr_writer or attr_accessor as appropriate.
  45. def definition
  46. case @rw
  47. when 'RW' then 'attr_accessor'
  48. when 'R' then 'attr_reader'
  49. when 'W' then 'attr_writer'
  50. end
  51. end
  52. def inspect # :nodoc:
  53. alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
  54. visibility = self.visibility
  55. visibility = "forced #{visibility}" if force_documentation
  56. "#<%s:0x%x %s %s (%s)%s>" % [
  57. self.class, object_id,
  58. full_name,
  59. rw,
  60. visibility,
  61. alias_for,
  62. ]
  63. end
  64. ##
  65. # Dumps this Attr for use by ri. See also #marshal_load
  66. def marshal_dump
  67. [ MARSHAL_VERSION,
  68. @name,
  69. full_name,
  70. @rw,
  71. @visibility,
  72. parse(@comment),
  73. singleton,
  74. @file.absolute_name,
  75. ]
  76. end
  77. ##
  78. # Loads this Attr from +array+. For a loaded Attr the following
  79. # methods will return cached values:
  80. #
  81. # * #full_name
  82. # * #parent_name
  83. def marshal_load array
  84. version = array[0]
  85. @name = array[1]
  86. @full_name = array[2]
  87. @rw = array[3]
  88. @visibility = array[4]
  89. @comment = array[5]
  90. @singleton = array[6] || false # MARSHAL_VERSION == 0
  91. @file = RDoc::TopLevel.new array[7] if version > 1
  92. @parent_name = @full_name
  93. end
  94. def pretty_print q # :nodoc:
  95. q.group 2, "[#{self.class.name} #{full_name} #{rw} #{visibility}", "]" do
  96. unless comment.empty? then
  97. q.breakable
  98. q.text "comment:"
  99. q.breakable
  100. q.pp @comment
  101. end
  102. end
  103. end
  104. def to_s # :nodoc:
  105. "#{definition} #{name} in: #{parent}"
  106. end
  107. end