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.
7 8 9 10 |
# File 'lib/rbs/namespace.rb', line 7 def initialize(path:, absolute:) @path = path @absolute = absolute ? true : false end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/rbs/namespace.rb', line 5 def path @path end |
Class Method Details
.empty ⇒ Object
12 13 14 |
# File 'lib/rbs/namespace.rb', line 12 def self.empty @empty ||= new(path: [], absolute: false) end |
.parse(string) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/rbs/namespace.rb', line 93 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
16 17 18 |
# File 'lib/rbs/namespace.rb', line 16 def self.root @root ||= new(path: [], absolute: true) end |
Instance Method Details
#+(other) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/rbs/namespace.rb', line 20 def +(other) if other.absolute? other else self.class.new(path: path + other.path, absolute: absolute?) end end |
#==(other) ⇒ Object Also known as: eql?
59 60 61 |
# File 'lib/rbs/namespace.rb', line 59 def ==(other) other.is_a?(Namespace) && other.path == path && other.absolute? == absolute? end |
#absolute! ⇒ Object
47 48 49 |
# File 'lib/rbs/namespace.rb', line 47 def absolute! self.class.new(path: path, absolute: true) end |
#absolute? ⇒ Boolean
39 40 41 |
# File 'lib/rbs/namespace.rb', line 39 def absolute? @absolute end |
#append(component) ⇒ Object
28 29 30 |
# File 'lib/rbs/namespace.rb', line 28 def append(component) self.class.new(path: path + [component], absolute: absolute?) end |
#ascend ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rbs/namespace.rb', line 101 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
55 56 57 |
# File 'lib/rbs/namespace.rb', line 55 def empty? path.empty? end |
#hash ⇒ Object
65 66 67 |
# File 'lib/rbs/namespace.rb', line 65 def hash path.hash ^ absolute?.hash end |
#parent ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rbs/namespace.rb', line 32 def parent @parent ||= begin raise "Parent with empty namespace" if empty? self.class.new(path: path.take(path.size - 1), absolute: absolute?) end end |
#relative! ⇒ Object
51 52 53 |
# File 'lib/rbs/namespace.rb', line 51 def relative! self.class.new(path: path, absolute: false) end |
#relative? ⇒ Boolean
43 44 45 |
# File 'lib/rbs/namespace.rb', line 43 def relative? !absolute? end |
#split ⇒ Object
69 70 71 72 73 |
# File 'lib/rbs/namespace.rb', line 69 def split last = path.last or return parent = self.parent [parent, last] end |
#to_s ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/rbs/namespace.rb', line 75 def to_s if empty? absolute? ? "::" : "" else s = path.join("::") absolute? ? "::#{s}::" : "#{s}::" end end |