Sha256: b326b3a5b2bfccdb1913fb0053c3358f1d6eed91c487a876d82a53557e9058f2

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

require 'opt_parse_validator/options_file/base'
require 'opt_parse_validator/options_file/json'
require 'opt_parse_validator/options_file/yml'

module OptParseValidator
  # Options Files container
  class OptionsFiles < Array
    # @return [ Array<String> ] The downcased supported extensions list
    def supported_extensions
      extensions = OptionsFile.constants.select do |const|
        name = OptionsFile.const_get(const)
        name.is_a?(Class) && name != OptionsFile::Base
      end

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

    # @param [ String ] file_path
    #
    # @return [ Self ]
    def <<(file_path)
      return self unless File.exist?(file_path)

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

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

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

    # @return [ Hash ] a { key: value } hash
    def parse
      files_data = {}

      each { |option_file| files_data.merge!(option_file.parse) }

      # Convert string-full keys to symbol ones
      # No need to go deeper than the first level as it's the max depth
      Hash[files_data.map { |k, v| [k.to_sym, v] }]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
opt_parse_validator-0.0.13.11 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.10 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.9 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.8 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.7 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.6 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.5 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.4 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.3 lib/opt_parse_validator/options_files.rb
opt_parse_validator-0.0.13.2 lib/opt_parse_validator/options_files.rb