Class: ConfigurationHandler
- Inherits:
-
Object
- Object
- ConfigurationHandler
- 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
-
#option_mapping ⇒ Hash
readonly
private
Mapping of (String => Symbol) for reading options from a file.
-
#options ⇒ ConfigurationRepository
readonly
Repository with the configuration parameters.
Instance Method Summary collapse
-
#check_and_read_options(filename) ⇒ Object
private
method to check the given filename and read the options when it is a valid file.
-
#check_for_valid_filepath(filepath) ⇒ Object
private
checks if the parsed filename is a valid unix or windows file name.
-
#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.
-
#initialize ⇒ ConfigurationHandler
constructor
initialization.
-
#initialize_option_mapping ⇒ Object
private
method to initialize the Hash with the mapping (String => Symbol).
-
#process_parameter(option) ⇒ Object
method to process the options provided by the script parameters.
-
#read_options(filename) ⇒ Object
private
method to read the configuration options from a file.
-
#save_options(filename) ⇒ Object
method to save the current configuration options.
Constructor Details
#initialize ⇒ ConfigurationHandler
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_mapping ⇒ Hash (readonly, private)
Returns 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 |
#options ⇒ ConfigurationRepository (readonly)
Returns repository with the configuration parameters.
15 16 17 |
# File 'lib/configuration/configuration_handler.rb', line 15 def @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
65 66 67 68 69 |
# File 'lib/configuration/configuration_handler.rb', line 65 def (filename) check_for_valid_filepath(filename) (filename) end |
#check_for_valid_filepath(filepath) ⇒ Object (private)
checks if the parsed filename is a valid unix or windows file name
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
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_mapping ⇒ Object (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
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. elsif (option.start_with?('file=')) (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
101 102 103 104 105 106 107 108 |
# File 'lib/configuration/configuration_handler.rb', line 101 def (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
39 40 41 42 43 44 45 46 47 |
# File 'lib/configuration/configuration_handler.rb', line 39 def (filename) output = File.new(filename, 'w') @options.repository.each_pair { |key, value| output.puts "#{@option_mapping.key(key)};#{value.class};#{value}" } output.close end |