constant.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ##
  2. # A constant
  3. class RDoc::Constant < RDoc::CodeObject
  4. ##
  5. # If this constant is an alias for a module or class,
  6. # this is the RDoc::ClassModule it is an alias for.
  7. # +nil+ otherwise.
  8. attr_accessor :is_alias_for
  9. ##
  10. # The constant's name
  11. attr_accessor :name
  12. ##
  13. # The constant's value
  14. attr_accessor :value
  15. ##
  16. # Creates a new constant with +name+, +value+ and +comment+
  17. def initialize(name, value, comment)
  18. super()
  19. @name = name
  20. @value = value
  21. @is_alias_for = nil
  22. self.comment = comment
  23. end
  24. ##
  25. # Constants are ordered by name
  26. def <=> other
  27. return unless self.class === other
  28. [parent_name, name] <=> [other.parent_name, other.name]
  29. end
  30. ##
  31. # Constants are equal when their #parent and #name is the same
  32. def == other
  33. self.class == other.class and
  34. @parent == other.parent and
  35. @name == other.name
  36. end
  37. ##
  38. # A constant is documented if it has a comment, or is an alias
  39. # for a documented class or module.
  40. def documented?
  41. super or is_alias_for && is_alias_for.documented?
  42. end
  43. def inspect # :nodoc:
  44. "#<%s:0x%x %s::%s>" % [
  45. self.class, object_id,
  46. parent_name, @name,
  47. ]
  48. end
  49. ##
  50. # Path to this constant
  51. def path
  52. "#{@parent.path}##{@name}"
  53. end
  54. def to_s # :nodoc:
  55. parent_name = parent ? parent.full_name : '(unknown)'
  56. if is_alias_for
  57. "constant #{parent_name}::#@name -> #{is_alias_for}"
  58. else
  59. "constant #{parent_name}::#@name"
  60. end
  61. end
  62. end