@@ -13,16 +13,18 @@ class Definition
1313 # @parameter parent [Symbol] The parent lexical scope.
1414 # @parameter language [Language] The language in which the symbol is defined in.
1515 # @parameter comments [Array(String)] The comments associated with the definition.
16- def initialize ( name , parent : nil , language : parent . language , comments : nil )
17- @name = name
16+ def initialize ( path , parent : nil , language : parent . language , comments : nil , visibility : :public )
17+ @path = Array ( path ) . map ( & :to_sym )
1818
1919 @parent = parent
2020 @language = language
2121
2222 @comments = comments
23+ @visibility = visibility
2324
24- @path = nil
25+ @full_path = nil
2526 @qualified_name = nil
27+ @nested_name = nil
2628 end
2729
2830 def inspect
@@ -34,7 +36,24 @@ def inspect
3436 # The symbol name.
3537 # e.g. `:Decode`.
3638 # @attribute [Symbol]
37- attr :name
39+ def name
40+ @path . last
41+ end
42+
43+ # The path to the definition, relative to the parent.
44+ # @attribute [Array(Symbol)]
45+ attr :path
46+
47+ # The full path to the definition.
48+ def full_path
49+ @full_path ||= begin
50+ if @parent
51+ @parent . full_path + @path
52+ else
53+ @path
54+ end
55+ end
56+ end
3857
3958 # The parent definition, defining lexical scope.
4059 # @attribute [Definition | Nil]
@@ -62,17 +81,17 @@ def public?
6281 def qualified_name
6382 @qualified_name ||= begin
6483 if @parent
65- @parent . qualified_name + self . nested_name
84+ [ @parent . qualified_name , self . nested_name ] . join ( "::" )
6685 else
67- @name . to_s
86+ self . nested_name
6887 end
6988 end
7089 end
7190
72- # The name of this definition plus the nesting prefix .
91+ # The name relative to the parent .
7392 # @returns [String]
7493 def nested_name
75- ":: #{ @name } "
94+ @nested_name ||= " #{ @path . join ( "::" ) } "
7695 end
7796
7897 # Does the definition name match the specified prefix?
@@ -86,28 +105,6 @@ def convert(kind)
86105 raise ArgumentError , "Unable to convert #{ self } into #{ kind } !"
87106 end
88107
89- # The lexical scope as an array of names.
90- # e.g. `[:Decode, :Definition]`
91- # @returns [Array]
92- def path
93- if @path
94- # Cached version:
95- @path
96- elsif @parent
97- # Merge with parent:
98- @path = [ *@parent . path , *path_name ] . freeze
99- else
100- # At top:
101- @path = path_name . freeze
102- end
103- end
104-
105- def path_name
106- [ @name ]
107- end
108-
109- alias lexical_path path
110-
111108 # A short form of the definition.
112109 # e.g. `def short_form`.
113110 #
@@ -173,5 +170,9 @@ def documentation
173170 def location
174171 nil
175172 end
173+
174+ # The visibility of the definition.
175+ # @attribute [Symbol] :public, :private, :protected
176+ attr_accessor :visibility
176177 end
177178end
0 commit comments