Class: ValidatesZipcode::Formatter
- Inherits:
-
Object
- Object
- ValidatesZipcode::Formatter
- Defined in:
- lib/validates_zipcode/formatter.rb
Constant Summary collapse
- ZIPCODES_TRANSFORMATIONS =
{ AT: ->(z) { z.scan(/\d/).join }, CZ: ->(z) { z.scan(/\d/).insert(3, ' ').join }, DE: ->(z) { z.scan(/\d/).join.rjust(5, "0") }, GB: ->(z) { z.upcase.scan(/[A-Z0-9]/).insert(-4, ' ').join }, NL: ->(z) { z.upcase.scan(/[A-Z0-9]/).insert(4, ' ').join }, PL: ->(z) { z.scan(/\d/).insert(2, '-').join }, SK: :CZ, UK: :GB, US: ->(z) { digits = z.scan(/\d/) digits.insert(5, '-') if digits.count > 5 digits.join } }
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(args = {}) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(args = {}) ⇒ Formatter
Returns a new instance of Formatter.
20 21 22 23 |
# File 'lib/validates_zipcode/formatter.rb', line 20 def initialize(args = {}) @zipcode = args.fetch(:zipcode).to_s @country_alpha2 = args.fetch(:country_alpha2).to_s.upcase.to_sym end |
Instance Method Details
#format ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/validates_zipcode/formatter.rb', line 25 def format transformation = ZIPCODES_TRANSFORMATIONS[@country_alpha2] case transformation when Proc transformation.call(@zipcode) when Symbol ZIPCODES_TRANSFORMATIONS[transformation].call(@zipcode) else @zipcode.strip end end |