Class: LayerChecker::CodeAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/layer_checker/code_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, project_root: Dir.pwd) ⇒ CodeAnalyzer

Returns a new instance of CodeAnalyzer.



9
10
11
12
13
# File 'lib/layer_checker/code_analyzer.rb', line 9

def initialize(config, project_root: Dir.pwd)
  @config = config
  @project_root = project_root
  @file_dependencies = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/layer_checker/code_analyzer.rb', line 7

def config
  @config
end

#file_dependenciesObject (readonly)

Returns the value of attribute file_dependencies.



7
8
9
# File 'lib/layer_checker/code_analyzer.rb', line 7

def file_dependencies
  @file_dependencies
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



7
8
9
# File 'lib/layer_checker/code_analyzer.rb', line 7

def project_root
  @project_root
end

Instance Method Details

#analyzeObject

Scan all Ruby files in the project



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/layer_checker/code_analyzer.rb', line 16

def analyze
  ruby_files = find_ruby_files
  
  ruby_files.each do |file_path|
    relative_path = file_path.sub("#{@project_root}/", '')
    dependencies = DependencyExtractor.extract(file_path)
    
    @file_dependencies[relative_path] = {
      absolute_path: file_path,
      module: @config.module_for_file(relative_path),
      dependencies: dependencies
    }
  end

  @file_dependencies
end

#files_for_module(mod) ⇒ Object

Get all files that belong to a specific module



34
35
36
# File 'lib/layer_checker/code_analyzer.rb', line 34

def files_for_module(mod)
  @file_dependencies.select { |_, info| info[:module] == mod }
end

#unassigned_filesObject

Get files that don’t belong to any module



39
40
41
# File 'lib/layer_checker/code_analyzer.rb', line 39

def unassigned_files
  @file_dependencies.select { |_, info| info[:module].nil? }
end