Class: RubyUtils::Configuration::BaseConfigurationRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_utils/configuration/base_configuration_repository.rb

Overview

Abstract configuration repository to store the valid configuration parameter. Will raise an NotImplementedError if the abstract methods are called without an implementation in a child class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ BaseConfigurationRepository

initialization

Parameters:

  • config_path (Pathname)

    the filepath the the json configuration file



18
19
20
21
# File 'lib/ruby_utils/configuration/base_configuration_repository.rb', line 18

def initialize(config_path)
  @config_hash = JSON.load(File.read(config_path))
  check_config_keys
end

Instance Attribute Details

#config_hashHash (readonly)

Returns the hash with the hashes of the given json file.

Returns:

  • (Hash)

    the hash with the hashes of the given json file



14
15
16
# File 'lib/ruby_utils/configuration/base_configuration_repository.rb', line 14

def config_hash
  @config_hash
end

Instance Method Details

#check_config_keysObject (private)

method to check if the required keys are available in the json hash and check for optional ones method

Raises:

  • (NotImplementedError)

    if the child class does not implement this



29
30
31
32
# File 'lib/ruby_utils/configuration/base_configuration_repository.rb', line 29

def check_config_keys
  fail NotImplementedError, " Error: the subclass #{self.class} needs " \
       "to implement the method: process_argument from its base class".red
end

#required_config_key_available?(key) ⇒ Boolean (private)

method to check if the required is present in the hash

Parameters:

  • key (String)

    the required key

Returns:

  • (Boolean)

Raises:

  • (KeyError)

    if the given key is not in the configuration hash



37
38
39
40
41
42
# File 'lib/ruby_utils/configuration/base_configuration_repository.rb', line 37

def required_config_key_available?(key)
  if (@config_hash[key] == nil)
    raise KeyError, "Required parameter #{key} is not present in configuration file."
  end
  nil
end