Class: CountriesData::PhoneValidator
- Inherits:
-
Object
- Object
- CountriesData::PhoneValidator
- Defined in:
- lib/countries_data/phone_validator.rb
Constant Summary collapse
- PHONE_REGEXP =
Regexp.new('^\\d+$')
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
-
#initialize(code: nil, number: nil) ⇒ PhoneValidator
constructor
A new instance of PhoneValidator.
- #max_length ⇒ Object
- #min_length ⇒ Object
- #valid? ⇒ Boolean
- #valid_code? ⇒ Boolean
- #valid_format? ⇒ Boolean
- #valid_length? ⇒ Boolean
Constructor Details
#initialize(code: nil, number: nil) ⇒ PhoneValidator
Returns a new instance of PhoneValidator.
8 9 10 11 |
# File 'lib/countries_data/phone_validator.rb', line 8 def initialize(code: nil, number: nil) @code = code @number = number end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/countries_data/phone_validator.rb', line 5 def code @code end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
6 7 8 |
# File 'lib/countries_data/phone_validator.rb', line 6 def number @number end |
Instance Method Details
#max_length ⇒ Object
40 41 42 43 44 |
# File 'lib/countries_data/phone_validator.rb', line 40 def max_length countries .filter_map(&:phone_max_length) .max || Float::INFINITY end |
#min_length ⇒ Object
34 35 36 37 38 |
# File 'lib/countries_data/phone_validator.rb', line 34 def min_length countries .filter_map(&:phone_min_length) .min || 0 end |
#valid? ⇒ Boolean
15 16 17 |
# File 'lib/countries_data/phone_validator.rb', line 15 def valid? valid_format? && valid_code? && valid_length? end |
#valid_code? ⇒ Boolean
23 24 25 |
# File 'lib/countries_data/phone_validator.rb', line 23 def valid_code? !countries.empty? end |
#valid_format? ⇒ Boolean
19 20 21 |
# File 'lib/countries_data/phone_validator.rb', line 19 def valid_format? PHONE_REGEXP.match?(code) && PHONE_REGEXP.match?(number) end |
#valid_length? ⇒ Boolean
27 28 29 30 31 32 |
# File 'lib/countries_data/phone_validator.rb', line 27 def valid_length? return false unless number&.length length = number.length length >= min_length && length <= max_length end |