Class: Ace::Support::Config::Molecules::ConfigFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/config/molecules/config_finder.rb

Overview

Find configuration files in cascade paths

Constant Summary collapse

DEFAULT_FILE_PATTERNS =

Common config file patterns

%w[
  settings.yml
  settings.yaml
  config.yml
  config.yaml
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir: ".ace", defaults_dir: ".ace-defaults", gem_path: nil, file_patterns: DEFAULT_FILE_PATTERNS, use_traversal: true, start_path: nil) ⇒ ConfigFinder

Initialize finder with configurable paths

Parameters:

  • config_dir (String) (defaults to: ".ace")

    User config folder name (default: “.ace”)

  • defaults_dir (String) (defaults to: ".ace-defaults")

    Gem defaults folder name (default: “.ace-defaults”)

  • gem_path (String, nil) (defaults to: nil)

    Gem root path for defaults

  • file_patterns (Array<String>) (defaults to: DEFAULT_FILE_PATTERNS)

    File patterns to look for

  • use_traversal (Boolean) (defaults to: true)

    Whether to use directory traversal (default: true)

  • start_path (String, nil) (defaults to: nil)

    Starting path for traversal (default: Dir.pwd)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ace/support/config/molecules/config_finder.rb', line 26

def initialize(
  config_dir: ".ace",
  defaults_dir: ".ace-defaults",
  gem_path: nil,
  file_patterns: DEFAULT_FILE_PATTERNS,
  use_traversal: true,
  start_path: nil
)
  @config_dir = config_dir
  @defaults_dir = defaults_dir
  @gem_path = gem_path
  @file_patterns = file_patterns
  @use_traversal = use_traversal
  @start_path = start_path ? Ace::Support::Fs::Atoms::PathExpander.expand(start_path) : Dir.pwd
  @search_paths = build_search_paths
end

Instance Attribute Details

#config_dirObject (readonly)

Returns the value of attribute config_dir.



17
18
19
# File 'lib/ace/support/config/molecules/config_finder.rb', line 17

def config_dir
  @config_dir
end

#defaults_dirObject (readonly)

Returns the value of attribute defaults_dir.



17
18
19
# File 'lib/ace/support/config/molecules/config_finder.rb', line 17

def defaults_dir
  @defaults_dir
end

#file_patternsObject (readonly)

Returns the value of attribute file_patterns.



17
18
19
# File 'lib/ace/support/config/molecules/config_finder.rb', line 17

def file_patterns
  @file_patterns
end

#gem_pathObject (readonly)

Returns the value of attribute gem_path.



17
18
19
# File 'lib/ace/support/config/molecules/config_finder.rb', line 17

def gem_path
  @gem_path
end

#search_pathsArray<String> (readonly)

Get the search paths being used

Returns:

  • (Array<String>)

    Ordered list of search paths



119
120
121
# File 'lib/ace/support/config/molecules/config_finder.rb', line 119

def search_paths
  @search_paths
end

#start_pathObject (readonly)

Returns the value of attribute start_path.



17
18
19
# File 'lib/ace/support/config/molecules/config_finder.rb', line 17

def start_path
  @start_path
end

Instance Method Details

#find_allArray<Models::CascadePath>

Find all config files in cascade order

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ace/support/config/molecules/config_finder.rb', line 45

def find_all
  paths = []

  @search_paths.each_with_index do |base_path, index|
    priority = index * 10 # Lower index = higher priority

    @file_patterns.each do |pattern|
      found = find_in_path(base_path, pattern, priority)
      paths.concat(found)
    end
  end

  # Add gem defaults with lowest priority
  gem_config = find_gem_defaults
  paths.concat(gem_config) if gem_config

  paths.sort
end

#find_all_files(filename) ⇒ Array<String>

Find all instances of a config file in the cascade

Parameters:

  • filename (String)

    Specific filename to find

Returns:

  • (Array<String>)

    All found file paths in cascade order



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ace/support/config/molecules/config_finder.rb', line 99

def find_all_files(filename)
  files = []

  # Use pre-built search_paths (respects @use_traversal setting)
  @search_paths.each do |dir|
    file_path = File.join(dir, filename)
    files << file_path if File.exist?(file_path)
  end

  # Check gem defaults if available
  if @gem_path
    gem_default_path = File.join(@gem_path, @defaults_dir, filename)
    files << gem_default_path if File.exist?(gem_default_path)
  end

  files
end

#find_by_type(type) ⇒ Array<Models::CascadePath>

Find configs by type

Parameters:

  • type (Symbol)

    Type to filter (:local, :home, :gem)

Returns:



73
74
75
# File 'lib/ace/support/config/molecules/config_finder.rb', line 73

def find_by_type(type)
  find_all.select { |path| path.type == type }
end

#find_file(filename) ⇒ String?

Find a specific config file using the cascade

Parameters:

  • filename (String)

    Specific filename to find

Returns:

  • (String, nil)

    Path to the first found config file



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ace/support/config/molecules/config_finder.rb', line 80

def find_file(filename)
  # Use pre-built search_paths (respects @use_traversal setting)
  @search_paths.each do |dir|
    file_path = File.join(dir, filename)
    return file_path if File.exist?(file_path)
  end

  # Check gem defaults if available
  if @gem_path
    gem_default_path = File.join(@gem_path, @defaults_dir, filename)
    return gem_default_path if File.exist?(gem_default_path)
  end

  nil
end

#find_firstModels::CascadePath?

Find first existing config file

Returns:



66
67
68
# File 'lib/ace/support/config/molecules/config_finder.rb', line 66

def find_first
  find_all.find(&:exists)
end