Class: ConfigurationHandler

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

Overview

handler class to serve as a component between the configuation repository and other components of the application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurationHandler

initialization



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

def initialize
  initialize_option_mapping
  @options = ConfigurationRepository.new()
end

Instance Attribute Details

#option_mappingHash (readonly, private)

Returns mapping of (String => Symbol) for reading options from a file.

Returns:

  • (Hash)

    mapping of (String => Symbol) for reading options from a file



52
53
54
# File 'lib/configuration/configuration_handler.rb', line 52

def option_mapping
  @option_mapping
end

#optionsConfigurationRepository (readonly)

Returns repository with the configuration parameters.

Returns:



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

def options
  @options
end

Instance Method Details

#check_and_read_options(filename) ⇒ Object (private)

method to check the given filename and read the options when it is a valid file

Parameters:

  • filename (String)

    the provided filename



65
66
67
68
69
# File 'lib/configuration/configuration_handler.rb', line 65

def check_and_read_options(filename)
  check_for_valid_filepath(filename)

  read_options(filename)
end

#check_for_valid_filepath(filepath) ⇒ Object (private)

checks if the parsed filename is a valid unix or windows file name

Parameters:

  • filepath (String)

    the provided filepath

Raises:

  • (ArgumentError)

    if filepath is not valid



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/configuration/configuration_handler.rb', line 74

def check_for_valid_filepath(filepath)
  unixfile_regex= %r{
    \A                       # start of string
    ((\.\/)|(\.\.\/)+|(\/))? # relativ path or upwards or absolute
    ([\-\w\s]+\/)*           # 0-n subsirectories
    [\-\w\s]*[a-zA-Z0-9]     # filename
    (\.[a-zA-Z0-9]+)?        # extension
    \z                       # end of string
  }x

  windowsfile_regex = %r{
    \A                      # start of string
    ([A-Z]:)?\\?            # device name
    ([\-\w\s]+\\)*          # directories
    [\-\w\s]*[a-zA-Z0-9]    # filename
    (\.[a-zA-Z0-9]+)?       # extension
    \z                      # end of string
  }x

  if (!(filepath =~ unixfile_regex || filepath =~ windowsfile_regex))
    raise ArgumentError,
          " Error [ConfigurationHandler]: invalid filepath: #{filepath}".red
  end
end

#determine_value(type, value) ⇒ Object (private)

method to determine the type of the value and return an object with the class of the intended value

Parameters:

  • type (Class)

    the required class

  • value (String)

    to string containing the required value

Returns:

  • (Object)

    the value cast in the intended class



115
116
117
118
119
120
121
# File 'lib/configuration/configuration_handler.rb', line 115

def determine_value(type, value)
  return false if (type == FalseClass)
  return true  if (type == TrueClass)
  return value.to_i if (type == Fixnum)
  return value.to_f if (type == Float)
  value
end

#initialize_option_mappingObject (private)

method to initialize the Hash with the mapping (String => Symbol)



55
56
57
58
59
60
# File 'lib/configuration/configuration_handler.rb', line 55

def initialize_option_mapping
  @option_mapping = Hash.new()
  @option_mapping['legend_extend']= :legend_extend
  @option_mapping['y_time_size']= :y_time_size
  @option_mapping['auto_scale'] = :auto_scale
end

#process_parameter(option) ⇒ Object

method to process the options provided by the script parameters

Parameters:

  • option (String)

    the given option the creation of configuation options



26
27
28
29
30
31
32
33
34
35
# File 'lib/configuration/configuration_handler.rb', line 26

def process_parameter(option)
  if (option =='menu')
    ConfigurationMenu.new.print_menu
  elsif (option.start_with?('file='))
    check_and_read_options(option.split('=')[1])
  elsif (!option.eql?('default'))
    raise ArgumentError,
          "Error [ConfigurationHandler]: Option parameter is not valid.".red
  end
end

#read_options(filename) ⇒ Object (private)

method to read the configuration options from a file

Parameters:

  • filename (String)

    the path of the file



101
102
103
104
105
106
107
108
# File 'lib/configuration/configuration_handler.rb', line 101

def read_options(filename)
  settings = Hash.new()
  DataInput::FileReader.new(filename, ';').data.each { |line|
    type = Object.const_get(line[1])
    settings[@option_mapping[line[0]]] = determine_value(type, line[2])
  }
  @options = ConfigurationRepository.new(settings)
end

#save_options(filename) ⇒ Object

method to save the current configuration options

Parameters:

  • filename (String)

    the provided filename to save the options



39
40
41
42
43
44
45
46
47
# File 'lib/configuration/configuration_handler.rb', line 39

def save_options(filename)
  output = File.new(filename, 'w')

  @options.repository.each_pair { |key, value|
    output.puts "#{@option_mapping.key(key)};#{value.class};#{value}"
  }

  output.close
end