Class: Phonelib::ValidationError
- Inherits:
-
Object
- Object
- Phonelib::ValidationError
- Defined in:
- lib/phonelib/validation_error.rb
Overview
A framework-independent, immutable phone validation diagnostic.
Constant Summary collapse
- DEFAULT_MESSAGES =
{ not_a_number: 'is not a phone number', invalid_country_code: 'has an invalid country calling code', too_short: 'is too short', too_long: 'is too long', invalid_length: 'has an invalid length', not_possible: 'is not a possible phone number', invalid: 'is not a valid phone number', type_not_allowed: 'has a phone type that is not allowed', country_not_allowed: 'is from a country that is not allowed', extension_not_allowed: 'must not include an extension' }.freeze
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
Instance Method Summary collapse
- #i18n_key ⇒ Object
-
#initialize(code, details = {}) ⇒ ValidationError
constructor
A new instance of ValidationError.
- #message(translator = nil) ⇒ Object
Constructor Details
#initialize(code, details = {}) ⇒ ValidationError
Returns a new instance of ValidationError.
21 22 23 24 25 26 27 28 |
# File 'lib/phonelib/validation_error.rb', line 21 def initialize(code, details = {}) @code = DEFAULT_MESSAGES.keys.find { |key| key.to_s == code.to_s } unless @code raise ArgumentError, "unknown validation error code: #{code}" end @details = immutable_copy(details) freeze end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
19 20 21 |
# File 'lib/phonelib/validation_error.rb', line 19 def code @code end |
#details ⇒ Object (readonly)
Returns the value of attribute details.
19 20 21 |
# File 'lib/phonelib/validation_error.rb', line 19 def details @details end |
Instance Method Details
#i18n_key ⇒ Object
30 31 32 |
# File 'lib/phonelib/validation_error.rb', line 30 def i18n_key "phonelib.errors.#{code}".to_sym end |
#message(translator = nil) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/phonelib/validation_error.rb', line 34 def (translator = nil) default = DEFAULT_MESSAGES.fetch(code, code.to_s.tr('_', ' ')) return default unless translator translator.call(i18n_key, **{ default: default }.merge(details)) end |