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

Instance Method Details

#check_positive(number, name = 'Input') ⇒ void

This method returns an undefined value.

Validates that a number is positive (greater than zero)

Examples:

check_positive(10, 'Distance') #=> nil (valid)
check_positive(-5, 'Time')     #=> raises NonPositiveInputError
check_positive(0, 'Speed')     #=> raises NonPositiveInputError

Parameters:

  • number (Numeric)

    the number to validate

  • name (String) (defaults to: 'Input')

    the name of the parameter for error messages

Raises:



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”

Examples:

check_time('01:30:45') #=> nil (valid)
check_time('5:30')     #=> nil (valid)
check_time('invalid')  #=> raises InvalidTimeFormatError

Parameters:

  • time_string (String)

    the time string to validate

Raises:



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