Class: DepsGrapher::SourceCache::ClassNameExtractor

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/deps_grapher/source_cache/class_name_extractor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ClassNameExtractor

Returns a new instance of ClassNameExtractor.



12
13
14
15
# File 'lib/deps_grapher/source_cache/class_name_extractor.rb', line 12

def initialize(&block)
  super()
  @block = block
end

Class Method Details

.cacheObject



7
8
9
# File 'lib/deps_grapher/source_cache/class_name_extractor.rb', line 7

def cache
  @cache ||= Set.new
end

Instance Method Details

#extract!(file_path) ⇒ Object



17
18
19
20
21
# File 'lib/deps_grapher/source_cache/class_name_extractor.rb', line 17

def extract!(file_path)
  source_buffer = Parser::Source::Buffer.new(file_path)
  parser = Prism::Translation::Parser.new
  process parser.parse(source_buffer.read)
end

#on_module(node) ⇒ Object Also known as: on_class



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/deps_grapher/source_cache/class_name_extractor.rb', line 23

def on_module(node)
  const_node = node.children[0]
  name = extract_const_name const_node

  @namespace_stack ||= []
  @namespace_stack.push name

  super

  const_name = @namespace_stack.join("::")

  unless self.class.cache.include? const_name
    self.class.cache << const_name
    @block.call const_name, node.location.name.source_buffer.name
  end

  @namespace_stack.pop
end