Class: CountriesData::PhoneValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/countries_data/phone_validator.rb

Constant Summary collapse

PHONE_REGEXP =
Regexp.new('^\\d+$')

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/countries_data/phone_validator.rb', line 5

def code
  @code
end

#numberObject (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_lengthObject



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_lengthObject



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

Returns:

  • (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

Returns:

  • (Boolean)


23
24
25
# File 'lib/countries_data/phone_validator.rb', line 23

def valid_code?
  !countries.empty?
end

#valid_format?Boolean

Returns:

  • (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

Returns:

  • (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