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.



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

def initialize(path:, absolute:)
  @path = path
  @absolute = absolute
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/rbs/namespace.rb', line 3

def path
  @path
end

Class Method Details

.emptyObject



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

.rootObject



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

Returns:

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

#ascendObject



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

Returns:

  • (Boolean)


51
52
53
# File 'lib/rbs/namespace.rb', line 51

def empty?
  path.empty?
end

#hashObject



61
62
63
# File 'lib/rbs/namespace.rb', line 61

def hash
  self.class.hash ^ path.hash ^ absolute?.hash
end

#parentObject



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

Returns:

  • (Boolean)


39
40
41
# File 'lib/rbs/namespace.rb', line 39

def relative?
  !absolute?
end

#splitObject



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

def split
  [parent, path.last]
end

#to_sObject



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

#to_type_nameObject



78
79
80
81
# File 'lib/rbs/namespace.rb', line 78

def to_type_name
  parent, name = split
  TypeName.new(name: name, namespace: parent)
end