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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#result_keyObject

Returns the value of attribute result_key.



31
32
33
# File 'lib/opt_parse_validator/config_files_loader_merger.rb', line 31

def result_key
  @result_key
end

Class Method Details

.supported_extensionsArray<String>

Returns The downcased supported extensions list.

Returns:

  • (Array<String>)

    The downcased supported extensions list



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

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:



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

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

#parseHash

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
57
# File 'lib/opt_parse_validator/config_files_loader_merger.rb', line 49

def parse
  result = {}

  each { |config_file| result.deep_merge!(config_file.parse) }

  result = result[result_key] || {} if result_key

  result.deep_symbolize_keys
end