Class: ConfigurationRepository

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

Overview

Repository storing available configuration parameters. If no parameters are set from the user, the default values for the parameters are used.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ ConfigurationRepository

initialization

Parameters:

  • options (Hash) (defaults to: nil)

    a hash with new values for the parameters



14
15
16
17
# File 'lib/configuration/configuration_repository.rb', line 14

def initialize(options=nil)
  default_options
  process_options(options) if (options != nil)
end

Instance Attribute Details

#repositoryHash (readonly)

Returns Hash of parameters and their values.

Returns:

  • (Hash)

    Hash of parameters and their values



10
11
12
# File 'lib/configuration/configuration_repository.rb', line 10

def repository
  @repository
end

Instance Method Details

#change_option(symbol, value) ⇒ Object

method to change the value of a specific symbol to a new value

Parameters:

  • symbol (Symbol)

    the provided symbol

  • value (Object)

    the provided value for the symbol



22
23
24
25
26
27
# File 'lib/configuration/configuration_repository.rb', line 22

def change_option(symbol, value)
  check_symbol_existance(symbol)
  check_value_class(symbol, value)

  @repository[symbol] = value
end

#check_boolean(value) ⇒ Object (private)

method to check the special case for boolean parameters

Parameters:

  • value (Object)

    the provided value



72
73
74
75
76
# File 'lib/configuration/configuration_repository.rb', line 72

def check_boolean(value)
  if (!(value.class == TrueClass || value.class == FalseClass))
    raise_type_error
  end
end

#check_symbol_existance(symbol) ⇒ Object (private)

method to check if a given symbol exists in the options hash

Parameters:

  • symbol (Symbol)

    the provided symbol

Raises:

  • (ArgumentError)

    if the symbol does not occur in the options hash



50
51
52
53
54
55
# File 'lib/configuration/configuration_repository.rb', line 50

def check_symbol_existance(symbol)
  if (@repository[symbol] == nil)
    raise ArgumentError,
          ' Error [Configuration]: the provided option does not exist.'.red
  end
end

#check_value_class(symbol, value) ⇒ Object (private)

method to check if the provided value class is the same as the value class set as the current value

Parameters:

  • symbol (Symbol)

    the provided symbol

  • value (Object)

    the provided value for the symbol



61
62
63
64
65
66
67
68
# File 'lib/configuration/configuration_repository.rb', line 61

def check_value_class(symbol, value)
  if (@repository[symbol].class == TrueClass ||
         @repository[symbol].class == FalseClass)
    check_boolean(value)
  elsif (@repository[symbol].class != value.class)
    raise_type_error
  end
end

#default_optionsObject (private)

method to initialize the attribute with the specified default values



32
33
34
35
36
37
# File 'lib/configuration/configuration_repository.rb', line 32

def default_options
  @repository = Hash.new()
  @repository[:legend_extend] = false # interval output for color legend
  @repository[:y_time_size] = 20      # number of intervals in y for timeline
  @repository[:auto_scale] = false    # scale visualization by terminal size
end

#process_options(options) ⇒ Object (private)

method to overwrite the default values with the provided values

Parameters:

  • options (Hash)

    a hash with the new parameter values



41
42
43
44
45
# File 'lib/configuration/configuration_repository.rb', line 41

def process_options(options)
  @repository.each_key { |key|
    @repository[key] = options[key] if (options[key])
  }
end

#raise_type_errorObject (private)

method to raise a Type error if the value check fails

Raises:

  • (TypeError)

    if the class of the now value does not fit the class of the old one



81
82
83
84
# File 'lib/configuration/configuration_repository.rb', line 81

def raise_type_error
    raise TypeError, ' Error [Configuration]: the type of a new value does '\
                     'not fit the original type.'.red
end