Class: Amka::Utils
- Inherits:
-
Object
- Object
- Amka::Utils
- 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.
Class Method Summary collapse
-
.positive_integer_or_fail(num) ⇒ Object
Validates that the input is a positive integer.
-
.string_with_date_or_fail(date) ⇒ Object
Validates that the input is a string with date in format dd/mm/yyyy.
-
.string_with_digits_or_empty_or_fail(id) ⇒ Object
Validates that the input is a string with only digits or empty.
-
.string_with_digits_or_fail(id) ⇒ Object
Validates that the input is a string with only digits.
-
.valid_date?(date, year = nil) ⇒ Boolean
Validates that the given date string is a valid date.
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.
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)
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.
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.
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:
-
Checks the basic format
-
Confirms the date actually exists in the calendar
-
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.
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 |