Class: Parameter::ParameterHandler

Inherits:
RubyUtils::Parameter::BaseParameterHandler
  • Object
show all
Defined in:
lib/parameter/parameter_handler.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#check_constraint(v, i, symbol) ⇒ Object (private)

creates a constraint error if an invalid parameter combination occurs

Parameters:

  • v (String)

    the first parameter

  • i (String)

    the second parameter

  • symbol (Symbol)

    the literal to check

Raises:

  • (ArgumentError)

    for an invalid parameter combination



94
95
96
97
98
99
# File 'lib/parameter/parameter_handler.rb', line 94

def check_constraint(v, i, symbol)
  if (@repository.parameters[symbol])
    raise ArgumentError,
          " Error: invalid parameter combination: #{v} and #{i}"
  end
end

#check_constraints_for_aObject (private)

checks constraints:

!(-a + -i), !(-i + -a),
!(-a + -t), !(-t + -a),
!(-a + -d), !(-d + -a)

Raises:

  • (ArgumentError)

    if invalid parameter combination occurs



57
58
59
60
61
# File 'lib/parameter/parameter_handler.rb', line 57

def check_constraints_for_a
  check_constraint('-a', '-i', :index)
  check_constraint('-a', '-d', :delta)
  check_constraint('-a', '-t', :time)
end

#check_constraints_for_cObject (private)

checks constraints:

!(-c + -e), !(-e + -c),
!(-c + -t), !(-t + -c)

Raises:

  • (ArgumentError)

    if invalid parameter combination occurs



67
68
69
70
# File 'lib/parameter/parameter_handler.rb', line 67

def check_constraints_for_c
  check_constraint('-c', '-e', :extreme) if (!@repository.parameters[:section])
  check_constraint('-c', '-t', :time)
end

#check_constraints_for_dObject (private)

checks contraints:

!(-d + -i), !(-i + -d),
!(-d + -t), !(-t + -d)

Raises:

  • (ArgumentError)

    if invalid parameter combination occurs



76
77
78
79
# File 'lib/parameter/parameter_handler.rb', line 76

def check_constraints_for_d
  check_constraint('-d', '-i', :index)
  check_constraint('-d', '-t', :time)
end

#check_constraints_for_rObject (private)

checks constraints:

!(-r + -t), !(-t + -r),
!(-r + -i), !(-i + -r)


84
85
86
87
# File 'lib/parameter/parameter_handler.rb', line 84

def check_constraints_for_r
  check_constraint('-r', '-t', :time)
  check_constraint('-r', '-i', :index)
end

#check_parameter_constraintsObject (private)

private method to the specified parameter constraints



35
36
37
38
39
40
41
42
43
# File 'lib/parameter/parameter_handler.rb', line 35

def check_parameter_constraints
  check_constraints_for_a if (@repository.parameters[:all])
  check_constraints_for_c if (@repository.parameters[:coord])
  check_constraints_for_d if (@repository.parameters[:delta])
  check_constraints_for_r if (@repository.parameters[:range])

  # check mandatory file parameter
  check_mandatory_parameter(:file)
end

#check_parameter_occurrenceObject (private)

private method to check the occurrence of two parameters



46
47
48
49
50
# File 'lib/parameter/parameter_handler.rb', line 46

def check_parameter_occurrence
  if (@repository.parameters[:section] && !@repository.parameters[:help])
    check_occurrence('-s', '-c', :coord)
  end
end

#initialize_repository(argv) ⇒ Object

method to initialize the correct repository that should be used in this handler

Parameters:

  • argv (Array)

    array of input parameters



17
18
19
# File 'lib/parameter/parameter_handler.rb', line 17

def initialize_repository(argv)
  @repository = ParameterRepository.new(argv)
end

#validate_parametersObject (private)

private method with calls of the different validations methods



24
25
26
27
28
29
30
31
32
# File 'lib/parameter/parameter_handler.rb', line 24

def validate_parameters
  check_for_valid_filepath if (@repository.parameters[:file])

  check_number_of_parameters(:coord, 2)
  check_number_of_parameters(:delta, 2)
  check_number_of_parameters(:time, 2)
  check_number_of_parameters(:range, 2)
  check_number_of_parameters(:section, 2)
end