Class: Israeli::Validators::PostalCode
- Inherits:
-
Object
- Object
- Israeli::Validators::PostalCode
- Defined in:
- lib/israeli/validators/postal_code.rb
Overview
Validates Israeli postal codes (Mikud).
Israeli postal codes consist of exactly 7 digits. They are organized geographically from north to south, with the first 2 digits indicating the postal area. Jerusalem postal codes start with 9 despite its central location.
Class Method Summary collapse
-
.format(value, style: :compact) ⇒ String?
Formats a postal code to standard representation.
-
.invalid_reason(value) ⇒ Symbol?
Returns the reason why a postal code is invalid.
-
.valid?(value) ⇒ Boolean
Validates an Israeli postal code.
Class Method Details
.format(value, style: :compact) ⇒ String?
Formats a postal code to standard representation.
62 63 64 65 66 67 68 69 70 |
# File 'lib/israeli/validators/postal_code.rb', line 62 def self.format(value, style: :compact) digits = Sanitizer.digits_only(value) return nil unless valid?(digits) case style when :spaced then "#{digits[0..4]} #{digits[5..6]}" else digits end end |
.invalid_reason(value) ⇒ Symbol?
Returns the reason why a postal code is invalid.
45 46 47 48 49 50 51 |
# File 'lib/israeli/validators/postal_code.rb', line 45 def self.invalid_reason(value) digits = Sanitizer.digits_only(value) return :blank if digits.nil? || digits.empty? return :wrong_length unless digits.match?(/\A\d{7}\z/) nil end |
.valid?(value) ⇒ Boolean
Validates an Israeli postal code.
27 28 29 30 31 32 |
# File 'lib/israeli/validators/postal_code.rb', line 27 def self.valid?(value) digits = Sanitizer.digits_only(value) return false if digits.nil? digits.match?(/\A\d{7}\z/) end |