Class: ConfigFileManager
- Inherits:
-
Object
- Object
- ConfigFileManager
- Defined in:
- lib/config_file_manager.rb,
lib/config_file_manager/version.rb
Overview
rubocop:disable Style/StaticClass
Constant Summary collapse
- COLORS =
::Pastel.new
- VERSION =
'0.1.1'
Instance Attribute Summary collapse
-
#config_dir ⇒ String
readonly
Absolute path to the main directory that contains all config files (and subdirectories).
-
#env ⇒ String
readonly
Current environment name.
-
#example_extension ⇒ String
readonly
Extension of the example/dummy version of a config file.
-
#max_dir_depth ⇒ Integer
readonly
Maximum depth of nested directories containing config files.
Instance Method Summary collapse
-
#create_missing_dirs(example_extension: @example_extension, print: false) ⇒ void
Create the missing config directories based on their dummy/example versions.
-
#create_missing_files(example_extension: @example_extension, print: false) ⇒ void
Create the missing config files based on their dummy/example versions.
- #delete_file(*file_name) ⇒ Object
- #dir_exist?(*dir_name) ⇒ Boolean
-
#dirs(example_extension: @example_extension) ⇒ Array<String>
Search for directories under the ‘config_dir` directory with the specified ending (eg. `.example`).
- #file_exist?(*file_name) ⇒ Boolean
- #file_path(*file_name) ⇒ String
-
#files(example_extension: @example_extension, result: [], depth: 0, dir_path: @config_dir) ⇒ Array<String>
Recursively search for files under the ‘config_dir` directory with the specified extension (eg. `.example`).
-
#initialize(config_dir, example_extension: '.example', max_dir_depth: 5, env: 'development') ⇒ ConfigFileManager
constructor
A new instance of ConfigFileManager.
- #load_erb(*file_name) ⇒ String
- #load_file(*file_name) ⇒ String
- #load_yaml(*file_name, env: @env, symbolize: true) ⇒ Hash, Array
-
#missing_dirs(example_extension: @example_extension) ⇒ Array<String>
Absolute paths to missing config directories.
-
#missing_files(example_extension: @example_extension) ⇒ Array<String>
Absolute paths to missing config files.
-
#to_absolute_path(relative_path) ⇒ String
Converts a relative path to an absolute path.
-
#to_absolute_paths(relative_paths) ⇒ Array<String>
Converts a collection of relative paths to an array of absolute paths.
-
#to_relative_path(absolute_path) ⇒ String
Converts an absolute path to a relative path.
-
#to_relative_paths(absolute_paths) ⇒ Array<String>
Converts a collection of absolute paths to an array of relative paths.
Constructor Details
#initialize(config_dir, example_extension: '.example', max_dir_depth: 5, env: 'development') ⇒ ConfigFileManager
Returns a new instance of ConfigFileManager.
39 40 41 42 43 44 |
# File 'lib/config_file_manager.rb', line 39 def initialize(config_dir, example_extension: '.example', max_dir_depth: 5, env: 'development') @config_dir = config_dir @example_extension = example_extension @max_dir_depth = max_dir_depth @env = env end |
Instance Attribute Details
#config_dir ⇒ String (readonly)
Absolute path to the main directory that contains all config files (and subdirectories).
17 18 19 |
# File 'lib/config_file_manager.rb', line 17 def config_dir @config_dir end |
#env ⇒ String (readonly)
Current environment name. Used to load the correct section of YAML files.
27 28 29 |
# File 'lib/config_file_manager.rb', line 27 def env @env end |
#example_extension ⇒ String (readonly)
Extension of the example/dummy version of a config file. eg. ‘.example`, `.dummy`
33 34 35 |
# File 'lib/config_file_manager.rb', line 33 def example_extension @example_extension end |
#max_dir_depth ⇒ Integer (readonly)
Maximum depth of nested directories containing config files.
22 23 24 |
# File 'lib/config_file_manager.rb', line 22 def max_dir_depth @max_dir_depth end |
Instance Method Details
#create_missing_dirs(example_extension: @example_extension, print: false) ⇒ void
This method returns an undefined value.
Create the missing config directories based on their dummy/example versions.
125 126 127 128 129 130 |
# File 'lib/config_file_manager.rb', line 125 def create_missing_dirs(example_extension: @example_extension, print: false) puts COLORS.blue('== Copying missing config directories ==') if print dirs(example_extension: example_extension).each do |dir| create_missing_dir("#{dir}#{example_extension}", file, print: print) end end |
#create_missing_files(example_extension: @example_extension, print: false) ⇒ void
This method returns an undefined value.
Create the missing config files based on their dummy/example versions.
91 92 93 94 95 96 |
# File 'lib/config_file_manager.rb', line 91 def create_missing_files(example_extension: @example_extension, print: false) puts COLORS.blue('== Copying missing config files ==') if print files(example_extension: example_extension).each do |file| create_missing_file("#{file}#{example_extension}", file, print: print) end end |
#delete_file(*file_name) ⇒ Object
183 184 185 |
# File 'lib/config_file_manager.rb', line 183 def delete_file(*file_name) ::File.delete(file_path(*file_name)) end |
#dir_exist?(*dir_name) ⇒ Boolean
208 209 210 |
# File 'lib/config_file_manager.rb', line 208 def dir_exist?(*dir_name) ::Dir.exist? file_path(*dir_name) end |
#dirs(example_extension: @example_extension) ⇒ Array<String>
Search for directories under the ‘config_dir` directory with the specified ending (eg. `.example`). Returns an array of absolute paths to the found files with the specified ending stripped away.
105 106 107 108 109 110 |
# File 'lib/config_file_manager.rb', line 105 def dirs(example_extension: @example_extension) ::Dir.each_child(@config_dir) .map { ::File.join(@config_dir, _1) } .select { ::File.directory?(_1) && _1.end_with?(example_extension) } .map { _1.delete_suffix(example_extension) } end |
#file_exist?(*file_name) ⇒ Boolean
202 203 204 |
# File 'lib/config_file_manager.rb', line 202 def file_exist?(*file_name) ::File.exist? file_path(*file_name) end |
#file_path(*file_name) ⇒ String
214 215 216 217 |
# File 'lib/config_file_manager.rb', line 214 def file_path(*file_name) *path, name = file_name ::File.join(@config_dir, *path, name) end |
#files(example_extension: @example_extension, result: [], depth: 0, dir_path: @config_dir) ⇒ Array<String>
Recursively search for files under the ‘config_dir` directory with the specified extension (eg. `.example`). Returns an array of absolute paths to the found files with the specified extension stripped away.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/config_file_manager.rb', line 53 def files(example_extension: @example_extension, result: [], depth: 0, dir_path: @config_dir) return result if depth > @max_dir_depth ::Dir.each_child(dir_path) do |path| abs_path = ::File.join(dir_path, path) if ::File.directory?(abs_path) # if the entry is a directory, scan it recursively # this essentially performs a depth limited search (DFS with a depth limit) next files( example_extension: example_extension, result: result, depth: depth + 1, dir_path: abs_path ) end next unless ::File.file?(abs_path) && path.end_with?(example_extension) result << abs_path.delete_suffix(example_extension) end result end |
#load_erb(*file_name) ⇒ String
189 190 191 |
# File 'lib/config_file_manager.rb', line 189 def load_erb(*file_name) ::ERB.new(load_file(*file_name)).result end |
#load_file(*file_name) ⇒ String
196 197 198 |
# File 'lib/config_file_manager.rb', line 196 def load_file(*file_name) ::File.read file_path(*file_name) end |
#load_yaml(*file_name, env: @env, symbolize: true) ⇒ Hash, Array
174 175 176 177 178 179 180 |
# File 'lib/config_file_manager.rb', line 174 def load_yaml(*file_name, env: @env, symbolize: true) env = env.to_sym if env && symbolize parsed = ruby_load_yaml(load_erb(*file_name), symbolize_names: symbolize) return parsed unless env parsed[env] end |
#missing_dirs(example_extension: @example_extension) ⇒ Array<String>
Returns Absolute paths to missing config directories.
114 115 116 117 118 |
# File 'lib/config_file_manager.rb', line 114 def missing_dirs(example_extension: @example_extension) dirs(example_extension: example_extension).reject do |file| ::Dir.exist?(file) end end |
#missing_files(example_extension: @example_extension) ⇒ Array<String>
Returns Absolute paths to missing config files.
80 81 82 83 84 |
# File 'lib/config_file_manager.rb', line 80 def missing_files(example_extension: @example_extension) files(example_extension: example_extension).reject do |file| ::File.exist?(file) end end |
#to_absolute_path(relative_path) ⇒ String
Converts a relative path to an absolute path.
166 167 168 |
# File 'lib/config_file_manager.rb', line 166 def to_absolute_path(relative_path) "#{@config_dir}/#{relative_path}" end |
#to_absolute_paths(relative_paths) ⇒ Array<String>
Converts a collection of relative paths to an array of absolute paths.
156 157 158 159 160 |
# File 'lib/config_file_manager.rb', line 156 def to_absolute_paths(relative_paths) relative_paths.map do |path| to_absolute_path(path) end end |
#to_relative_path(absolute_path) ⇒ String
Converts an absolute path to a relative path
147 148 149 |
# File 'lib/config_file_manager.rb', line 147 def to_relative_path(absolute_path) absolute_path.delete_prefix("#{@config_dir}/") end |
#to_relative_paths(absolute_paths) ⇒ Array<String>
Converts a collection of absolute paths to an array of relative paths.
137 138 139 140 141 |
# File 'lib/config_file_manager.rb', line 137 def to_relative_paths(absolute_paths) absolute_paths.map do |path| to_relative_path(path) end end |