Class: RuboCop::SketchUp::Namespace

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

Constant Summary collapse

SEPARATOR =
'::'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Namespace

Returns a new instance of Namespace.

Parameters:

  • namespace (String)


12
13
14
15
16
17
18
# File 'lib/rubocop/sketchup/namespace.rb', line 12

def initialize(namespace)
  unless namespace.is_a?(String)
    raise TypeError, "Expected String, got #{namespace.class.name}"
  end

  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/rubocop/sketchup/namespace.rb', line 7

def namespace
  @namespace
end

Instance Method Details

#firstObject

Get the first component of a namespace relative to Object. May return ‘Object’ if the namespace is in the global namespace.



22
23
24
# File 'lib/rubocop/sketchup/namespace.rb', line 22

def first
  parts.find { |name| name != 'Object' } || 'Object'
end

#from_rootObject

Get a namespace string that is relative to Object.



27
28
29
30
31
# File 'lib/rubocop/sketchup/namespace.rb', line 27

def from_root
  items = parts
  items.shift if items.size > 1 && items.first == 'Object'
  items.join(SEPARATOR)
end

#join(other) ⇒ Object



33
34
35
# File 'lib/rubocop/sketchup/namespace.rb', line 33

def join(other)
  self.class.new("#{@namespace}#{SEPARATOR}#{other}")
end

#partsObject

Get the first component of a namespace relative to Object. May return ‘Object’ if the namespace is in the global namespace.



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

def parts
  namespace.split(SEPARATOR)
end

#top_level?Boolean

Returns:

  • (Boolean)


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

def top_level?
  %w[Kernel Object].include?(parts.first)
end