Class: Amka::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/amka/utils.rb

Overview

Utility methods for the Amka module

This class provides helper methods for parameter validation and date handling that are used throughout the AMKA implementation. These utilities encapsulate common validation logic and error handling to keep the main code cleaner.

The methods primarily fall into these categories:

  • String validation (digits only, format checking)

  • Number validation

  • Date validation and conversion

All validation methods follow a consistent pattern: they either succeed silently or raise descriptive ArgumentError exceptions.

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.positive_integer_or_fail(num) ⇒ Object

Validates that the input is a positive integer

Used primarily for length/count parameters where negative or zero values would be invalid.

Examples:

Utils.positive_integer_or_fail(10)  # => nil (success)
Utils.positive_integer_or_fail(0)   # => ArgumentError
Utils.positive_integer_or_fail(-5)  # => ArgumentError
Utils.positive_integer_or_fail('5') # => ArgumentError

Parameters:

  • num (Integer)

    the number to validate

Raises:

  • (ArgumentError)

    if the number is not a positive integer

Since:

  • 1.0.0



87
88
89
90
91
# File 'lib/amka/utils.rb', line 87

def positive_integer_or_fail(num)
  return if num.is_a?(Integer) && num.positive?

  raise ArgumentError, "'#{num}': must be a non-zero positive integer!"
end

.string_with_date_or_fail(date) ⇒ Object

Validates that the input is a string with date in format dd/mm/yyyy

Checks that the string follows the Greek date format where:

  • Day can be 1 or 2 digits (1-31)

  • Month can be 1 or 2 digits (1-12)

  • Year must be 4 digits

Note: This only checks the format, not if the date is valid (e.g., 31/02/2022 would pass this check but is not a valid date)

Examples:

Utils.string_with_date_or_fail('1/1/2020')   # => nil (success)
Utils.string_with_date_or_fail('31/12/2020') # => nil (success)
Utils.string_with_date_or_fail('2020-12-31') # => ArgumentError

Parameters:

  • date (String)

    the date string to validate

Raises:

  • (ArgumentError)

    if the date format is invalid

Since:

  • 1.0.0



69
70
71
72
73
# File 'lib/amka/utils.rb', line 69

def string_with_date_or_fail(date)
  return if date.is_a?(String) && date.match(%r{\A\d?\d{1}/\d?\d{1}/\d{4}\Z})

  raise ArgumentError, 'date of birth must be in this format: [d]d/[m]m/yyyy'
end

.string_with_digits_or_empty_or_fail(id) ⇒ Object

Validates that the input is a string with only digits or empty

Similar to string_with_digits_or_fail but allows empty strings. Used when an optional string of digits is expected.

Examples:

Utils.string_with_digits_or_empty_or_fail('12345')  # => nil (success)
Utils.string_with_digits_or_empty_or_fail('')       # => nil (success)
Utils.string_with_digits_or_empty_or_fail('123abc') # => ArgumentError

Parameters:

  • id (String)

    the string to validate

Raises:

  • (ArgumentError)

    if the string contains non-digits

Since:

  • 1.0.0



47
48
49
50
51
# File 'lib/amka/utils.rb', line 47

def string_with_digits_or_empty_or_fail(id)
  return if id.is_a?(String) && id.match(/\A\d*\Z/)

  raise ArgumentError, "'#{id}': must be a string of digits or even an empty one!"
end

.string_with_digits_or_fail(id) ⇒ Object

Validates that the input is a string with only digits

Used when a value must be a non-empty string of digits, such as an ID or number that needs to be processed digit by digit.

Examples:

Utils.string_with_digits_or_fail('12345')  # => nil (success)
Utils.string_with_digits_or_fail('123abc') # => ArgumentError
Utils.string_with_digits_or_fail('')       # => ArgumentError

Parameters:

  • id (String)

    the string to validate

Raises:

  • (ArgumentError)

    if the string is not all digits

Since:

  • 1.0.0



30
31
32
33
34
# File 'lib/amka/utils.rb', line 30

def string_with_digits_or_fail(id)
  return if id.is_a?(String) && id.match(/\A\d+\Z/)

  raise ArgumentError, "'#{id}': must be a string of digits only!"
end

.valid_date?(date, year = nil) ⇒ Boolean

Validates that the given date string is a valid date

This is a more comprehensive date validation that:

  1. Checks the basic format

  2. Confirms the date actually exists in the calendar

  3. Optionally verifies it matches a specific 4-digit year

This is particularly important for AMKA validation since the first 6 digits must represent a valid date of birth.

Examples:

Simple validation (auto-guesses century)

Utils.valid_date?('010190')  # => true (January 1, 1990)

With explicit year

Utils.valid_date?('010190', '1990')  # => true
Utils.valid_date?('010190', '2090')  # => ArgumentError

Parameters:

  • date (String)

    the date string to validate in format ddmmyy

  • year (String, nil) (defaults to: nil)

    optional 4-digit year to check against

Returns:

  • (Boolean)

    true if date is valid, false otherwise

Raises:

  • (ArgumentError)

    if year format or range is invalid

Since:

  • 1.0.0



112
113
114
115
116
117
118
119
120
121
# File 'lib/amka/utils.rb', line 112

def valid_date?(date, year = nil)
  return false unless date.match(/\A\d{6,}\Z/)

  if year
    validate_year_format(year, date)
    return validate_full_date(date, year)
  end

  validate_short_date(date)
end