Class: Steep::ModuleName

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/module_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, absolute:) ⇒ ModuleName

Returns a new instance of ModuleName.



5
6
7
8
# File 'lib/steep/module_name.rb', line 5

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/steep/module_name.rb', line 3

def name
  @name
end

Class Method Details

.from_node(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steep/module_name.rb', line 15

def self.from_node(node)
  case node.type
  when :const
    relative_node = new(name: node.children.last.to_s, absolute: false)
    parent_node = node.children.first

    case parent_node&.type
    when :cbase
      relative_node.absolute!
    when nil
      relative_node
    else
      from_node(parent_node)&.yield_self do |parent|
        parent + relative_node
      end
    end
  when :casgn
    relative_node = new(name: node.children[1].to_s, absolute: false)
    parent_node = node.children.first

    case parent_node&.type
    when :cbase
      relative_node.absolute!
    when nil
      relative_node
    else
      from_node(parent_node)&.yield_self do |parent|
        parent + relative_node
      end
    end
  else
    nil
  end
end

.parse(name) ⇒ Object



10
11
12
13
# File 'lib/steep/module_name.rb', line 10

def self.parse(name)
  name = name.to_s
  new(name: name.gsub(/\A::/, ""), absolute: name.start_with?("::"))
end

Instance Method Details

#+(other) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/steep/module_name.rb', line 80

def +(other)
  case other
  when self.class
    if other.absolute?
      other
    else
      self.class.new(name: "#{name}::#{other.name}", absolute: absolute?)
    end
  else
    self + self.class.parse(other)
  end
end

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



50
51
52
# File 'lib/steep/module_name.rb', line 50

def ==(other)
  other.is_a?(self.class) && other.name == name && other.absolute? == absolute?
end

#absolute!Object



60
61
62
# File 'lib/steep/module_name.rb', line 60

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

#absolute?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/steep/module_name.rb', line 64

def absolute?
  !!@absolute
end

#componentsObject



93
94
95
96
97
98
99
100
101
# File 'lib/steep/module_name.rb', line 93

def components
  name.split(/::/).map.with_index {|s, index|
    if index == 0 && absolute?
      self.class.parse(s).absolute!
    else
      self.class.parse(s)
    end
  }
end

#hashObject



54
55
56
# File 'lib/steep/module_name.rb', line 54

def hash
  self.class.hash ^ name.hash ^ @absolute.hash
end

#parentObject



103
104
105
106
107
108
109
110
# File 'lib/steep/module_name.rb', line 103

def parent
  components = components()
  components.pop

  unless components.empty?
    self.class.parse(components.join("::"))
  end
end

#relative?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/steep/module_name.rb', line 68

def relative?
  !absolute?
end

#simple?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/steep/module_name.rb', line 112

def simple?
  components.size == 1
end

#to_sObject



72
73
74
75
76
77
78
# File 'lib/steep/module_name.rb', line 72

def to_s
  if absolute?
    "::#{name}"
  else
    name
  end
end