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
-
.convert_float_parameter(parameter) ⇒ Float
method to convert a string into a float parameter.
-
.convert_int_parameter(parameter) ⇒ Integer
method to convert a string into a int parameter.
-
.convert_time_parameter(parameter) ⇒ Time
method to convert a string into a time parameter.
-
.create_error_message(parameter, type) ⇒ Object
private
method to create an error message when rescuing an error.
Class Method Details
.convert_float_parameter(parameter) ⇒ Float
method to convert a string into a float parameter
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 (parameter, "float") end end |
.convert_int_parameter(parameter) ⇒ Integer
method to convert a string into a int parameter
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 (parameter, "integer") end end |
.convert_time_parameter(parameter) ⇒ Time
method to convert a string into a time parameter
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 (parameter, "time") end end |
.create_error_message(parameter, type) ⇒ Object (private)
method to create an error message when rescuing an error
50 51 52 53 54 55 56 57 |
# File 'lib/ruby_utils/parameter_converter.rb', line 50 private_class_method def self.(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 |