Class: OptParseValidator::ConfigFilesLoaderMerger

Inherits:
Array
  • Object
show all
Defined in:
lib/opt_parse_validator/config_files_loader_merger.rb,
lib/opt_parse_validator/config_files_loader_merger/yml.rb,
lib/opt_parse_validator/config_files_loader_merger/base.rb,
lib/opt_parse_validator/config_files_loader_merger/json.rb

Overview

Options Files container

Defined Under Namespace

Modules: ConfigFile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.supported_extensionsArray<String>

Returns The downcased supported extensions list.

Returns:

  • (Array<String>)

    The downcased supported extensions list



11
12
13
14
15
16
17
18
# File 'lib/opt_parse_validator/config_files_loader_merger.rb', line 11

def self.supported_extensions
  extensions = ConfigFile.constants.select do |const|
    name = ConfigFile.const_get(const)
    name.is_a?(Class) && name != ConfigFile::Base
  end

  extensions.map { |sym| sym.to_s.downcase }
end

Instance Method Details

#<<(file_path) ⇒ Self

Parameters:

  • file_path (String)

Returns:

  • (Self)

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/opt_parse_validator/config_files_loader_merger.rb', line 23

def <<(file_path)
  return self unless File.exist?(file_path)

  ext = File.extname(file_path).delete('.')

  raise Error, "The option file's extension '#{ext}' is not supported" unless self.class.supported_extensions.include?(ext)

  super(ConfigFile.const_get(ext.upcase).new(file_path))
end

#parse(opts = {}) ⇒ Hash

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

Returns:

  • (Hash)


38
39
40
41
42
43
44
# File 'lib/opt_parse_validator/config_files_loader_merger.rb', line 38

def parse(opts = {})
  result = {}

  each { |option_file| result.deep_merge!(option_file.parse(opts)) }

  opts[:symbolize_keys] ? result.deep_symbolize_keys : result
end