Class: RubyUtils::Parameter::BaseParameterRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_utils/parameter/base_parameter_repository.rb

Overview

Abstract parameter repository to store the valid parameters of the script. #initialize gets the provided parameters and fills a hash which grants access to the provided parameters and arguments Will raise an NotImplementedError if the abstract methods are called without an implementation in a child class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ BaseParameterRepository

initialization

Parameters:

  • argv (Array)

    Array of input parameters

Raises:

  • (ArgumentError)

    for an invalid or empty combination of script parameters



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 23

def initialize(argv)
  @parameters = Hash.new()
  define_base_mapping
  @unflagged_arguments = Array.new()
  raise ArgumentError, " Error: argv is empty".red if (argv.empty?)
  argv.each { |arg|
    process_base_argument(arg)
  }
  if (@unflagged_arguments.size > 0)
    raise ArgumentError, " Error: invalid combination of parameters.".red
  end
end

Instance Attribute Details

#mappingHash (readonly)

Returns Hash of parameter symbols and string representation.

Returns:

  • (Hash)

    Hash of parameter symbols and string representation



18
19
20
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 18

def mapping
  @mapping
end

#parametersHash (readonly)

Returns Hash of valid parameters and their values.

Returns:

  • (Hash)

    Hash of valid parameters and their values



16
17
18
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 16

def parameters
  @parameters
end

#unflagged_argumentsObject (readonly, private)

Returns the value of attribute unflagged_arguments.



38
39
40
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 38

def unflagged_arguments
  @unflagged_arguments
end

Instance Method Details

#check_and_set_argument(arg_key, arg) ⇒ Object (private)

check if a parameter holds one or more arguments and adds the argument depending on the check

Parameters:

  • arg_key (Symbol)

    the symbol referencing a parameter

  • arg (String)

    the argument from the input array

Raises:

  • (ArgumentError)

    if the parameter cannot be set up with the arguments



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 98

def check_and_set_argument(arg_key, arg)
  if (arg_key != nil)
    if(@parameters[arg_key] != nil)
      @parameters[arg_key] << arg
    else
      @parameters[arg_key] = arg
    end
  elsif (parameters[:help] == nil && @parameters[:version] == nil)
    raise ArgumentError, " Error: invalid arguments for #{arg_key}, got #{arg}.".red
  end
end

#check_and_set_helpvalueObject (private)

checks if the help parameter was entered with a parameter of if the general help information is requested



112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 112

def check_and_set_helpvalue
  if(@parameters.keys.last != nil)
    # help in context to a parameter
    @parameters[:help] = @parameters.keys.last
    @unflagged_arguments.shift
  else
    # help without parameter => global help
    @parameters[:help] = true
  end
end

#create_argument_entry(symbol) ⇒ Object (private)

creates a new entry for a parameter with one argument

Parameters:

  • symbol (Symbol)

    the symbol of the argument



81
82
83
84
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 81

def create_argument_entry(symbol)
  @parameters[symbol] = nil
  @unflagged_arguments.unshift(symbol)
end

#create_two_argument_entry(symbol) ⇒ Object (private)

creates a new entry for a parameter with two arguments

Parameters:

  • symbol (Symbol)

    the symbol of the argument



88
89
90
91
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 88

def create_two_argument_entry(symbol)
  @parameters[symbol] = Array.new()
  2.times{ @unflagged_arguments.unshift(symbol) }
end

#define_base_mappingObject (private)

method to define the input string values that will match a given paramter symbol



56
57
58
59
60
61
62
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 56

def define_base_mapping
  @mapping = Hash.new()
  @mapping[:file] = ["-f", "--file"]
  @mapping[:help] = ["-h", "--help"]
  @mapping[:version] = ["-v", "--version"]
  define_mapping
end

#define_mappingObject (private)

method to define the input string values that will match a given paramter symbol



74
75
76
77
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 74

def define_mapping
  fail NotImplementedError, " Error: the subclass #{self.class} needs " \
       "to implement the method: define_mapping from its base class".red
end

#process_argument(arg) ⇒ Object (private)

abstract method to read further argument and process it depending on its content method

Parameters:

  • arg (String)

    the given argument

Raises:

  • (NotImplementedError)

    if the child class does not implement this



68
69
70
71
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 68

def process_argument(arg)
  fail NotImplementedError, " Error: the subclass #{self.class} needs " \
       "to implement the method: process_argument from its base class".red
end

#process_base_argument(arg) ⇒ boolean (private)

method to read the given argument and process it depending on its content

Parameters:

  • arg (String)

    the given argument

Returns:

  • (boolean)

    if the size of the argument array is zero or not



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 43

def process_base_argument(arg)
  case arg
    when *@mapping[:help]    then check_and_set_helpvalue
    when *@mapping[:version] then @parameters[:version] = true
    when *@mapping[:file]    then create_argument_entry(:file)          
    when /-[a-z]|--[a-z]+/ then process_argument(arg)
  else
    check_and_set_argument(@unflagged_arguments.shift, arg)
  end
  nil
end

#raise_invalid_parameter(arg) ⇒ Object (private)

error message in the case of an invalid argument

Parameters:

  • arg (String)

    invalid parameter string

Raises:

  • (ArgumentError)

    if an invalid argument is provided



126
127
128
# File 'lib/ruby_utils/parameter/base_parameter_repository.rb', line 126

def raise_invalid_parameter(arg)
  raise ArgumentError, " Error: invalid or unknown argument: #{arg}".red
end