Class: Ace::Support::Config::Molecules::ConfigFinder
- Inherits:
-
Object
- Object
- Ace::Support::Config::Molecules::ConfigFinder
- 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
-
#config_dir ⇒ Object
readonly
Returns the value of attribute config_dir.
-
#defaults_dir ⇒ Object
readonly
Returns the value of attribute defaults_dir.
-
#file_patterns ⇒ Object
readonly
Returns the value of attribute file_patterns.
-
#gem_path ⇒ Object
readonly
Returns the value of attribute gem_path.
-
#search_paths ⇒ Array<String>
readonly
Get the search paths being used.
-
#start_path ⇒ Object
readonly
Returns the value of attribute start_path.
Instance Method Summary collapse
-
#find_all ⇒ Array<Models::CascadePath>
Find all config files in cascade order.
-
#find_all_files(filename) ⇒ Array<String>
Find all instances of a config file in the cascade.
-
#find_by_type(type) ⇒ Array<Models::CascadePath>
Find configs by type.
-
#find_file(filename) ⇒ String?
Find a specific config file using the cascade.
-
#find_first ⇒ Models::CascadePath?
Find first existing config file.
-
#initialize(config_dir: ".ace", defaults_dir: ".ace-defaults", gem_path: nil, file_patterns: DEFAULT_FILE_PATTERNS, use_traversal: true, start_path: nil) ⇒ ConfigFinder
constructor
Initialize finder with configurable paths.
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
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.(start_path) : Dir.pwd @search_paths = build_search_paths end |
Instance Attribute Details
#config_dir ⇒ Object (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_dir ⇒ Object (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_patterns ⇒ Object (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_path ⇒ Object (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_paths ⇒ Array<String> (readonly)
Get the search paths being used
119 120 121 |
# File 'lib/ace/support/config/molecules/config_finder.rb', line 119 def search_paths @search_paths end |
#start_path ⇒ Object (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_all ⇒ Array<Models::CascadePath>
Find all config files in cascade order
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
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
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
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_first ⇒ Models::CascadePath?
Find first existing config file
66 67 68 |
# File 'lib/ace/support/config/molecules/config_finder.rb', line 66 def find_first find_all.find(&:exists) end |