biggs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ biggs-*.*.*.gem
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.2@biggs --create
data/CHANGES.textile ADDED
@@ -0,0 +1,33 @@
1
+ h3. biggs 0.2.0 - 2011-10-12
2
+
3
+ * Added Bundler & removed jewler
4
+ * Fixed Specs
5
+ * Added NC (New Caledonia)
6
+
7
+ h3. biggs 0.1.5 - 2010-11-24
8
+
9
+ * Fixed address format for Norway
10
+ * Added TH (Thailand)
11
+
12
+ h3. biggs 0.1.4 - 2009-5-21
13
+
14
+ * Fixed Specs
15
+
16
+ h3. biggs 0.1.3 - 2009-3-6
17
+
18
+ * Correct japanese address format. [hiroshi]
19
+ * Fixed docs for current API. [hiroshi]
20
+
21
+ h3. biggs 0.1.2 - 2009-3-4
22
+
23
+ * Values can now be specified by an array of symbols.
24
+
25
+ h3. biggs 0.1.1 - 2009-3-3
26
+
27
+ * Refactored activerecord-adapter to include only basic setup method in ActiveRecord:Base
28
+
29
+ h3. biggs 0.1.0 - 2009-3-3
30
+
31
+ * Allow Procs as params in biggs-activerecord-setup.
32
+ * Cleanup
33
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dar_da_da.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ biggs (0.2.0)
5
+ activerecord (>= 3.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.1.1)
11
+ activesupport (= 3.1.1)
12
+ builder (~> 3.0.0)
13
+ i18n (~> 0.6)
14
+ activerecord (3.1.1)
15
+ activemodel (= 3.1.1)
16
+ activesupport (= 3.1.1)
17
+ arel (~> 2.2.1)
18
+ tzinfo (~> 0.3.29)
19
+ activesupport (3.1.1)
20
+ multi_json (~> 1.0)
21
+ arel (2.2.1)
22
+ builder (3.0.0)
23
+ diff-lcs (1.1.3)
24
+ i18n (0.6.0)
25
+ multi_json (1.0.3)
26
+ rake (0.9.2)
27
+ rspec (2.6.0)
28
+ rspec-core (~> 2.6.0)
29
+ rspec-expectations (~> 2.6.0)
30
+ rspec-mocks (~> 2.6.0)
31
+ rspec-core (2.6.4)
32
+ rspec-expectations (2.6.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.6.0)
35
+ sqlite3 (1.3.4)
36
+ sqlite3-ruby (1.3.3)
37
+ sqlite3 (>= 1.3.3)
38
+ tzinfo (0.3.30)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ biggs!
45
+ rake
46
+ rspec (>= 2.4.0)
47
+ sqlite3-ruby
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Yolk Sebastian Munz & Julia Soergel GbR
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,150 @@
1
+ biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
2
+
3
+ h3. Install
4
+
5
+ As a ruby gem:
6
+
7
+ sudo gem install yolk-biggs
8
+
9
+ If your rather prefer to install it as a plugin for rails, from your application directory simply run:
10
+
11
+ script/plugin install git://github.com/yolk/biggs.git
12
+
13
+ h3. Standalone usage
14
+
15
+ f = Biggs::Formatter.new
16
+
17
+ f.format("de", # <= ISO alpha 2 code
18
+ :recipient => "Yolk Sebastian Munz & Julia Soergel GbR",
19
+ :street => "Adalbertstr. 11", # <= street + house number
20
+ :city => "Berlin",
21
+ :zip => 10999,
22
+ :state => "Berlin" # <= state/province/region
23
+ )
24
+
25
+ returns
26
+
27
+ "Yolk Sebastian Munz & Julia Soergel GbR
28
+ Adalbertstr. 11
29
+ 10999 Berlin
30
+ Germany"
31
+
32
+ At the moment Biggs::Formatter.new accepts only one option:
33
+
34
+ *blank_county_on* ISO alpha 2 code (single string or array) of countries the formatter should skip the line "country" (for national shipping).
35
+
36
+ f = Biggs::Formatter.new(:blank_county_on => "de")
37
+
38
+ With the data from the above example this would return:
39
+
40
+ "Yolk Sebastian Munz & Julia Soergel GbR
41
+ Adalbertstr. 11
42
+ 10999 Berlin"
43
+
44
+ h3. Usage with Rails and ActiveRecord
45
+
46
+ Address < ActiveRecord::Base
47
+ biggs :postal_address
48
+ end
49
+
50
+ This adds the method postal_address to your Address-model, and assumes the presence of the methods/columns recipient, street, city, zip, state, and country to get the address data. Country should return the ISO-code (e.g. 'us', 'fr', 'de').
51
+
52
+ You can customize the method-names biggs will use by passing in a hash of options:
53
+
54
+ Address < ActiveRecord::Base
55
+ biggs :postal_address,
56
+ :zip => :postal_code,
57
+ :country => :country_code,
58
+ :street => Proc.new {|address| "#{address.street} #{address.house_number}" }
59
+ end
60
+
61
+ You can pass in a symbol to let biggs call a different method on your Address-model, or a Proc-object to create your data on the fly.
62
+
63
+ You can even pass in a array of symbols:
64
+
65
+ Address < ActiveRecord::Base
66
+ biggs :postal_address,
67
+ :recipient => [:company_name, :person_name]
68
+ end
69
+
70
+ This will call the methods company_name and person_name on your address-instance, remove any blank returned values and join the rest by a line break.
71
+
72
+ To access the formatted address string, simply call the provided method on an address instance:
73
+
74
+ Address.find(1).postal_address
75
+
76
+ If you pass in a ISO alpha 2 code as :country that is not supported by biggs, it will choose the US-format for addresses with an state specified, and the french/german format for addresses without an state.
77
+
78
+ h3. Supported countries
79
+
80
+ biggs knows how to format addresses of over 60 different countries. If you are missing one or find an misstake, feel free to let us know, fork this repository and commit your additions.
81
+
82
+ * Argentina
83
+ * Australia
84
+ * Austria
85
+ * Bahrain
86
+ * Belgium
87
+ * Bosnia and Herzegovina
88
+ * Brazil
89
+ * Bulgaria
90
+ * Canada
91
+ * China
92
+ * Croatia
93
+ * Czech
94
+ * Denmark
95
+ * Egypt
96
+ * Finland
97
+ * France
98
+ * Germany
99
+ * Greece
100
+ * Greenland
101
+ * Hong Kong
102
+ * Hungary
103
+ * Iceland
104
+ * India
105
+ * Indonesia
106
+ * Ireland
107
+ * Israel
108
+ * Italy
109
+ * Japan
110
+ * Jordan
111
+ * Kuwait
112
+ * Lebanon
113
+ * Lichtenstein
114
+ * Luxembourg
115
+ * Macedonia
116
+ * Mexico
117
+ * Netherlands
118
+ * New Caledonia
119
+ * New Zealand
120
+ * Norway
121
+ * Oman
122
+ * Philippines
123
+ * Poland
124
+ * Portugal
125
+ * Qatar
126
+ * Romania
127
+ * Russian Federation
128
+ * Saudi Arabia
129
+ * Serbia and Montenegro
130
+ * Singapore
131
+ * Slovakia
132
+ * Slovenia
133
+ * South Africa
134
+ * South Korea
135
+ * Spain
136
+ * Sweden
137
+ * Switzerland
138
+ * Syrian Arab Republic
139
+ * Taiwan
140
+ * Thailand
141
+ * Turkey
142
+ * Ukraine
143
+ * United Arab Emirates
144
+ * United Kingdom
145
+ * United States
146
+ * Yemen
147
+
148
+ biggs is tested to behave well with Rails 2.2.2.
149
+
150
+ Copyright (c) 2009 Yolk Sebastian Munz & Julia Soergel GbR
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/biggs.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "biggs/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "biggs"
7
+ s.version = Biggs::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sebastian Munz"]
10
+ s.email = ["sebastian@yo.lk"]
11
+ s.homepage = "https://github.com/yolk/biggs"
12
+ s.summary = %q{biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.}
13
+ s.description = %q{biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.}
14
+
15
+ s.rubyforge_project = "biggs"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activerecord', '>= 3.0'
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'rspec', '>= 2.4.0'
25
+ s.add_development_dependency 'sqlite3-ruby'
26
+ end
27
+
data/formats.yml ADDED
@@ -0,0 +1,494 @@
1
+ # Avaible placeholders:
2
+ # * recipient
3
+ # * street
4
+ # * zip
5
+ # * city
6
+ # * state
7
+ # * country
8
+ #
9
+ ---
10
+ ae:
11
+ name: United Arab Emirates
12
+ format: |-
13
+ {{recipient}}
14
+ {{street}}
15
+ {{zip}} {{city}}
16
+ {{country}}
17
+ ar:
18
+ name: Argentina
19
+ format: |-
20
+ {{recipient}}
21
+ {{street}}
22
+ {{zip}} {{city}}
23
+ {{state}}
24
+ {{country}}
25
+ at:
26
+ name: Austria
27
+ format: |-
28
+ {{recipient}}
29
+ {{street}}
30
+ {{zip}} {{city}}
31
+ {{country}}
32
+ au:
33
+ name: Australia
34
+ format: |-
35
+ {{recipient}}
36
+ {{street}}
37
+ {{city}} {{state}} {{zip}}
38
+ {{country}}
39
+ ba:
40
+ name: Bosnia and Herzegovina
41
+ format: |-
42
+ {{recipient}}
43
+ {{street}}
44
+ {{zip}} {{city}}
45
+ {{country}}
46
+ be:
47
+ name: Belgium
48
+ format: |-
49
+ {{recipient}}
50
+ {{street}}
51
+ {{zip}} {{city}}
52
+ {{country}}
53
+ bg:
54
+ name: Bulgaria
55
+ format: |-
56
+ {{recipient}}
57
+ {{street}}
58
+ {{zip}} {{city}}
59
+ {{country}}
60
+ bh:
61
+ name: Bahrain
62
+ format: |-
63
+ {{recipient}}
64
+ {{street}}
65
+ {{zip}} {{city}}
66
+ {{country}}
67
+ br:
68
+ name: Brazil
69
+ format: |-
70
+ {{recipient}}
71
+ {{street}}
72
+ {{zip}} {{city}} {{state}}
73
+ {{country}}
74
+ ca:
75
+ name: Canada
76
+ format: |-
77
+ {{recipient}}
78
+ {{street}}
79
+ {{city}} {{state}} {{zip}}
80
+ {{country}}
81
+ ch:
82
+ name: Switzerland
83
+ format: |-
84
+ {{recipient}}
85
+ {{street}}
86
+ {{zip}} {{city}}
87
+ {{country}}
88
+ cn:
89
+ name: China
90
+ format: |-
91
+ {{recipient}}
92
+ {{street}}
93
+ {{zip}} {{city}} {{state}}
94
+ {{country}}
95
+ cz:
96
+ name: Czech
97
+ format: |-
98
+ {{recipient}}
99
+ {{street}}
100
+ {{zip}} {{city}}
101
+ {{country}}
102
+ de:
103
+ name: Germany
104
+ format: |-
105
+ {{recipient}}
106
+ {{street}}
107
+ {{zip}} {{city}}
108
+ {{country}}
109
+ dk:
110
+ name: Denmark
111
+ format: |-
112
+ {{recipient}}
113
+ {{street}}
114
+ {{zip}} {{city}}
115
+ {{state}}
116
+ {{country}}
117
+ eg:
118
+ name: Egypt
119
+ format: |-
120
+ {{recipient}}
121
+ {{street}}
122
+ {{zip}} {{city}}
123
+ {{country}}
124
+ es:
125
+ name: Spain
126
+ format: |-
127
+ {{recipient}}
128
+ {{street}}
129
+ {{zip}} {{city}} {{state}}
130
+ {{country}}
131
+ fi:
132
+ name: Finland
133
+ format: |-
134
+ {{recipient}}
135
+ {{street}}
136
+ {{zip}} {{city}}
137
+ {{country}}
138
+ fr:
139
+ name: France
140
+ format: |-
141
+ {{recipient}}
142
+ {{street}}
143
+ {{zip}} {{city}}
144
+ {{country}}
145
+ gb:
146
+ name: United Kingdom
147
+ format: |-
148
+ {{recipient}}
149
+ {{street}}
150
+ {{city}}
151
+ {{state}}
152
+ {{zip}}
153
+ {{country}}
154
+ gl:
155
+ name: Greenland
156
+ format: |-
157
+ {{recipient}}
158
+ {{street}}
159
+ {{zip}} {{city}}
160
+ {{country}}
161
+ gr:
162
+ name: Greece
163
+ format: |-
164
+ {{recipient}}
165
+ {{street}}
166
+ {{zip}} {{city}}
167
+ {{country}}
168
+ hk:
169
+ name: Hong Kong
170
+ format: |-
171
+ {{recipient}}
172
+ {{street}}
173
+ {{zip}} {{city}} {{state}}
174
+ {{country}}
175
+ hr:
176
+ name: Croatia
177
+ format: |-
178
+ {{recipient}}
179
+ {{street}}
180
+ {{zip}} {{city}}
181
+ {{country}}
182
+ hu:
183
+ name: Hungary
184
+ format: |-
185
+ {{recipient}}
186
+ {{city}}
187
+ {{street}}
188
+ {{zip}}
189
+ {{country}}
190
+ id:
191
+ name: Indonesia
192
+ format: |-
193
+ {{recipient}}
194
+ {{street}}
195
+ {{city}}
196
+ {{state}} {{zip}}
197
+ {{country}}
198
+ ie:
199
+ name: Ireland
200
+ format: |-
201
+ {{recipient}}
202
+ {{street}}
203
+ {{city}} {{state}} {{zip}}
204
+ {{country}}
205
+ il:
206
+ name: Israel
207
+ format: |-
208
+ {{recipient}}
209
+ {{street}}
210
+ {{zip}} {{city}}
211
+ {{country}}
212
+ in:
213
+ name: India
214
+ format: |-
215
+ {{recipient}}
216
+ {{street}}
217
+ {{state}}
218
+ {{city}} {{zip}}
219
+ {{country}}
220
+ is:
221
+ name: Iceland
222
+ format: |-
223
+ {{recipient}}
224
+ {{street}}
225
+ {{zip}} {{city}}
226
+ {{country}}
227
+ it:
228
+ name: Italy
229
+ format: |-
230
+ {{recipient}}
231
+ {{street}}
232
+ {{zip}} {{city}} {{state}}
233
+ {{country}}
234
+ jo:
235
+ name: Jordan
236
+ format: |-
237
+ {{recipient}}
238
+ {{street}}
239
+ {{zip}} {{city}}
240
+ {{country}}
241
+ # Here is a sample of Japanese formatted addresses (Apple Japan HQ).
242
+ # Usually they have "Postal mark" before zip (postal) code,
243
+ # and parts of an address are not separated with space.
244
+ # Biggs::Formatter.new(:blank_country_on => "jp").format(
245
+ # "jp",
246
+ # :recipient => "アップルジャパン株式会社 本社",
247
+ # :street => "西新宿3-20-2",
248
+ # :city => "新宿区",
249
+ # :state => "東京都",
250
+ # :zip => "163-1480")
251
+ # 〒163-1480
252
+ # 東京都新宿区西新宿3-20-2
253
+ # アップルジャパン株式会社 本社
254
+ jp:
255
+ name: Japan
256
+ format: |-
257
+ 〒{{zip}}
258
+ {{state}}{{city}}{{street}}
259
+ {{recipient}}
260
+ {{country}}
261
+ kr:
262
+ name: South Korea
263
+ format: |-
264
+ {{recipient}}
265
+ {{street}}
266
+ {{city}} {{state}}
267
+ {{zip}}
268
+ {{country}}
269
+ kw:
270
+ name: Kuwait
271
+ format: |-
272
+ {{recipient}}
273
+ {{street}}
274
+ {{zip}} {{city}}
275
+ {{state}}
276
+ {{country}}
277
+ lb:
278
+ name: Lebanon
279
+ format: |-
280
+ {{recipient}}
281
+ {{street}}
282
+ {{zip}} {{city}}
283
+ {{country}}
284
+ li:
285
+ name: Liechtenstein
286
+ format: |-
287
+ {{recipient}}
288
+ {{street}}
289
+ {{zip}} {{city}}
290
+ {{country}}
291
+ lu:
292
+ name: Luxembourg
293
+ format: |-
294
+ {{recipient}}
295
+ {{street}}
296
+ {{zip}} {{city}}
297
+ {{country}}
298
+ mk:
299
+ name: Macedonia
300
+ format: |-
301
+ {{recipient}}
302
+ {{street}}
303
+ {{city}} {{zip}}
304
+ {{country}}
305
+ mx:
306
+ name: Mexico
307
+ format: |-
308
+ {{recipient}}
309
+ {{street}}
310
+ {{zip}} {{city}} {{state}}
311
+ {{country}}
312
+ nc:
313
+ name: New Caledonia
314
+ format: |-
315
+ {{recipient}}
316
+ {{street}}
317
+ {{zip}} {{city}}
318
+ {{country}}
319
+ nl:
320
+ name: Netherlands
321
+ format: |-
322
+ {{recipient}}
323
+ {{street}}
324
+ {{zip}} {{city}}
325
+ {{country}}
326
+ "no":
327
+ name: Norway
328
+ format: |-
329
+ {{recipient}}
330
+ {{street}}
331
+ {{zip}} {{city}}
332
+ {{country}}
333
+ nz:
334
+ name: New Zealand
335
+ format: |-
336
+ {{recipient}}
337
+ {{street}}
338
+ {{state}}
339
+ {{city}} {{zip}}
340
+ {{country}}
341
+ om:
342
+ name: Oman
343
+ format: |-
344
+ {{recipient}}
345
+ {{street}}
346
+ {{zip}} {{city}}
347
+ {{state}}
348
+ {{country}}
349
+ ph:
350
+ name: Philippines
351
+ format: |-
352
+ {{recipient}}
353
+ {{street}} {{state}}
354
+ {{zip}} {{city}}
355
+ {{country}}
356
+ pl:
357
+ name: Poland
358
+ format: |-
359
+ {{recipient}}
360
+ {{street}}
361
+ {{zip}} {{city}}
362
+ {{state}}
363
+ {{country}}
364
+ pt:
365
+ name: Portugal
366
+ format: |-
367
+ {{recipient}}
368
+ {{street}}
369
+ {{zip}} {{city}} {{state}}
370
+ {{country}}
371
+ qa:
372
+ name: Qatar
373
+ format: |-
374
+ {{recipient}}
375
+ {{street}}
376
+ {{zip}} {{city}}
377
+ {{country}}
378
+ ro:
379
+ name: Romania
380
+ format: |-
381
+ {{recipient}}
382
+ {{street}}
383
+ {{zip}} {{city}}
384
+ {{country}}
385
+ ru:
386
+ name: Russian Federation
387
+ format: |-
388
+ {{recipient}}
389
+ {{zip}} {{city}}
390
+ {{street}}
391
+ {{country}}
392
+ sa:
393
+ name: Saudi Arabia
394
+ format: |-
395
+ {{recipient}}
396
+ {{street}}
397
+ {{zip}} {{city}}
398
+ {{country}}
399
+ se:
400
+ name: Sweden
401
+ format: |-
402
+ {{recipient}}
403
+ {{street}}
404
+ {{zip}} {{city}}
405
+ {{country}}
406
+ sg:
407
+ name: Singapore
408
+ format: |-
409
+ {{recipient}}
410
+ {{street}}
411
+ {{city}} {{zip}}
412
+ {{country}}
413
+ si:
414
+ name: Slovenia
415
+ format: |-
416
+ {{recipient}}
417
+ {{street}}
418
+ {{zip}} {{city}}
419
+ {{country}}
420
+ sk:
421
+ name: Slovakia
422
+ format: |-
423
+ {{recipient}}
424
+ {{street}}
425
+ {{zip}} {{city}}
426
+ {{country}}
427
+ sy:
428
+ name: Syrian Arab Republic
429
+ format: |-
430
+ {{recipient}}
431
+ {{street}}
432
+ {{zip}} {{city}}
433
+ {{country}}
434
+ th:
435
+ name: Thailand
436
+ format: |-
437
+ {{recipient}}
438
+ {{street}}
439
+ {{city}} {{state}} {{zip}}
440
+ {{country}}
441
+ tr:
442
+ name: Turkey
443
+ format: |-
444
+ {{recipient}}
445
+ {{street}}
446
+ {{zip}} {{city}}
447
+ {{country}}
448
+ tw:
449
+ name: Taiwan
450
+ format: |-
451
+ {{recipient}}
452
+ {{street}}
453
+ {{city}} {{state}} {{zip}}
454
+ {{country}}
455
+ ua:
456
+ name: Ukraine
457
+ format: |-
458
+ {{recipient}}
459
+ {{street}}
460
+ {{city}} {{state}}
461
+ {{zip}}
462
+ {{country}}
463
+ us:
464
+ name: United States
465
+ format: |-
466
+ {{recipient}}
467
+ {{street}}
468
+ {{city}} {{state}} {{zip}}
469
+ {{country}}
470
+ ye:
471
+ name: Yemen
472
+ format: |-
473
+ {{recipient}}
474
+ {{street}}
475
+ {{zip}} {{city}}
476
+ {{country}}
477
+ yu:
478
+ name: Serbia and Montenegro
479
+ format: |-
480
+ {{recipient}}
481
+ {{street}}
482
+ {{zip}} {{city}}
483
+
484
+ {{state}}
485
+ {{country}}
486
+ za:
487
+ name: South Africa
488
+ format: |-
489
+ {{recipient}}
490
+ {{street}}
491
+ {{city}}
492
+ {{state}}
493
+ {{zip}}
494
+ {{country}}
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'biggs'
data/lib/biggs.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'biggs/formatter'
2
+ require 'yaml'
3
+
4
+ module Biggs
5
+ class << self
6
+ def formats
7
+ @@formats ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'formats.yml')) || {}
8
+ end
9
+
10
+ def enable_activerecord
11
+ return if ActiveRecord::Base.respond_to? :biggs_formatter
12
+ require 'biggs/activerecord'
13
+ ActiveRecord::Base.send :include, Biggs::ActiveRecordAdapter
14
+ end
15
+ end
16
+ end
17
+
18
+ if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
19
+ Biggs.enable_activerecord
20
+ end
21
+
@@ -0,0 +1,75 @@
1
+ require 'active_support/core_ext/class/inheritable_attributes'
2
+
3
+ module Biggs
4
+ module ActiveRecordAdapter
5
+
6
+ def self.included(base)
7
+ base.extend(InitialClassMethods)
8
+ end
9
+
10
+ module InitialClassMethods
11
+ def biggs(method_name=nil, options={})
12
+ self.extend(ClassMethods)
13
+ self.send(:include, Biggs::ActiveRecordAdapter::InstanceMethods)
14
+ alias_method(method_name || :postal_address, :biggs_postal_address)
15
+
16
+ value_methods = {}
17
+ Biggs::Formatter::FIELDS.each do |field|
18
+ value_methods[field] = options.delete(field) if options[field]
19
+ end
20
+ write_inheritable_attribute(:biggs_value_methods, value_methods)
21
+ write_inheritable_attribute(:biggs_formatter, Biggs::Formatter.new(options))
22
+ end
23
+ end
24
+
25
+ module ClassMethods
26
+ def biggs_value_methods
27
+ read_inheritable_attribute(:biggs_value_methods) || write_inheritable_attribute(:biggs_value_methods, {})
28
+ end
29
+
30
+ def biggs_instance
31
+ read_inheritable_attribute(:biggs_formatter) || write_inheritable_attribute(:biggs_formatter, Biggs::Formatter.new)
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+
37
+ def biggs_postal_address
38
+ self.class.biggs_instance.format(biggs_country, biggs_values)
39
+ end
40
+
41
+ private
42
+
43
+ def biggs_country
44
+ biggs_get_value(:country)
45
+ end
46
+
47
+ def biggs_values
48
+ values = {}
49
+ (Biggs::Formatter::FIELDS - [:country]).each do |field|
50
+ values[field] = biggs_get_value(field)
51
+ end
52
+ values
53
+ end
54
+
55
+ def biggs_get_value(field)
56
+ key = self.class.biggs_value_methods[field.to_sym] || field.to_sym
57
+
58
+ case key
59
+ when Symbol
60
+ self.send(key.to_sym)
61
+ when Proc
62
+ key.call(self).to_s
63
+ when Array
64
+ if key.all?{|it| it.is_a?(Symbol) }
65
+ key.map{|method| self.send(method) }.reject(&:blank?).join("\n")
66
+ else
67
+ raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
68
+ end
69
+ else
70
+ raise "Biggs: Can't handle #{field} type #{key.class}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,42 @@
1
+ module Biggs
2
+ class Formatter
3
+
4
+ FIELDS = [:recipient, :street, :city, :state, :zip, :country]
5
+
6
+ def initialize(options={})
7
+ @blank_country_on = [options[:blank_country_on]].compact.flatten.map{|s| s.to_s.downcase}
8
+ end
9
+
10
+ def format(country_code, values={})
11
+ values.symbolize_keys! if values.respond_to?(:symbolize_keys!)
12
+ country_code = country_code.dup.to_s.downcase
13
+
14
+ country_entry = (Biggs.formats[country_code] || default_country_entry(country_code, values))
15
+ country_name = (country_entry["name"].dup || "").to_s
16
+ country_format = (country_entry["format"].dup || "").to_s
17
+
18
+ (FIELDS - [:country]).each do |key|
19
+ country_format.gsub!(/\{\{#{key}\}\}/, (values[key] || "").to_s)
20
+ end
21
+
22
+ country_name = "" if blank_country_on.include?(country_code)
23
+ country_format.gsub!(/\{\{country\}\}/, country_name)
24
+
25
+ country_format.gsub(/\n$/,"")
26
+ end
27
+
28
+ attr_accessor :blank_country_on, :default_country_without_state, :default_country_with_state
29
+
30
+ private
31
+
32
+ def default_country_entry(country_code, values={})
33
+ {
34
+ "name" => country_code.to_s,
35
+ "format" => (values[:state] && values[:state] != "" ?
36
+ Biggs.formats[default_country_with_state || "us"] :
37
+ Biggs.formats[default_country_without_state || "fr"])["format"]
38
+ }
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,3 @@
1
+ module Biggs
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rubygems'
3
+ require 'rspec'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'biggs')
5
+ require 'logger'
6
+
7
+ ActiveRecord::Base.logger = Logger.new('/tmp/biggs.log')
8
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/biggs.sqlite')
9
+ ActiveRecord::Migration.verbose = false
10
+
11
+ ActiveRecord::Schema.define do
12
+ create_table :base_tables, :force => true do |table|
13
+ table.string :name
14
+ end
15
+ end
@@ -0,0 +1,147 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
4
+
5
+ class BaseTable < ActiveRecord::Base
6
+ Biggs::Formatter::FIELDS.each do |field|
7
+ define_method(field) do
8
+ field == :country ? "us" : field.to_s.upcase
9
+ end
10
+ end
11
+ end
12
+
13
+ class FooBarEmpty < BaseTable;end
14
+
15
+ class FooBar < BaseTable
16
+ biggs :postal_address
17
+ end
18
+
19
+ class FooBarCustomFields < BaseTable
20
+ biggs :postal_address, :country => :my_custom_country_method,
21
+ :city => :my_custom_city_method
22
+
23
+ def my_custom_country_method
24
+ "de"
25
+ end
26
+
27
+ def my_custom_city_method
28
+ "Hamburg"
29
+ end
30
+ end
31
+
32
+ class FooBarCustomBlankDECountry < BaseTable
33
+ biggs :postal_address, :blank_country_on => "de"
34
+
35
+ def country
36
+ "DE"
37
+ end
38
+ end
39
+
40
+ class FooBarCustomMethod < BaseTable
41
+ biggs :my_postal_address_method
42
+ end
43
+
44
+ class FooBarCustomProc < BaseTable
45
+ biggs :postal_address,
46
+ :country => Proc.new {|it| it.method_accessed_from_proc + "XX"},
47
+ :state => Proc.new {|it| it.state.downcase }
48
+
49
+ def method_accessed_from_proc
50
+ "DE"
51
+ end
52
+ end
53
+
54
+ class FooBarCustomArray < BaseTable
55
+ biggs :postal_address,
56
+ :street => [:address_1, :address_empty, :address_nil, :address_2]
57
+
58
+ def address_1
59
+ "Address line 1"
60
+ end
61
+
62
+ def address_2
63
+ "Address line 2"
64
+ end
65
+
66
+ def address_empty
67
+ ""
68
+ end
69
+
70
+ def address_nil
71
+ nil
72
+ end
73
+ end
74
+
75
+ describe "ActiveRecord Class" do
76
+
77
+ it "should include Biggs::ActiveRecordAdapter" do
78
+ FooBar.included_modules.should be_include(Biggs::ActiveRecordAdapter)
79
+ end
80
+
81
+ it "should set class value biggs_value_methods" do
82
+ FooBar.class_eval("biggs_value_methods").should be_is_a(Hash)
83
+ FooBar.class_eval("biggs_value_methods").size.should be_zero
84
+ end
85
+
86
+ it "should set class value biggs_instance" do
87
+ FooBar.class_eval("biggs_instance").should be_is_a(Biggs::Formatter)
88
+ end
89
+
90
+ it "should respond to biggs" do
91
+ FooBar.should be_respond_to(:biggs)
92
+ end
93
+ end
94
+
95
+ describe "ActiveRecord Instance" do
96
+
97
+ describe "Empty" do
98
+ it "should not have postal_address method" do
99
+ FooBarEmpty.new.should_not be_respond_to(:postal_address)
100
+ end
101
+ end
102
+
103
+ describe "Standard" do
104
+ it "should have postal_address method" do
105
+ FooBar.new.should be_respond_to(:postal_address)
106
+ end
107
+
108
+ it "should return postal_address on postal_address" do
109
+ FooBar.new.postal_address.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nUnited States")
110
+ end
111
+ end
112
+
113
+ describe "Customized Fields" do
114
+ it "should return address from custom fields on postal_address" do
115
+ FooBarCustomFields.new.postal_address.should eql("RECIPIENT\nSTREET\nZIP Hamburg\nGermany")
116
+ end
117
+ end
118
+
119
+ describe "Customized Blank DE Country" do
120
+ it "should return address wo country on postal_address" do
121
+ FooBarCustomBlankDECountry.new.postal_address.should eql("RECIPIENT\nSTREET\nZIP CITY")
122
+ end
123
+ end
124
+
125
+ describe "Customized Method name" do
126
+ it "should have my_postal_address_method" do
127
+ FooBarCustomMethod.new.should be_respond_to(:my_postal_address_method)
128
+ end
129
+
130
+ it "should return formatted address on my_postal_address_method" do
131
+ FooBarCustomMethod.new.my_postal_address_method.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nUnited States")
132
+ end
133
+ end
134
+
135
+ describe "Customized Proc as Param" do
136
+ it "should return formatted address for unknown-country DEXX" do
137
+ FooBarCustomProc.new.postal_address.should eql("RECIPIENT\nSTREET\nCITY state ZIP\ndexx")
138
+ end
139
+ end
140
+
141
+ describe "Customized array of symbols" do
142
+ it "should return formatted address with two lines for street" do
143
+ FooBarCustomArray.new.postal_address.should eql("RECIPIENT\nAddress line 1\nAddress line 2\nCITY STATE ZIP\nUnited States")
144
+ end
145
+ end
146
+
147
+ end
@@ -0,0 +1,72 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ FAKE_ATTR_WITH_STATE = {:state => "STATE", :city => "CITY", :zip => 12345, :street => "STREET", :recipient => "MR. X"}
4
+ FAKE_ATTR_WO_STATE = {:city => "CITY", :zip => 12345, :street => "STREET", :recipient => "MR. X"}
5
+
6
+ describe Biggs::Formatter, "with defaults" do
7
+
8
+ before { @biggs = Biggs::Formatter.new }
9
+
10
+ it "should format to us format" do
11
+ @biggs.format('us', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\nCITY STATE 12345\nUnited States")
12
+ end
13
+
14
+ it "should format to de format" do
15
+ @biggs.format('de', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\n12345 CITY\nGermany")
16
+ end
17
+
18
+ it "should format to british format" do
19
+ @biggs.format('gb', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\nCITY\nSTATE\n12345\nUnited Kingdom")
20
+ end
21
+
22
+ it "should format to fr format" do
23
+ @biggs.format('fr', FAKE_ATTR_WO_STATE).should eql("MR. X\nSTREET\n12345 CITY\nFrance")
24
+ end
25
+
26
+ it "should format to fr format if country_code unknown and there is no STATE given" do
27
+ @biggs.format('unknown', FAKE_ATTR_WO_STATE).should eql("MR. X\nSTREET\n12345 CITY\nunknown")
28
+ end
29
+
30
+ it "should format to us format if country_code unknown and there is no STATE given" do
31
+ @biggs.format('unknown', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\nCITY STATE 12345\nunknown")
32
+ end
33
+
34
+ it "should format to no(rwegian) format" do
35
+ @biggs.format('no', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\n12345 CITY\nNorway")
36
+ end
37
+
38
+ it "should format to NC format" do
39
+ @biggs.format('nc', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\n12345 CITY\nNew Caledonia")
40
+ end
41
+
42
+ end
43
+
44
+ describe Biggs, "with options" do
45
+
46
+ describe "blank_country_on de" do
47
+ before { @biggs = Biggs::Formatter.new(:blank_country_on => 'de') }
48
+
49
+ it "should have blank country in 'de' address" do
50
+ @biggs.format('de', FAKE_ATTR_WO_STATE).should eql("MR. X\nSTREET\n12345 CITY")
51
+ end
52
+
53
+ it "should have country in 'fr' address" do
54
+ @biggs.format('fr', FAKE_ATTR_WO_STATE).should eql("MR. X\nSTREET\n12345 CITY\nFrance")
55
+ end
56
+
57
+ end
58
+
59
+ describe "blank_country_on multiple (US,de)" do
60
+ before { @biggs = Biggs::Formatter.new(:blank_country_on => ['US', "de"]) }
61
+
62
+ it "should have blank country in 'us' address" do
63
+ @biggs.format('us', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\nCITY STATE 12345")
64
+ end
65
+
66
+ it "should have country in 'fr' address" do
67
+ @biggs.format('fr', FAKE_ATTR_WITH_STATE).should eql("MR. X\nSTREET\n12345 CITY\nFrance")
68
+ end
69
+
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: biggs
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ segments_generated: true
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Sebastian Munz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-12 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ segments_generated: true
32
+ version: "3.0"
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ segments_generated: true
46
+ version: "0"
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 2
59
+ - 4
60
+ - 0
61
+ segments_generated: true
62
+ version: 2.4.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: sqlite3-ruby
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ segments_generated: true
76
+ version: "0"
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ description: biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
81
+ email:
82
+ - sebastian@yo.lk
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ files:
90
+ - .gitignore
91
+ - .rvmrc
92
+ - CHANGES.textile
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.textile
97
+ - Rakefile
98
+ - biggs.gemspec
99
+ - formats.yml
100
+ - init.rb
101
+ - lib/biggs.rb
102
+ - lib/biggs/activerecord.rb
103
+ - lib/biggs/formatter.rb
104
+ - lib/biggs/version.rb
105
+ - spec/spec_helper.rb
106
+ - spec/unit/activerecord_spec.rb
107
+ - spec/unit/biggs_spec.rb
108
+ has_rdoc: true
109
+ homepage: https://github.com/yolk/biggs
110
+ licenses: []
111
+
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: -2951630295030723948
123
+ segments:
124
+ - 0
125
+ segments_generated: true
126
+ version: "0"
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: -2951630295030723948
133
+ segments:
134
+ - 0
135
+ segments_generated: true
136
+ version: "0"
137
+ requirements: []
138
+
139
+ rubyforge_project: biggs
140
+ rubygems_version: 1.3.7
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ - spec/unit/activerecord_spec.rb
147
+ - spec/unit/biggs_spec.rb