Class: ValidatesZipcode::Zipcode
- Inherits:
-
Object
- Object
- ValidatesZipcode::Zipcode
- Includes:
- CldrRegexpCollection
- Defined in:
- lib/validates_zipcode/zipcode.rb
Constant Summary
Constants included from CldrRegexpCollection
CldrRegexpCollection::ZIPCODES_REGEX
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(args = {}) ⇒ Zipcode
constructor
A new instance of Zipcode.
- #valid? ⇒ Boolean (also: #validate)
Methods included from CldrRegexpCollection
Constructor Details
#initialize(args = {}) ⇒ Zipcode
Returns a new instance of Zipcode.
6 7 8 9 10 |
# File 'lib/validates_zipcode/zipcode.rb', line 6 def initialize(args = {}) @zipcode = args.fetch(:zipcode) @country_alpha2 = args.fetch(:country_alpha2) @excluded_country_codes = args.fetch(:excluded_country_codes, []) end |
Instance Method Details
#format ⇒ Object
19 20 21 22 |
# File 'lib/validates_zipcode/zipcode.rb', line 19 def format raise InvalidZipcodeError, "invalid zipcode #{@zipcode} for country #{@country_alpha2.to_s.upcase}" unless valid? Formatter.new(zipcode: @zipcode, country_alpha2: @country_alpha2).format end |
#valid? ⇒ Boolean Also known as: validate
12 13 14 15 16 |
# File 'lib/validates_zipcode/zipcode.rb', line 12 def valid? return true if @excluded_country_codes.include?(@country_alpha2) return true unless regexp !!(regexp =~ @zipcode) end |