Module: RubyUtils::ParameterConverter

Defined in:
lib/ruby_utils/parameter_converter.rb

Overview

This module holds methods to convert a given string value to its corresponding Object format.

Class Method Summary collapse

Class Method Details

.convert_float_parameter(parameter) ⇒ Float

method to convert a string into a float parameter

Parameters:

  • parameter (String)

    the requested parameter

Returns:

  • (Float)

    the hash with the required values

Raises:

  • (TypeError)

    raises a type error when the parsing fails



26
27
28
29
30
31
32
# File 'lib/ruby_utils/parameter_converter.rb', line 26

def self.convert_float_parameter(parameter)
  begin 
    Float(parameter)
  rescue StandardError
    create_error_message(parameter, "float")
  end
end

.convert_int_parameter(parameter) ⇒ Integer

method to convert a string into a int parameter

Parameters:

  • parameter (String)

    the requested parameter

Returns:

  • (Integer)

    the hash with the required values

Raises:

  • (TypeError)

    raises a type error when the parsing fails



14
15
16
17
18
19
20
# File 'lib/ruby_utils/parameter_converter.rb', line 14

def self.convert_int_parameter(parameter)
  begin 
    Integer(parameter)
  rescue StandardError
    create_error_message(parameter, "integer")
  end
end

.convert_time_parameter(parameter) ⇒ Time

method to convert a string into a time parameter

Parameters:

  • parameter (String)

    the requested parameter

Returns:

  • (Time)

    the hash with the required values

Raises:

  • (TypeError)

    raises a type error when the parsing fails



38
39
40
41
42
43
44
# File 'lib/ruby_utils/parameter_converter.rb', line 38

def self.convert_time_parameter(parameter)
  begin 
    Time.parse(parameter)
  rescue StandardError
    create_error_message(parameter, "time")
  end
end

.create_error_message(parameter, type) ⇒ Object (private)

method to create an error message when rescuing an error

Parameters:

  • parameter (Object)

    the parameter that should be converted

  • type (String)

    a string containing the type for the error message

Raises:

  • (TypeError)

    raises a type error with a specific error message



50
51
52
53
54
55
56
57
# File 'lib/ruby_utils/parameter_converter.rb', line 50

private_class_method def self.create_error_message(parameter, type)
  if (parameter == nil)
    raise ArgumentError, "Error: Given parameter is nil".red
  else  
    raise TypeError, 
          "Error: the given argument #{parameter} is not a valid #{type}".red
  end
end