Class: ValidatesZipcode::Zipcode

Inherits:
Object
  • Object
show all
Includes:
CldrRegexpCollection
Defined in:
lib/validates_zipcode/zipcode.rb

Constant Summary

Constants included from CldrRegexpCollection

CldrRegexpCollection::ZIPCODES_REGEX

Instance Method Summary collapse

Methods included from CldrRegexpCollection

#regexp_for_country_alpha2

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

#formatObject


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

Returns:

  • (Boolean)

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