2017-11-27 13:45:24 +03:00
|
|
|
# frozen_string_literal: true
|
2010-04-01 11:45:16 +04:00
|
|
|
##
|
|
|
|
# A constant
|
|
|
|
|
|
|
|
class RDoc::Constant < RDoc::CodeObject
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
MARSHAL_VERSION = 0 # :nodoc:
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
##
|
2012-11-27 08:28:14 +04:00
|
|
|
# Sets the module or class this is constant is an alias for.
|
2010-12-20 06:22:49 +03:00
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
attr_writer :is_alias_for
|
2010-12-20 06:22:49 +03:00
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
##
|
|
|
|
# The constant's name
|
|
|
|
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
##
|
|
|
|
# The constant's value
|
|
|
|
|
|
|
|
attr_accessor :value
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
##
|
|
|
|
# The constant's visibility
|
|
|
|
|
|
|
|
attr_accessor :visibility
|
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
##
|
|
|
|
# Creates a new constant with +name+, +value+ and +comment+
|
|
|
|
|
|
|
|
def initialize(name, value, comment)
|
|
|
|
super()
|
2012-11-27 08:28:14 +04:00
|
|
|
|
|
|
|
@name = name
|
2010-04-01 11:45:16 +04:00
|
|
|
@value = value
|
2012-11-27 08:28:14 +04:00
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
@is_alias_for = nil
|
2017-09-12 06:42:54 +03:00
|
|
|
@visibility = :public
|
2012-11-27 08:28:14 +04:00
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
self.comment = comment
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Constants are ordered by name
|
|
|
|
|
|
|
|
def <=> other
|
|
|
|
return unless self.class === other
|
|
|
|
|
|
|
|
[parent_name, name] <=> [other.parent_name, other.name]
|
|
|
|
end
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
##
|
|
|
|
# Constants are equal when their #parent and #name is the same
|
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
def == other
|
|
|
|
self.class == other.class and
|
|
|
|
@parent == other.parent and
|
|
|
|
@name == other.name
|
|
|
|
end
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
##
|
|
|
|
# A constant is documented if it has a comment, or is an alias
|
|
|
|
# for a documented class or module.
|
|
|
|
|
|
|
|
def documented?
|
2013-12-03 07:18:52 +04:00
|
|
|
return true if super
|
|
|
|
return false unless @is_alias_for
|
|
|
|
case @is_alias_for
|
|
|
|
when String then
|
|
|
|
found = @store.find_class_or_module @is_alias_for
|
|
|
|
return false unless found
|
|
|
|
@is_alias_for = found
|
|
|
|
end
|
|
|
|
@is_alias_for.documented?
|
2010-12-20 06:22:49 +03:00
|
|
|
end
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
##
|
|
|
|
# Full constant name including namespace
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
@full_name ||= "#{parent_name}::#{@name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The module or class this constant is an alias for
|
|
|
|
|
|
|
|
def is_alias_for
|
|
|
|
case @is_alias_for
|
|
|
|
when String then
|
|
|
|
found = @store.find_class_or_module @is_alias_for
|
|
|
|
@is_alias_for = found if found
|
|
|
|
@is_alias_for
|
|
|
|
else
|
|
|
|
@is_alias_for
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
def inspect # :nodoc:
|
2010-12-20 06:22:49 +03:00
|
|
|
"#<%s:0x%x %s::%s>" % [
|
|
|
|
self.class, object_id,
|
|
|
|
parent_name, @name,
|
|
|
|
]
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-27 08:28:14 +04:00
|
|
|
# Dumps this Constant for use by ri. See also #marshal_load
|
|
|
|
|
|
|
|
def marshal_dump
|
|
|
|
alias_name = case found = is_alias_for
|
|
|
|
when RDoc::CodeObject then found.full_name
|
|
|
|
else found
|
|
|
|
end
|
|
|
|
|
|
|
|
[ MARSHAL_VERSION,
|
|
|
|
@name,
|
|
|
|
full_name,
|
|
|
|
@visibility,
|
|
|
|
alias_name,
|
|
|
|
parse(@comment),
|
2012-11-27 12:54:03 +04:00
|
|
|
@file.relative_name,
|
2012-11-27 08:28:14 +04:00
|
|
|
parent.name,
|
|
|
|
parent.class,
|
|
|
|
section.title,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Loads this Constant from +array+. For a loaded Constant the following
|
|
|
|
# methods will return cached values:
|
|
|
|
#
|
|
|
|
# * #full_name
|
|
|
|
# * #parent_name
|
|
|
|
|
|
|
|
def marshal_load array
|
|
|
|
initialize array[1], nil, array[5]
|
|
|
|
|
|
|
|
@full_name = array[2]
|
2017-09-12 06:42:54 +03:00
|
|
|
@visibility = array[3] || :public
|
2012-11-27 08:28:14 +04:00
|
|
|
@is_alias_for = array[4]
|
|
|
|
# 5 handled above
|
|
|
|
# 6 handled below
|
|
|
|
@parent_name = array[7]
|
|
|
|
@parent_class = array[8]
|
|
|
|
@section_title = array[9]
|
|
|
|
|
|
|
|
@file = RDoc::TopLevel.new array[6]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Path to this constant for use with HTML generator output.
|
2010-04-01 11:45:16 +04:00
|
|
|
|
|
|
|
def path
|
|
|
|
"#{@parent.path}##{@name}"
|
|
|
|
end
|
|
|
|
|
2012-11-27 08:28:14 +04:00
|
|
|
def pretty_print q # :nodoc:
|
|
|
|
q.group 2, "[#{self.class.name} #{full_name}", "]" do
|
|
|
|
unless comment.empty? then
|
|
|
|
q.breakable
|
|
|
|
q.text "comment:"
|
|
|
|
q.breakable
|
|
|
|
q.pp @comment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sets the store for this class or module and its contained code objects.
|
|
|
|
|
|
|
|
def store= store
|
|
|
|
super
|
|
|
|
|
|
|
|
@file = @store.add_file @file.full_name if @file
|
|
|
|
end
|
|
|
|
|
2010-12-20 06:22:49 +03:00
|
|
|
def to_s # :nodoc:
|
|
|
|
parent_name = parent ? parent.full_name : '(unknown)'
|
|
|
|
if is_alias_for
|
|
|
|
"constant #{parent_name}::#@name -> #{is_alias_for}"
|
|
|
|
else
|
|
|
|
"constant #{parent_name}::#@name"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 11:45:16 +04:00
|
|
|
end
|
|
|
|
|