Class: RubyUtils::Parameter::BaseParameterHandler
- Inherits:
-
Object
- Object
- RubyUtils::Parameter::BaseParameterHandler
- Defined in:
- lib/ruby_utils/parameter/base_parameter_handler.rb
Overview
Abstract class to seperate the storage of the parameter in a repository entity and checking for valid parameter combination as part of the application logic. Can raise an ArgumentError or IndexError when invalid parameter arguments or parameter combinations are provided Will raise a NotImplementedError if the abstract methods are called without an implementation in a child class
Instance Attribute Summary collapse
-
#repository ⇒ ParameterRepository
readonly
parameter provided as arguments in script call.
Instance Method Summary collapse
-
#base_validate_parameters ⇒ Object
private
private method with calls of the different validations methods.
-
#check_constraint(invalid, symbol) ⇒ Object
private
creates a constraint error if an invalid parameter combination occurs, with the exception when the help parameter is used.
-
#check_for_valid_filepath ⇒ Object
private
checks if the parsed filename is a valid unix or windows file name caution: this validation only checks for a valid syntax, it DOES NOT mitigate path traversal attacks or other attacks to move within the file system, security relevant directory should follow in the child class using the #validate_parameters method.
-
#check_mandatory_parameter(symbol) ⇒ Object
private
method to check mandatory parameters, throws an ArgumentError if the parameter has no been set.
-
#check_number_of_parameters(key, count_parameters) ⇒ Object
private
checks the correct number of parameters for the given key.
-
#check_occurrence(symbol, required) ⇒ Object
private
checks if the second parameter is required when the first symbol occurs, with the exception when the help parameter is used.
-
#check_parameter_constraints ⇒ Object
private
abstract private method to the specified parameter constraints method.
-
#initialize(argv) ⇒ BaseParameterHandler
constructor
initialization.
-
#initialize_repository(argv) ⇒ Object
private
abstract private method to initialize the correct repository that should be used in this handler method.
-
#validate_parameters ⇒ Object
private
abstract private method with calls of the different validations methods method.
Constructor Details
#initialize(argv) ⇒ BaseParameterHandler
initialization
21 22 23 24 25 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 21 def initialize(argv) initialize_repository(argv) base_validate_parameters check_parameter_constraints end |
Instance Attribute Details
#repository ⇒ ParameterRepository (readonly)
parameter provided as arguments in script call
17 18 19 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 17 def repository @repository end |
Instance Method Details
#base_validate_parameters ⇒ Object (private)
private method with calls of the different validations methods
56 57 58 59 60 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 56 def base_validate_parameters check_for_valid_filepath if (@repository.parameters[:file]) validate_parameters nil end |
#check_constraint(invalid, symbol) ⇒ Object (private)
creates a constraint error if an invalid parameter combination occurs, with the exception when the help parameter is used
131 132 133 134 135 136 137 138 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 131 def check_constraint(invalid, symbol) if (@repository.parameters[symbol] && @repository.parameters[invalid] && !@repository.parameters[:help]) raise ArgumentError, " Error: invalid parameter combination: " \ " #{@repository.mapping[symbol]} and #{@repository.mapping[invalid]}".red end nil end |
#check_for_valid_filepath ⇒ Object (private)
checks if the parsed filename is a valid unix or windows file name
caution: this validation only checks for a valid syntax, it DOES NOT mitigate path traversal attacks or other attacks to move within the file system, security relevant directory should follow in the child class using the #validate_parameters method
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 68 def check_for_valid_filepath filepath = @repository.parameters[:file] unixfile_regex= %r{ \A # start of string ((\.\/)|(\.\.\/)+|(\/))? # relativ path or upwards or absolute ((\.\/)|(\.\.\/)+|[\-\w\s]+\/)* # 0-n subsirectories and relativ movement [\-\w\s]*[a-zA-Z0-9] # filename (\.[a-zA-Z0-9]+)* # point separated filename or 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]+)* # point separated filename or extension \z # end of string }x if (!(filepath =~ unixfile_regex || filepath =~ windowsfile_regex)) raise ArgumentError, " Error: invalid filepath: #{filepath}".red end nil end |
#check_mandatory_parameter(symbol) ⇒ Object (private)
method to check mandatory parameters, throws an ArgumentError if the parameter has no been set
144 145 146 147 148 149 150 151 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 144 def check_mandatory_parameter(symbol) if (@repository.parameters[symbol] == nil && !@repository.parameters[:help] && !@repository.parameters[:version]) raise ArgumentError, "Error: #{@repository.mapping[symbol]} is mandatory, but missing" end nil end |
#check_number_of_parameters(key, count_parameters) ⇒ Object (private)
checks the correct number of parameters for the given key
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 99 def check_number_of_parameters(key, count_parameters) if (@repository.parameters[key] && !@repository.parameters[:help]) value = @repository.parameters[key] if (value.size != count_parameters) raise IndexError, " Error: invalid number of parameters for option: #{key};" \ " expected #{count_parameters} got #{value.size}.".red end end nil end |
#check_occurrence(symbol, required) ⇒ Object (private)
checks if the second parameter is required when the first symbol occurs, with the exception when the help parameter is used
116 117 118 119 120 121 122 123 124 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 116 def check_occurrence(symbol, required) if (@repository.parameters[symbol] && !@repository.parameters[required] && !@repository.parameters[:help]) raise ArgumentError, " Error: #{@repository.mapping[symbol]} requires the parameters" \ " of #{@repository.mapping[required]}".red end nil end |
#check_parameter_constraints ⇒ Object (private)
abstract private method to the specified parameter constraints method
50 51 52 53 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 50 def check_parameter_constraints fail NotImplementedError, " Error: the subclass #{self.class} needs " \ "to implement the method: check_parameter_constraints from its base class".red end |
#initialize_repository(argv) ⇒ Object (private)
abstract private method to initialize the correct repository that should be used in this handler method
34 35 36 37 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 34 def initialize_repository(argv) fail NotImplementedError, " Error: the subclass #{self.class} needs " \ "to implement the method: initialize_repository from its base class".red end |
#validate_parameters ⇒ Object (private)
abstract private method with calls of the different validations methods method
42 43 44 45 |
# File 'lib/ruby_utils/parameter/base_parameter_handler.rb', line 42 def validate_parameters fail NotImplementedError, " Error: the subclass #{self.class} needs " \ "to implement the method: validate_parameters from its base class".red end |