Class: CodeQualia::ConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/code_qualia/config_helper.rb

Class Method Summary collapse

Class Method Details

.get_target_patternsArray<String>

Gets target file patterns from configuration and expands them to glob patterns.

This method reads the architectural_weights configuration, expands any brace patterns (e.g., ‘app/models,controllers/*/.rb’), and converts directory patterns to file patterns by appending ‘*/.rb’ when necessary.

Examples:

ConfigHelper.get_target_patterns
# => ["app/models/**/*.rb", "app/controllers/**/*.rb", "lib/**/*.rb"]

Returns:

  • (Array<String>)

    Array of expanded glob patterns for target files



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/code_qualia/config_helper.rb', line 20

def get_target_patterns
  config = load_config
  patterns = config.architectural_weights.map { |entry| entry['path'] }

  # Expand brace patterns and convert to file patterns if they're directory patterns
  expanded_patterns = []
  patterns.each do |pattern|
    if pattern.include?('{') && pattern.include?('}')
      expanded = PatternExpander.expand_brace_patterns(pattern)
      expanded_patterns.concat(expanded)
    else
      expanded_patterns << pattern
    end
  end

  # Convert directory patterns to file patterns
  expanded_patterns.map do |pattern|
    if pattern.end_with?('/')
      "#{pattern}**/*.rb"
    elsif pattern.end_with?('/**/*')
      "#{pattern}.rb"
    elsif !pattern.include?('*')
      # Assume it's a directory
      "#{pattern}/**/*.rb"
    else
      pattern
    end
  end
end

.load_configCodeQualia::Config

Loads the Code Qualia configuration from file or returns default configuration.

This method attempts to load the configuration from the default config file (qualia.yml). If the file doesn’t exist, it returns a new Config instance with default values.

Examples:

config = ConfigHelper.load_config
puts config.architectural_weights
# => [{"path"=>"**/*.rb", "weight"=>1.0}]

Returns:



111
112
113
114
115
116
117
# File 'lib/code_qualia/config_helper.rb', line 111

def load_config
  if File.exist?(DEFAULT_CONFIG_FILE)
    Config.load(DEFAULT_CONFIG_FILE)
  else
    Config.new
  end
end

.normalize_file_path(file_path) ⇒ String

Normalizes a file path to a relative path from the current working directory.

This method takes an absolute file path and converts it to a relative path from the current working directory. This preserves the full directory structure including any intermediate directories like ‘packs/users/’.

Examples:

ConfigHelper.normalize_file_path("/full/path/to/project/packs/users/app/models/user.rb")
# => "packs/users/app/models/user.rb"
ConfigHelper.normalize_file_path("app/models/user.rb")
# => "app/models/user.rb"

Parameters:

  • file_path (String)

    The file path to normalize (can be absolute or relative)

Returns:

  • (String)

    Relative path from current working directory



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/code_qualia/config_helper.rb', line 85

def normalize_file_path(file_path)
  # If already a relative path, return as-is
  return file_path unless Pathname.new(file_path).absolute?

  # Convert absolute path to relative path from current working directory
  current_dir = Dir.pwd
  if file_path.start_with?(current_dir)
    relative_path = file_path[(current_dir.length + 1)..-1]
    return relative_path || file_path
  end

  # If it doesn't start with current directory, return as-is
  file_path
end

.should_include_file?(file_path, target_patterns) ⇒ Boolean

Checks if a file should be included based on target patterns.

This method determines whether a given file path matches any of the provided target patterns. Only Ruby files (.rb extension) are considered for inclusion.

Examples:

patterns = ["app/models/**/*.rb", "lib/**/*.rb"]
ConfigHelper.should_include_file?("app/models/user.rb", patterns)
# => true
ConfigHelper.should_include_file?("app/views/index.html.erb", patterns)
# => false

Parameters:

  • file_path (String)

    The file path to check

  • target_patterns (Array<String>)

    Array of glob patterns to match against

Returns:

  • (Boolean)

    true if the file should be included, false otherwise



64
65
66
67
68
69
70
# File 'lib/code_qualia/config_helper.rb', line 64

def should_include_file?(file_path, target_patterns)
  return false unless file_path.end_with?('.rb')

  target_patterns.any? do |pattern|
    File.fnmatch(pattern, file_path, File::FNM_PATHNAME)
  end
end