Class: RBS::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/namespace.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/rbs/namespace.rb', line 5

def path
  @path
end

Class Method Details

.emptyObject



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

.rootObject



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

Returns:

  • (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

#ascendObject



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

Returns:

  • (Boolean)


55
56
57
# File 'lib/rbs/namespace.rb', line 55

def empty?
  path.empty?
end

#hashObject



65
66
67
# File 'lib/rbs/namespace.rb', line 65

def hash
  path.hash ^ absolute?.hash
end

#parentObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/rbs/namespace.rb', line 43

def relative?
  !absolute?
end

#splitObject



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_sObject



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

#to_type_nameObject



84
85
86
87
88
89
90
91
# File 'lib/rbs/namespace.rb', line 84

def to_type_name
  parent, name = split

  raise unless name
  raise unless parent

  TypeName.new(name: name, namespace: parent)
end