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.3'
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_dir(*dir_name) ⇒ Object
- #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_json(*file_name, env: @env, symbolize: true) ⇒ Hash, Array
- #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.
40 41 42 43 44 45 |
# File 'lib/config_file_manager.rb', line 40 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).
18 19 20 |
# File 'lib/config_file_manager.rb', line 18 def config_dir @config_dir end |
#env ⇒ String (readonly)
Current environment name. Used to load the correct section of YAML files.
28 29 30 |
# File 'lib/config_file_manager.rb', line 28 def env @env end |
#example_extension ⇒ String (readonly)
Extension of the example/dummy version of a config file. eg. ‘.example`, `.dummy`
34 35 36 |
# File 'lib/config_file_manager.rb', line 34 def example_extension @example_extension end |
#max_dir_depth ⇒ Integer (readonly)
Maximum depth of nested directories containing config files.
23 24 25 |
# File 'lib/config_file_manager.rb', line 23 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.
126 127 128 129 130 131 |
# File 'lib/config_file_manager.rb', line 126 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}", dir, 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.
92 93 94 95 96 97 |
# File 'lib/config_file_manager.rb', line 92 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_dir(*dir_name) ⇒ Object
201 202 203 |
# File 'lib/config_file_manager.rb', line 201 def delete_dir(*dir_name) ::FileUtils.rm_r(file_path(*dir_name)) end |
#delete_file(*file_name) ⇒ Object
196 197 198 |
# File 'lib/config_file_manager.rb', line 196 def delete_file(*file_name) ::File.delete(file_path(*file_name)) end |
#dir_exist?(*dir_name) ⇒ Boolean
226 227 228 |
# File 'lib/config_file_manager.rb', line 226 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.
106 107 108 109 110 111 |
# File 'lib/config_file_manager.rb', line 106 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
220 221 222 |
# File 'lib/config_file_manager.rb', line 220 def file_exist?(*file_name) ::File.exist? file_path(*file_name) end |
#file_path(*file_name) ⇒ String
232 233 234 235 |
# File 'lib/config_file_manager.rb', line 232 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/config_file_manager.rb', line 54 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
207 208 209 |
# File 'lib/config_file_manager.rb', line 207 def load_erb(*file_name) ::ERB.new(load_file(*file_name)).result end |
#load_file(*file_name) ⇒ String
214 215 216 |
# File 'lib/config_file_manager.rb', line 214 def load_file(*file_name) ::File.read file_path(*file_name) end |
#load_json(*file_name, env: @env, symbolize: true) ⇒ Hash, Array
187 188 189 190 191 192 193 |
# File 'lib/config_file_manager.rb', line 187 def load_json(*file_name, env: @env, symbolize: true) env = env.to_sym if env && symbolize parsed = ::JSON.parse(load_erb(*file_name), symbolize_names: symbolize) return parsed unless env parsed[env] end |
#load_yaml(*file_name, env: @env, symbolize: true) ⇒ Hash, Array
175 176 177 178 179 180 181 |
# File 'lib/config_file_manager.rb', line 175 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.
115 116 117 118 119 |
# File 'lib/config_file_manager.rb', line 115 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.
81 82 83 84 85 |
# File 'lib/config_file_manager.rb', line 81 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.
167 168 169 |
# File 'lib/config_file_manager.rb', line 167 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.
157 158 159 160 161 |
# File 'lib/config_file_manager.rb', line 157 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
148 149 150 |
# File 'lib/config_file_manager.rb', line 148 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.
138 139 140 141 142 |
# File 'lib/config_file_manager.rb', line 138 def to_relative_paths(absolute_paths) absolute_paths.map do |path| to_relative_path(path) end end |