Module: Checker
- Included in:
- Calcpace
- Defined in:
- lib/calcpace/checker.rb
Overview
Module to validate input values and formats
This module provides validation methods for numeric inputs and time format strings used throughout the Calcpace gem.
Instance Method Summary collapse
-
#check_positive(number, name = 'Input') ⇒ void
Validates that a number is positive (greater than zero).
-
#check_time(time_string) ⇒ void
Validates that a time string is in the correct format.
Instance Method Details
#check_positive(number, name = 'Input') ⇒ void
This method returns an undefined value.
Validates that a number is positive (greater than zero)
21 22 23 24 25 26 |
# File 'lib/calcpace/checker.rb', line 21 def check_positive(number, name = 'Input') return if number.is_a?(Numeric) && number.positive? raise Calcpace::NonPositiveInputError, "#{name} must be a positive number" end |
#check_time(time_string) ⇒ void
This method returns an undefined value.
Validates that a time string is in the correct format
Accepted formats:
-
HH:MM:SS (hours:minutes:seconds) - e.g., “01:30:45”
-
MM:SS (minutes:seconds) - e.g., “05:30”
-
H:MM:SS or M:SS (single digit hours/minutes) - e.g., “1:30:45”
43 44 45 46 47 48 49 50 51 |
# File 'lib/calcpace/checker.rb', line 43 def check_time(time_string) # Check if string is valid and matches expected patterns return if time_string.is_a?(String) && (time_string =~ /\A\d{1,2}:\d{2}:\d{2}\z/ || time_string =~ /\A\d{1,2}:\d{2}\z/) raise Calcpace::InvalidTimeFormatError, 'It must be a valid time in the XX:XX:XX or XX:XX format' end |