Module: SecID::Validatable
- Included in:
- Base
- Defined in:
- lib/sec_id/concerns/validatable.rb
Overview
Provides validation methods for identifier types.
Including classes should override #valid_format? and optionally #detect_errors
for type-specific validation.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- ERROR_MAP =
Maps error-code symbols to their exception classes; unmapped codes default to InvalidFormatError.
{ invalid_checksum: InvalidChecksumError, invalid_prefix: InvalidStructureError, invalid_category: InvalidStructureError, invalid_group: InvalidStructureError, invalid_attribute: InvalidStructureError, invalid_bban: InvalidStructureError, invalid_date: InvalidStructureError, invalid_country: InvalidStructureError }.freeze
Class Method Summary collapse
-
.included(base) ⇒ void
private
Extends the including identifier class with the concern's class methods.
Instance Method Summary collapse
-
#errors ⇒ Errors
Returns an Errors object with error codes and human-readable messages.
- #valid? ⇒ Boolean
-
#validate ⇒ self
Eagerly triggers validation and caches errors.
-
#validate! ⇒ self
Validates and returns self if valid, raises an exception otherwise.
Class Method Details
.included(base) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Extends the including identifier class with the concern's class methods.
26 27 28 |
# File 'lib/sec_id/concerns/validatable.rb', line 26 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#errors ⇒ Errors
Returns an Errors object with error codes and human-readable messages.
80 81 82 83 84 |
# File 'lib/sec_id/concerns/validatable.rb', line 80 def errors return @errors if defined?(@errors) @errors = Errors.new(error_codes.map { |code| build_error(code, (code)) }) end |
#valid? ⇒ Boolean
65 66 67 |
# File 'lib/sec_id/concerns/validatable.rb', line 65 def valid? valid_format? end |
#validate ⇒ self
Eagerly triggers validation and caches errors.
72 73 74 75 |
# File 'lib/sec_id/concerns/validatable.rb', line 72 def validate errors self end |
#validate! ⇒ self
Validates and returns self if valid, raises an exception otherwise.
90 91 92 93 94 95 |
# File 'lib/sec_id/concerns/validatable.rb', line 90 def validate! return self if valid? detail = errors.details.first raise self.class.error_class_for(detail[:error]), detail[:message] end |