Class: ConfigFileManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(config_dir, example_extension: '.example', max_dir_depth: 5, env: 'development') ⇒ ConfigFileManager

Returns a new instance of ConfigFileManager.

Parameters:

  • config_dir (String)

    Absolute path to the root config directory

  • example_extension (String) (defaults to: '.example')
  • max_dir_depth (Integer) (defaults to: 5)

    Maximum depth of nested directories containing config files.

  • env (String) (defaults to: 'development')

    Current environment name



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_dirString (readonly)

Absolute path to the main directory that contains all config files (and subdirectories).

Returns:

  • (String)


18
19
20
# File 'lib/config_file_manager.rb', line 18

def config_dir
  @config_dir
end

#envString (readonly)

Current environment name. Used to load the correct section of YAML files.

Returns:

  • (String)


28
29
30
# File 'lib/config_file_manager.rb', line 28

def env
  @env
end

#example_extensionString (readonly)

Extension of the example/dummy version of a config file. eg. ‘.example`, `.dummy`

Returns:

  • (String)


34
35
36
# File 'lib/config_file_manager.rb', line 34

def example_extension
  @example_extension
end

#max_dir_depthInteger (readonly)

Maximum depth of nested directories containing config files.

Returns:

  • (Integer)


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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)
  • print (Boolean) (defaults to: false)


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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)
  • print (Boolean) (defaults to: false)


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

Parameters:

  • dir_name (Array<String>)


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

Parameters:

  • file_name (Array<String>)


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

Parameters:

  • dir_name (Array<String>)

Returns:

  • (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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)

    ending of example directories

Returns:

  • (Array<String>)


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

Parameters:

  • file_name (Array<String>)

Returns:

  • (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

Parameters:

  • file_name (Array<String>)

Returns:

  • (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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)

    File extension of example files

Returns:

  • (Array<String>)


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

Parameters:

  • file_name (Array<String>)

Returns:

  • (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

Parameters:

  • file_name (Array<String>)

Returns:

  • (String)

Raises:

  • (SystemCallError)


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

Parameters:

  • file_name (Array<String>)
  • env (String, nil) (defaults to: @env)
  • symbolize (Boolean) (defaults to: true)

    Whether the keys should be converted to Ruby symbols

Returns:

  • (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

Parameters:

  • file_name (Array<String>)
  • env (String, nil) (defaults to: @env)
  • symbolize (Boolean) (defaults to: true)

    Whether the keys should be converted to Ruby symbols

Returns:

  • (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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)

Returns:

  • (Array<String>)

    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.

Parameters:

  • example_extension (String) (defaults to: @example_extension)

Returns:

  • (Array<String>)

    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.

Parameters:

  • relative_path (String)

Returns:

  • (String)


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.

Parameters:

  • relative_paths (Array<String>)

Returns:

  • (Array<String>)


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

Parameters:

  • absolute_path (String)

Returns:

  • (String)


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.

Parameters:

  • absolute_paths (Array<String>)

Returns:

  • (Array<String>)


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