Class: RBS::Namespace
- Inherits:
-
Object
- Object
- RBS::Namespace
- Defined in:
- lib/rbs/namespace.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #absolute! ⇒ Object
- #absolute? ⇒ Boolean
- #append(component) ⇒ Object
- #ascend ⇒ Object
- #empty? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(path:, absolute:) ⇒ Namespace
constructor
A new instance of Namespace.
- #parent ⇒ Object
- #relative! ⇒ Object
- #relative? ⇒ Boolean
- #split ⇒ Object
- #to_s ⇒ Object
- #to_type_name ⇒ Object
Constructor Details
#initialize(path:, absolute:) ⇒ Namespace
Returns a new instance of Namespace.
5 6 7 8 |
# File 'lib/rbs/namespace.rb', line 5 def initialize(path:, absolute:) @path = path @absolute = absolute end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/rbs/namespace.rb', line 3 def path @path end |
Class Method Details
.empty ⇒ Object
10 11 12 |
# File 'lib/rbs/namespace.rb', line 10 def self.empty new(path: [], absolute: false) end |
.parse(string) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/rbs/namespace.rb', line 83 def self.parse(string) if string.start_with?("::") new(path: string.split("::").drop(1).map(&:to_sym), absolute: true) else new(path: string.split("::").map(&:to_sym), absolute: false) end end |
.root ⇒ Object
14 15 16 |
# File 'lib/rbs/namespace.rb', line 14 def self.root new(path: [], absolute: true) end |
Instance Method Details
#+(other) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/rbs/namespace.rb', line 18 def +(other) if other.absolute? other else self.class.new(path: path + other.path, absolute: absolute?) end end |
#==(other) ⇒ Object Also known as: eql?
55 56 57 |
# File 'lib/rbs/namespace.rb', line 55 def ==(other) other.is_a?(Namespace) && other.path == path && other.absolute? == absolute? end |
#absolute! ⇒ Object
43 44 45 |
# File 'lib/rbs/namespace.rb', line 43 def absolute! self.class.new(path: path, absolute: true) end |
#absolute? ⇒ Boolean
35 36 37 |
# File 'lib/rbs/namespace.rb', line 35 def absolute? @absolute end |
#append(component) ⇒ Object
26 27 28 |
# File 'lib/rbs/namespace.rb', line 26 def append(component) self.class.new(path: path + [component], absolute: absolute?) end |
#ascend ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rbs/namespace.rb', line 92 def ascend if block_given? current = self until current.empty? yield current current = current.parent end yield current self else enum_for(:ascend) end end |
#empty? ⇒ Boolean
51 52 53 |
# File 'lib/rbs/namespace.rb', line 51 def empty? path.empty? end |
#hash ⇒ Object
61 62 63 |
# File 'lib/rbs/namespace.rb', line 61 def hash self.class.hash ^ path.hash ^ absolute?.hash end |
#parent ⇒ Object
30 31 32 33 |
# File 'lib/rbs/namespace.rb', line 30 def parent raise "Parent with empty namespace" if empty? self.class.new(path: path.take(path.size - 1), absolute: absolute?) end |
#relative! ⇒ Object
47 48 49 |
# File 'lib/rbs/namespace.rb', line 47 def relative! self.class.new(path: path, absolute: false) end |
#relative? ⇒ Boolean
39 40 41 |
# File 'lib/rbs/namespace.rb', line 39 def relative? !absolute? end |
#split ⇒ Object
65 66 67 |
# File 'lib/rbs/namespace.rb', line 65 def split [parent, path.last] end |
#to_s ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/rbs/namespace.rb', line 69 def to_s if empty? absolute? ? "::" : "" else s = path.join("::") absolute? ? "::#{s}::" : "#{s}::" end end |