Class: Steep::AST::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/ast/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.



6
7
8
9
# File 'lib/steep/ast/namespace.rb', line 6

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/steep/ast/namespace.rb', line 4

def path
  @path
end

Class Method Details

.emptyObject



11
12
13
# File 'lib/steep/ast/namespace.rb', line 11

def self.empty
  new(path: [], absolute: false)
end

.parse(string) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/steep/ast/namespace.rb', line 71

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



15
16
17
# File 'lib/steep/ast/namespace.rb', line 15

def self.root
  new(path: [], absolute: true)
end

Instance Method Details

#+(other) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/steep/ast/namespace.rb', line 19

def +(other)
  if other.absolute?
    other
  else
    self.class.new(path: path + other.path, absolute: absolute?)
  end
end

#==(other) ⇒ Object Also known as: eql?



52
53
54
# File 'lib/steep/ast/namespace.rb', line 52

def ==(other)
  other.is_a?(Namespace) && other.path == path && other.absolute? == absolute?
end

#absolute!Object



44
45
46
# File 'lib/steep/ast/namespace.rb', line 44

def absolute!
  self.class.new(path: path, absolute: true)
end

#absolute?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/steep/ast/namespace.rb', line 36

def absolute?
  @absolute
end

#append(component) ⇒ Object



27
28
29
# File 'lib/steep/ast/namespace.rb', line 27

def append(component)
  self.class.new(path: path + [component], absolute: absolute?)
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/steep/ast/namespace.rb', line 48

def empty?
  path.empty?
end

#hashObject



58
59
60
# File 'lib/steep/ast/namespace.rb', line 58

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

#parentObject



31
32
33
34
# File 'lib/steep/ast/namespace.rb', line 31

def parent
  raise "Parent with empty namespace" if empty?
  self.class.new(path: path.take(path.size - 1), absolute: absolute?)
end

#relative?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/steep/ast/namespace.rb', line 40

def relative?
  !absolute?
end

#to_sObject



62
63
64
65
66
67
68
69
# File 'lib/steep/ast/namespace.rb', line 62

def to_s
  if empty?
    absolute? ? "::" : ""
  else
    s = path.join("::")
    absolute? ? "::#{s}::" : "#{s}::"
  end
end