kid80-biggs 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.textile ADDED
@@ -0,0 +1,27 @@
1
+ h3. biggs 0.1.5 - 2009-07-03
2
+
3
+ * Removed recipient from format
4
+ * Added alias for biggs -> formatted_address, to be clearer what the method does.
5
+
6
+ h3. biggs 0.1.4 - 2009-5-21
7
+
8
+ * Fixed Specs
9
+
10
+ h3. biggs 0.1.3 - 2009-3-6
11
+
12
+ * Correct japanese address format. [hiroshi]
13
+ * Fixed docs for current API. [hiroshi]
14
+
15
+ h3. biggs 0.1.2 - 2009-3-4
16
+
17
+ * Values can now be specified by an array of symbols.
18
+
19
+ h3. biggs 0.1.1 - 2009-3-3
20
+
21
+ * Refactored activerecord-adapter to include only basic setup method in ActiveRecord:Base
22
+
23
+ h3. biggs 0.1.0 - 2009-3-3
24
+
25
+ * Allow Procs as params in biggs-activerecord-setup.
26
+ * Cleanup
27
+
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,147 @@
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
+ * Luxembourg
114
+ * Macedonia
115
+ * Mexico
116
+ * Netherlands
117
+ * New Zealand
118
+ * Norway
119
+ * Oman
120
+ * Philippines
121
+ * Poland
122
+ * Portugal
123
+ * Qatar
124
+ * Romania
125
+ * Russian Federation
126
+ * Saudi Arabia
127
+ * Serbia and Montenegro
128
+ * Singapore
129
+ * Slovakia
130
+ * Slovenia
131
+ * South Africa
132
+ * South Korea
133
+ * Spain
134
+ * Sweden
135
+ * Switzerland
136
+ * Syrian Arab Republic
137
+ * Taiwan
138
+ * Turkey
139
+ * Ukraine
140
+ * United Arab Emirates
141
+ * United Kingdom
142
+ * United States
143
+ * Yemen
144
+
145
+ biggs is tested to behave well with Rails 2.2.2.
146
+
147
+ Copyright (c) 2009 Yolk Sebastian Munz & Julia Soergel GbR
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ task :default => [:spec]
2
+
3
+ $gem_name = "biggs"
4
+
5
+ desc "Run specs"
6
+ task :spec do
7
+ sh "spec spec/* --format specdoc --color"
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |s|
13
+ s.name = $gem_name
14
+ s.summary = "biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries."
15
+ s.email = "sebastian@yo.lk"
16
+ s.homepage = "http://github.com/yolk/biggs"
17
+ s.description = s.summary
18
+ s.authors = ["Sebastian Munz"]
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 5
4
+ :major: 0
data/formats.yml ADDED
@@ -0,0 +1,410 @@
1
+ # Avaible placeholders:
2
+ # * street
3
+ # * zip
4
+ # * city
5
+ # * state
6
+ # * country
7
+ #
8
+ ---
9
+ ae:
10
+ name: United Arab Emirates
11
+ format: |-
12
+ {{street}}
13
+ {{postal_code}} {{locality}}
14
+ {{country}}
15
+ ar:
16
+ name: Argentina
17
+ format: |-
18
+ {{street}}
19
+ {{postal_code}} {{locality}}
20
+ {{region}}
21
+ {{country}}
22
+ at:
23
+ name: Austria
24
+ format: |-
25
+ {{street}}
26
+ {{postal_code}} {{locality}}
27
+ {{country}}
28
+ au:
29
+ name: Australia
30
+ format: |-
31
+ {{street}}
32
+ {{locality}} {{region}} {{postal_code}}
33
+ {{country}}
34
+ ba:
35
+ name: Bosnia and Herzegovina
36
+ format: |-
37
+ {{street}}
38
+ {{postal_code}} {{locality}}
39
+ {{country}}
40
+ be:
41
+ name: Belgium
42
+ format: |-
43
+ {{street}}
44
+ {{postal_code}} {{locality}}
45
+ {{country}}
46
+ bg:
47
+ name: Bulgaria
48
+ format: |-
49
+ {{street}}
50
+ {{postal_code}} {{locality}}
51
+ {{country}}
52
+ bh:
53
+ name: Bahrain
54
+ format: |-
55
+ {{street}}
56
+ {{postal_code}} {{locality}}
57
+ {{country}}
58
+ br:
59
+ name: Brazil
60
+ format: |-
61
+ {{street}}
62
+ {{postal_code}} {{locality}} {{region}}
63
+ {{country}}
64
+ ca:
65
+ name: Canada
66
+ format: |-
67
+ {{street}}
68
+ {{locality}} {{region}} {{postal_code}}
69
+ {{country}}
70
+ ch:
71
+ name: Switzerland
72
+ format: |-
73
+ {{street}}
74
+ {{postal_code}} {{locality}}
75
+ {{country}}
76
+ cn:
77
+ name: China
78
+ format: |-
79
+ {{street}}
80
+ {{postal_code}} {{locality}} {{region}}
81
+ {{country}}
82
+ cz:
83
+ name: Czech
84
+ format: |-
85
+ {{street}}
86
+ {{postal_code}} {{locality}}
87
+ {{country}}
88
+ de:
89
+ name: Germany
90
+ format: |-
91
+ {{street}}
92
+ {{postal_code}} {{locality}}
93
+ {{country}}
94
+ dk:
95
+ name: Denmark
96
+ format: |-
97
+ {{street}}
98
+ {{postal_code}} {{locality}}
99
+ {{region}}
100
+ {{country}}
101
+ eg:
102
+ name: Egypt
103
+ format: |-
104
+ {{street}}
105
+ {{postal_code}} {{locality}}
106
+ {{country}}
107
+ es:
108
+ name: Spain
109
+ format: |-
110
+ {{street}}
111
+ {{postal_code}} {{locality}} {{region}}
112
+ {{country}}
113
+ fi:
114
+ name: Finland
115
+ format: |-
116
+ {{street}}
117
+ {{postal_code}} {{locality}}
118
+ {{country}}
119
+ fr:
120
+ name: France
121
+ format: |-
122
+ {{street}}
123
+ {{postal_code}} {{locality}}
124
+ {{country}}
125
+ gb:
126
+ name: United Kingdom
127
+ format: |-
128
+ {{street}}
129
+ {{locality}}
130
+ {{region}}
131
+ {{postal_code}}
132
+ {{country}}
133
+ gl:
134
+ name: Greenland
135
+ format: |-
136
+ {{street}}
137
+ {{postal_code}} {{locality}}
138
+ {{country}}
139
+ gr:
140
+ name: Greece
141
+ format: |-
142
+ {{street}}
143
+ {{postal_code}} {{locality}}
144
+ {{country}}
145
+ hk:
146
+ name: Hong Kong
147
+ format: |-
148
+ {{street}}
149
+ {{postal_code}} {{locality}} {{region}}
150
+ {{country}}
151
+ hr:
152
+ name: Croatia
153
+ format: |-
154
+ {{street}}
155
+ {{postal_code}} {{locality}}
156
+ {{country}}
157
+ hu:
158
+ name: Hungary
159
+ format: |-
160
+ {{locality}}
161
+ {{street}}
162
+ {{postal_code}}
163
+ {{country}}
164
+ id:
165
+ name: Indonesia
166
+ format: |-
167
+ {{street}}
168
+ {{locality}}
169
+ {{region}} {{postal_code}}
170
+ {{country}}
171
+ ie:
172
+ name: Ireland
173
+ format: |-
174
+ {{street}}
175
+ {{locality}} {{region}} {{postal_code}}
176
+ {{country}}
177
+ il:
178
+ name: Israel
179
+ format: |-
180
+ {{street}}
181
+ {{postal_code}} {{locality}}
182
+ {{country}}
183
+ in:
184
+ name: India
185
+ format: |-
186
+ {{street}}
187
+ {{region}}
188
+ {{locality}} {{postal_code}}
189
+ {{country}}
190
+ is:
191
+ name: Iceland
192
+ format: |-
193
+ {{street}}
194
+ {{postal_code}} {{locality}}
195
+ {{country}}
196
+ it:
197
+ name: Italy
198
+ format: |-
199
+ {{street}}
200
+ {{postal_code}} {{locality}} {{region}}
201
+ {{country}}
202
+ jo:
203
+ name: Jordan
204
+ format: |-
205
+ {{street}}
206
+ {{postal_code}} {{locality}}
207
+ {{country}}
208
+ # Here is a sample of Japanese formatted addresses (Apple Japan HQ).
209
+ # Usually they have "Postal mark" before zip (postal) code,
210
+ # and parts of an address are not separated with space.
211
+ # Biggs::Formatter.new(:blank_country_on => "jp").format(
212
+ # "jp",
213
+ # :recipient => "アップルジャパン株式会社 本社",
214
+ # :street => "西新宿3-20-2",
215
+ # :city => "新宿区",
216
+ # :state => "東京都",
217
+ # :zip => "163-1480")
218
+ # 〒163-1480
219
+ # 東京都新宿区西新宿3-20-2
220
+ # アップルジャパン株式会社 本社
221
+ jp:
222
+ name: Japan
223
+ format: |-
224
+ 〒{{postal_code}}
225
+ {{region}}{{locality}}{{street}}
226
+ {{country}}
227
+ kr:
228
+ name: South Korea
229
+ format: |-
230
+ {{street}}
231
+ {{locality}} {{region}}
232
+ {{postal_code}}
233
+ {{country}}
234
+ kw:
235
+ name: Kuwait
236
+ format: |-
237
+ {{street}}
238
+ {{postal_code}} {{locality}}
239
+ {{region}}
240
+ {{country}}
241
+ lb:
242
+ name: Lebanon
243
+ format: |-
244
+ {{street}}
245
+ {{postal_code}} {{locality}}
246
+ {{country}}
247
+ lu:
248
+ name: Luxembourg
249
+ format: |-
250
+ {{street}}
251
+ {{postal_code}} {{locality}}
252
+ {{country}}
253
+ mk:
254
+ name: Macedonia
255
+ format: |-
256
+ {{street}}
257
+ {{locality}} {{postal_code}}
258
+ {{country}}
259
+ mx:
260
+ name: Mexico
261
+ format: |-
262
+ {{street}}
263
+ {{postal_code}} {{locality}} {{region}}
264
+ {{country}}
265
+ nl:
266
+ name: Netherlands
267
+ format: |-
268
+ {{street}}
269
+ {{postal_code}} {{locality}}
270
+ {{country}}
271
+ no:
272
+ name: Norway
273
+ format: |-
274
+ {{street}}
275
+ {{postal_code}} {{locality}}
276
+ {{country}}
277
+ nz:
278
+ name: New Zealand
279
+ format: |-
280
+ {{street}}
281
+ {{region}}
282
+ {{locality}} {{postal_code}}
283
+ {{country}}
284
+ om:
285
+ name: Oman
286
+ format: |-
287
+ {{street}}
288
+ {{postal_code}} {{locality}}
289
+ {{region}}
290
+ {{country}}
291
+ ph:
292
+ name: Philippines
293
+ format: |-
294
+ {{street}} {{region}}
295
+ {{postal_code}} {{locality}}
296
+ {{country}}
297
+ pl:
298
+ name: Poland
299
+ format: |-
300
+ {{street}}
301
+ {{postal_code}} {{locality}}
302
+ {{region}}
303
+ {{country}}
304
+ pt:
305
+ name: Portugal
306
+ format: |-
307
+ {{street}}
308
+ {{postal_code}} {{locality}} {{region}}
309
+ {{country}}
310
+ qa:
311
+ name: Qatar
312
+ format: |-
313
+ {{street}}
314
+ {{postal_code}} {{locality}}
315
+ {{country}}
316
+ ro:
317
+ name: Romania
318
+ format: |-
319
+ {{street}}
320
+ {{postal_code}} {{locality}}
321
+ {{country}}
322
+ ru:
323
+ name: Russian Federation
324
+ format: |-
325
+ {{postal_code}} {{locality}}
326
+ {{street}}
327
+ {{country}}
328
+ sa:
329
+ name: Saudi Arabia
330
+ format: |-
331
+ {{street}}
332
+ {{postal_code}} {{locality}}
333
+ {{country}}
334
+ se:
335
+ name: Sweden
336
+ format: |-
337
+ {{street}}
338
+ {{postal_code}} {{locality}}
339
+ {{country}}
340
+ sg:
341
+ name: Singapore
342
+ format: |-
343
+ {{street}}
344
+ {{locality}} {{postal_code}}
345
+ {{country}}
346
+ si:
347
+ name: Slovenia
348
+ format: |-
349
+ {{street}}
350
+ {{postal_code}} {{locality}}
351
+ {{country}}
352
+ sk:
353
+ name: Slovakia
354
+ format: |-
355
+ {{street}}
356
+ {{postal_code}} {{locality}}
357
+ {{country}}
358
+ sy:
359
+ name: Syrian Arab Republic
360
+ format: |-
361
+ {{street}}
362
+ {{postal_code}} {{locality}}
363
+ {{country}}
364
+ tr:
365
+ name: Turkey
366
+ format: |-
367
+ {{street}}
368
+ {{postal_code}} {{locality}}
369
+ {{country}}
370
+ tw:
371
+ name: Taiwan
372
+ format: |-
373
+ {{street}}
374
+ {{locality}} {{region}} {{postal_code}}
375
+ {{country}}
376
+ ua:
377
+ name: Ukraine
378
+ format: |-
379
+ {{street}}
380
+ {{locality}} {{region}}
381
+ {{postal_code}}
382
+ {{country}}
383
+ us:
384
+ name: United States
385
+ format: |-
386
+ {{street}}
387
+ {{locality}} {{region}} {{postal_code}}
388
+ {{country}}
389
+ ye:
390
+ name: Yemen
391
+ format: |-
392
+ {{street}}
393
+ {{postal_code}} {{locality}}
394
+ {{country}}
395
+ yu:
396
+ name: Serbia and Montenegro
397
+ format: |-
398
+ {{street}}
399
+ {{postal_code}} {{locality}}
400
+
401
+ {{region}}
402
+ {{country}}
403
+ za:
404
+ name: South Africa
405
+ format: |-
406
+ {{street}}
407
+ {{locality}}
408
+ {{region}}
409
+ {{postal_code}}
410
+ {{country}}
@@ -0,0 +1,79 @@
1
+ module Biggs
2
+ module ActiveRecordAdapter
3
+
4
+ def self.included(base)
5
+ base.extend(InitialClassMethods)
6
+ end
7
+
8
+ module InitialClassMethods
9
+ def biggs(method_name=nil, options={})
10
+ self.extend(ClassMethods)
11
+ self.send(:include, Biggs::ActiveRecordAdapter::InstanceMethods)
12
+ alias_method(method_name || :postal_address, :biggs_postal_address)
13
+
14
+ value_methods = {}
15
+ Biggs::Formatter::FIELDS.each do |field|
16
+ value_methods[field] = options.delete(field) if options[field]
17
+ end
18
+ write_inheritable_attribute(:biggs_value_methods, value_methods)
19
+ write_inheritable_attribute(:biggs_formatter, Biggs::Formatter.new(options))
20
+ end
21
+
22
+ alias_method :formatted_address, :biggs
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
+ def formatted_field_names
42
+ self.class.biggs_instance.fields_for_format(biggs_country, biggs_values)
43
+ end
44
+
45
+ private
46
+
47
+ def biggs_country
48
+ biggs_get_value(:country)
49
+ end
50
+
51
+ def biggs_values
52
+ values = {}
53
+ (Biggs::Formatter::FIELDS - [:country]).each do |field|
54
+ values[field] = biggs_get_value(field)
55
+ end
56
+ values
57
+ end
58
+
59
+ def biggs_get_value(field)
60
+ key = self.class.biggs_value_methods[field.to_sym] || field.to_sym
61
+
62
+ case key
63
+ when Symbol
64
+ self.send(key.to_sym)
65
+ when Proc
66
+ key.call(self).to_s
67
+ when Array
68
+ if key.all?{|it| it.is_a?(Symbol) }
69
+ key.map{|method| self.send(method) }.reject(&:blank?).join("\n")
70
+ else
71
+ raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
72
+ end
73
+ else
74
+ raise "Biggs: Can't handle #{field} type #{key.class}"
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,52 @@
1
+ module Biggs
2
+ class Formatter
3
+
4
+ FIELDS = [:street, :locality, :region, :postal_code, :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
+ def fields_for_format(country_code, values={})
29
+ values.symbolize_keys! if values.respond_to?(:symbolize_keys!)
30
+ country_code = country_code.dup.to_s.downcase
31
+ country_entry = (Biggs.formats[country_code])
32
+ country_format = (country_entry["format"].dup || "").to_s
33
+
34
+ form_fields = country_format.scan(/\{\{(\w+)\}\}/)
35
+
36
+ end
37
+
38
+ attr_accessor :blank_country_on, :default_country_without_state, :default_country_with_state
39
+
40
+ private
41
+
42
+ def default_country_entry(country_code, values={})
43
+ {
44
+ "name" => country_code.to_s,
45
+ "format" => (values[:region] && values[:region] != "" ?
46
+ Biggs.formats[default_country_with_state || "us"] :
47
+ Biggs.formats[default_country_without_state || "fr"])["format"]
48
+ }
49
+ end
50
+ end
51
+
52
+ end
data/lib/biggs.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'biggs/formatter'
2
+
3
+ module Biggs
4
+ class << self
5
+ def formats
6
+ @@formats ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'formats.yml')) || {}
7
+ end
8
+
9
+ def enable_activerecord
10
+ return if ActiveRecord::Base.respond_to? :biggs_formatter
11
+ require 'biggs/activerecord'
12
+ ActiveRecord::Base.send :include, Biggs::ActiveRecordAdapter
13
+ end
14
+ end
15
+ end
16
+
17
+ if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
18
+ Biggs.enable_activerecord
19
+ end
20
+
@@ -0,0 +1,4 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'biggs')
@@ -0,0 +1,201 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
4
+
5
+ # A dummy class for mocking the activerecord connection class
6
+ class Connection;end
7
+
8
+ # StandardMethods for Address
9
+ module BiggsStandardMethods
10
+ Biggs::Formatter::FIELDS.each do |field|
11
+ define_method(field) do
12
+ field == :country ? "us" : field.to_s.upcase
13
+ end
14
+ end
15
+ end
16
+
17
+ class FooBarEmpty < ActiveRecord::Base
18
+ include BiggsStandardMethods
19
+ end
20
+
21
+ class FooBar < ActiveRecord::Base
22
+ include BiggsStandardMethods
23
+
24
+ biggs :postal_address
25
+ end
26
+
27
+ class FooBarCustomFields < ActiveRecord::Base
28
+ include BiggsStandardMethods
29
+
30
+ biggs :postal_address, :country => :my_custom_country_method,
31
+ :city => :my_custom_city_method
32
+
33
+ def my_custom_country_method
34
+ "de"
35
+ end
36
+
37
+ def my_custom_city_method
38
+ "Hamburg"
39
+ end
40
+ end
41
+
42
+ class FooBarCustomBlankDECountry < ActiveRecord::Base
43
+ include BiggsStandardMethods
44
+
45
+ biggs :postal_address, :blank_country_on => "de"
46
+
47
+ def country
48
+ "DE"
49
+ end
50
+ end
51
+
52
+ class FooBarCustomMethod < ActiveRecord::Base
53
+ include BiggsStandardMethods
54
+
55
+ biggs :my_postal_address_method
56
+ end
57
+
58
+ class FooBarCustomProc < ActiveRecord::Base
59
+ include BiggsStandardMethods
60
+
61
+ biggs :postal_address,
62
+ :country => Proc.new {|it| it.method_accessed_from_proc + "XX"},
63
+ :state => Proc.new {|it| it.state.downcase }
64
+
65
+ def method_accessed_from_proc
66
+ "DE"
67
+ end
68
+ end
69
+
70
+ class FooBarCustomArray < ActiveRecord::Base
71
+ include BiggsStandardMethods
72
+
73
+ biggs :postal_address,
74
+ :street => [:address_1, :address_empty, :address_nil, :address_2]
75
+
76
+ def address_1
77
+ "Address line 1"
78
+ end
79
+
80
+ def address_2
81
+ "Address line 2"
82
+ end
83
+
84
+ def address_empty
85
+ ""
86
+ end
87
+
88
+ def address_nil
89
+ nil
90
+ end
91
+ end
92
+
93
+
94
+ describe "ActiveRecord Class" do
95
+
96
+ it "should include Biggs::ActiveRecordAdapter" do
97
+ FooBar.included_modules.should be_include(Biggs::ActiveRecordAdapter)
98
+ end
99
+
100
+ it "should set class value biggs_value_methods" do
101
+ FooBar.class_eval("biggs_value_methods").should be_is_a(Hash)
102
+ FooBar.class_eval("biggs_value_methods").size.should be_zero
103
+ end
104
+
105
+ it "should set class value biggs_instance" do
106
+ FooBar.class_eval("biggs_instance").should be_is_a(Biggs::Formatter)
107
+ end
108
+
109
+ it "should respond to biggs" do
110
+ FooBar.should be_respond_to(:biggs)
111
+ end
112
+ end
113
+
114
+ describe "ActiveRecord Instance" do
115
+
116
+ describe "Empty" do
117
+ before(:each) do
118
+ connection = mock(Connection, :columns => [])
119
+ FooBarEmpty.stub!(:connection).and_return(connection)
120
+ end
121
+
122
+ it "should not have postal_address method" do
123
+ FooBarEmpty.new.should_not be_respond_to(:postal_address)
124
+ end
125
+ end
126
+
127
+ describe "Standard" do
128
+ before(:each) do
129
+ connection = mock(Connection, :columns => [])
130
+ FooBar.stub!(:connection).and_return(connection)
131
+ end
132
+
133
+ it "should have postal_address method" do
134
+ FooBar.new.should be_respond_to(:postal_address)
135
+ end
136
+
137
+ it "should return postal_address on postal_address" do
138
+ FooBar.new.postal_address.should eql("STREET\nCITY STATE ZIP\nUnited States")
139
+ end
140
+ end
141
+
142
+ describe "Customized Fields" do
143
+ before(:each) do
144
+ connection = mock(Connection, :columns => [])
145
+ FooBarCustomFields.stub!(:connection).and_return(connection)
146
+ end
147
+
148
+ it "should return address from custom fields on postal_address" do
149
+ FooBarCustomFields.new.postal_address.should eql("STREET\nZIP Hamburg\nGermany")
150
+ end
151
+ end
152
+
153
+ describe "Customized Blank DE Country" do
154
+ before(:each) do
155
+ connection = mock(Connection, :columns => [])
156
+ FooBarCustomBlankDECountry.stub!(:connection).and_return(connection)
157
+ end
158
+
159
+ it "should return address wo country on postal_address" do
160
+ FooBarCustomBlankDECountry.new.postal_address.should eql("STREET\nZIP CITY")
161
+ end
162
+ end
163
+
164
+ describe "Customized Method name" do
165
+ before(:each) do
166
+ connection = mock(Connection, :columns => [])
167
+ FooBarCustomMethod.stub!(:connection).and_return(connection)
168
+ end
169
+
170
+ it "should have my_postal_address_method" do
171
+ FooBarCustomMethod.new.should be_respond_to(:my_postal_address_method)
172
+ end
173
+
174
+ it "should return formatted address on my_postal_address_method" do
175
+ FooBarCustomMethod.new.my_postal_address_method.should eql("STREET\nCITY STATE ZIP\nUnited States")
176
+ end
177
+ end
178
+
179
+ describe "Customized Proc as Param" do
180
+ before(:each) do
181
+ connection = mock(Connection, :columns => [])
182
+ FooBarCustomProc.stub!(:connection).and_return(connection)
183
+ end
184
+
185
+ it "should return formatted address for unknown-country DEXX" do
186
+ FooBarCustomProc.new.postal_address.should eql("STREET\nCITY state ZIP\ndexx")
187
+ end
188
+ end
189
+
190
+ describe "Customized array of symbols" do
191
+ before(:each) do
192
+ connection = mock(Connection, :columns => [])
193
+ FooBarCustomArray.stub!(:connection).and_return(connection)
194
+ end
195
+
196
+ it "should return formatted address with two lines for street" do
197
+ FooBarCustomArray.new.postal_address.should eql("Address line 1\nAddress line 2\nCITY STATE ZIP\nUnited States")
198
+ end
199
+ end
200
+
201
+ end
@@ -0,0 +1,64 @@
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("STREET\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("STREET\n12345 CITY\nGermany")
16
+ end
17
+
18
+ it "should format to british format" do
19
+ @biggs.format('gb', FAKE_ATTR_WITH_STATE).should eql("STREET\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("STREET\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("STREET\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("STREET\nCITY STATE 12345\nunknown")
32
+ end
33
+
34
+ end
35
+
36
+ describe Biggs, "with options" do
37
+
38
+ describe "blank_country_on de" do
39
+ before { @biggs = Biggs::Formatter.new(:blank_country_on => 'de') }
40
+
41
+ it "should have blank country in 'de' address" do
42
+ @biggs.format('de', FAKE_ATTR_WO_STATE).should eql("STREET\n12345 CITY")
43
+ end
44
+
45
+ it "should have country in 'fr' address" do
46
+ @biggs.format('fr', FAKE_ATTR_WO_STATE).should eql("STREET\n12345 CITY\nFrance")
47
+ end
48
+
49
+ end
50
+
51
+ describe "blank_country_on multiple (US,de)" do
52
+ before { @biggs = Biggs::Formatter.new(:blank_country_on => ['US', "de"]) }
53
+
54
+ it "should have blank country in 'us' address" do
55
+ @biggs.format('us', FAKE_ATTR_WITH_STATE).should eql("STREET\nCITY STATE 12345")
56
+ end
57
+
58
+ it "should have country in 'fr' address" do
59
+ @biggs.format('fr', FAKE_ATTR_WITH_STATE).should eql("STREET\n12345 CITY\nFrance")
60
+ end
61
+
62
+ end
63
+
64
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kid80-biggs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Munz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
17
+ email: sebastian@yo.lk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ files:
26
+ - CHANGES.textile
27
+ - formats.yml
28
+ - LICENSE
29
+ - README.textile
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - lib/biggs.rb
33
+ - lib/biggs/activerecord.rb
34
+ - lib/biggs/formatter.rb
35
+ - spec/spec_helper.rb
36
+ - spec/unit/activerecord_spec.rb
37
+ - spec/unit/biggs_spec.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/yolk/biggs
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
64
+ test_files:
65
+ - spec/spec_helper.rb
66
+ - spec/unit/activerecord_spec.rb
67
+ - spec/unit/biggs_spec.rb