going_postal 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/going_postal.rb +18 -20
  2. metadata +3 -3
data/lib/going_postal.rb CHANGED
@@ -66,13 +66,10 @@ module GoingPostal
66
66
  #
67
67
  def postcode?(*args)
68
68
  string, country_code = get_args_for_format_postcode(args)
69
- case country_code.to_s.upcase
70
- when "GB", "UK", "US", "USA", "CA", "AU", "NZ", "ZA"
71
- format_postcode(string, country_code) || false
72
- when "IE"
69
+ if country_code.to_s.upcase == "IE"
73
70
  string.nil? || string.to_s.empty? ? "" : false
74
71
  else
75
- format_postcode(string, country_code)
72
+ format_postcode(string, country_code) || false
76
73
  end
77
74
  end
78
75
  alias post_code? postcode?
@@ -123,23 +120,15 @@ module GoingPostal
123
120
  # trailing whitespace.
124
121
  #
125
122
  # Ireland (IE) has no postcodes, so nil will always be returned.
126
- #
123
+ #--
124
+ # The magic is done calling a formatting method for each country. If no such
125
+ # method exists a default method is called stripping the leading and trailing
126
+ # whitespace.
127
+ #++
127
128
  def format_postcode(*args)
128
129
  string, country_code = get_args_for_format_postcode(args)
129
- case country_code.to_s.upcase
130
- when "GB", "UK"
131
- format_gb_postcode(string)
132
- when "US", "USA"
133
- format_us_zipcode(string)
134
- when "CA"
135
- format_ca_postcode(string)
136
- when "AU", "NZ", "ZA"
137
- format_au_postcode(string)
138
- when "IE"
139
- nil
140
- else
141
- string.to_s.strip
142
- end
130
+ method = :"format_#{country_code.to_s.downcase}_postcode"
131
+ respond_to?(method) ? __send__(method, string) : string.to_s.strip
143
132
  end
144
133
  alias format_post_code format_postcode
145
134
  alias format_zip format_postcode
@@ -148,6 +137,10 @@ module GoingPostal
148
137
 
149
138
  # :stopdoc:
150
139
 
140
+ def format_ie_postcode(string)
141
+ nil
142
+ end
143
+
151
144
  def format_gb_postcode(string)
152
145
  out_code = string.to_s.upcase.delete(" \t\r\n")
153
146
  in_code = out_code.slice!(-3, 3)
@@ -184,6 +177,11 @@ module GoingPostal
184
177
  end
185
178
  alias format_us_postcode format_us_zipcode
186
179
 
180
+ def format_ch_postcode(string)
181
+ string = string.to_s.delete(" \t\r\n")
182
+ string if string =~ /^[1-9][0-9]{3}$/
183
+ end
184
+
187
185
  private
188
186
 
189
187
  def get_args_for_format_postcode(args)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: going_postal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-21 00:00:00.000000000Z
12
+ date: 2012-02-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Post/zip code formatting and validation for the UK, US, CA and more.
15
15
  email: mat@sourcetagsandcodes.com
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  requirements: []
46
46
  rubyforge_project:
47
- rubygems_version: 1.8.7
47
+ rubygems_version: 1.8.11
48
48
  signing_key:
49
49
  specification_version: 3
50
50
  summary: Global post/zip code formatting and validation mixin