yolk-biggs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +139 -0
- data/VERSION.yml +4 -0
- data/lib/biggs/activerecord.rb +64 -0
- data/lib/biggs/formatter.rb +42 -0
- data/lib/biggs.rb +20 -0
- metadata +59 -0
data/README.textile
ADDED
@@ -0,0 +1,139 @@
|
|
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(
|
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
|
+
:country => "de" # <= ISO alpha 2 code
|
24
|
+
)
|
25
|
+
|
26
|
+
returns
|
27
|
+
|
28
|
+
"Yolk Sebastian Munz & Julia Soergel GbR
|
29
|
+
Adalbertstr. 11
|
30
|
+
10999 Berlin
|
31
|
+
Germany"
|
32
|
+
|
33
|
+
At the moment Biggs::Formatter.new accepts only one option:
|
34
|
+
|
35
|
+
*blank_county_on* ISO alpha 2 code (single string or array) of countries the formatter should skip the line "country" (for national shipping).
|
36
|
+
|
37
|
+
f = Biggs::Formatter.new(:blank_county_on => "de")
|
38
|
+
|
39
|
+
With the data from the above example this would return:
|
40
|
+
|
41
|
+
"Yolk Sebastian Munz & Julia Soergel GbR
|
42
|
+
Adalbertstr. 11
|
43
|
+
10999 Berlin"
|
44
|
+
|
45
|
+
h3. Usage with Rails and ActiveRecord
|
46
|
+
|
47
|
+
Address < ActiveRecord::Base
|
48
|
+
biggs :postal_address
|
49
|
+
end
|
50
|
+
|
51
|
+
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').
|
52
|
+
|
53
|
+
You can customize the method-names biggs will use by passing in a hash of options:
|
54
|
+
|
55
|
+
Address < ActiveRecord::Base
|
56
|
+
biggs :postal_address,
|
57
|
+
:zip => :postal_code,
|
58
|
+
:country => :country_code,
|
59
|
+
:street => Proc.new {|address| "#{address.street} #{address.house_number}" }
|
60
|
+
end
|
61
|
+
|
62
|
+
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.
|
63
|
+
|
64
|
+
To access the formatted address string, simply call the provided method on an address instance:
|
65
|
+
|
66
|
+
Address.find(1).postal_address
|
67
|
+
|
68
|
+
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.
|
69
|
+
|
70
|
+
h3. Supported countries
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
* Argentina
|
75
|
+
* Australia
|
76
|
+
* Austria
|
77
|
+
* Bahrain
|
78
|
+
* Belgium
|
79
|
+
* Bosnia and Herzegovina
|
80
|
+
* Brazil
|
81
|
+
* Bulgaria
|
82
|
+
* Canada
|
83
|
+
* China
|
84
|
+
* Croatia
|
85
|
+
* Czech
|
86
|
+
* Denmark
|
87
|
+
* Egypt
|
88
|
+
* Finland
|
89
|
+
* France
|
90
|
+
* Germany
|
91
|
+
* Greece
|
92
|
+
* Greenland
|
93
|
+
* Hong Kong
|
94
|
+
* Hungary
|
95
|
+
* Iceland
|
96
|
+
* India
|
97
|
+
* Indonesia
|
98
|
+
* Ireland
|
99
|
+
* Israel
|
100
|
+
* Italy
|
101
|
+
* Japan
|
102
|
+
* Jordan
|
103
|
+
* Kuwait
|
104
|
+
* Lebanon
|
105
|
+
* Luxembourg
|
106
|
+
* Macedonia
|
107
|
+
* Mexico
|
108
|
+
* Netherlands
|
109
|
+
* New Zealand
|
110
|
+
* Norway
|
111
|
+
* Oman
|
112
|
+
* Philippines
|
113
|
+
* Poland
|
114
|
+
* Portugal
|
115
|
+
* Qatar
|
116
|
+
* Romania
|
117
|
+
* Russian Federation
|
118
|
+
* Saudi Arabia
|
119
|
+
* Serbia and Montenegro
|
120
|
+
* Singapore
|
121
|
+
* Slovakia
|
122
|
+
* Slovenia
|
123
|
+
* South Africa
|
124
|
+
* South Korea
|
125
|
+
* Spain
|
126
|
+
* Sweden
|
127
|
+
* Switzerland
|
128
|
+
* Syrian Arab Republic
|
129
|
+
* Taiwan
|
130
|
+
* Turkey
|
131
|
+
* Ukraine
|
132
|
+
* United Arab Emirates
|
133
|
+
* United Kingdom
|
134
|
+
* United States
|
135
|
+
* Yemen
|
136
|
+
|
137
|
+
biggs is tested to behave well with Rails 2.2.2.
|
138
|
+
|
139
|
+
Copyright (c) 2009 Yolk Sebastian Munz & Julia Soergel GbR
|
data/VERSION.yml
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Biggs
|
2
|
+
module ActiveRecordAdapter
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def biggs_value_methods
|
10
|
+
read_inheritable_attribute(:biggs_value_methods) || write_inheritable_attribute(:biggs_value_methods, {})
|
11
|
+
end
|
12
|
+
|
13
|
+
def biggs_instance
|
14
|
+
read_inheritable_attribute(:biggs_formatter) || write_inheritable_attribute(:biggs_formatter, Biggs::Formatter.new)
|
15
|
+
end
|
16
|
+
|
17
|
+
def biggs(method_name=nil, options={})
|
18
|
+
self.send(:include, Biggs::ActiveRecordAdapter::InstanceMethods)
|
19
|
+
alias_method(method_name || :postal_address, :biggs_postal_address)
|
20
|
+
|
21
|
+
value_methods = {}
|
22
|
+
Biggs::Formatter::FIELDS.each do |field|
|
23
|
+
value_methods[field] = options.delete(field) if options[field]
|
24
|
+
end
|
25
|
+
write_inheritable_attribute(:biggs_value_methods, value_methods)
|
26
|
+
write_inheritable_attribute(:biggs_formatter, Biggs::Formatter.new(options))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module InstanceMethods
|
31
|
+
|
32
|
+
def biggs_postal_address
|
33
|
+
self.class.biggs_instance.format(biggs_country, biggs_values)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def biggs_country
|
39
|
+
biggs_get_value(:country)
|
40
|
+
end
|
41
|
+
|
42
|
+
def biggs_values
|
43
|
+
values = {}
|
44
|
+
(Biggs::Formatter::FIELDS - [:country]).each do |field|
|
45
|
+
values[field] = biggs_get_value(field)
|
46
|
+
end
|
47
|
+
values
|
48
|
+
end
|
49
|
+
|
50
|
+
def biggs_get_value(field)
|
51
|
+
key = self.class.biggs_value_methods[field.to_sym] || field.to_sym
|
52
|
+
|
53
|
+
case key
|
54
|
+
when Symbol, String
|
55
|
+
self.send(key.to_sym)
|
56
|
+
when Proc
|
57
|
+
key.call(self).to_s
|
58
|
+
else
|
59
|
+
raise "Biggs: Can't handle #{field} type #{key.class}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
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
|
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
|
+
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yolk-biggs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastian Munz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-03 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: biggs is a small ruby tool to format postal addresses from over 60 countries. Use it as a standalone ruby gem or as a plugin for Rails.
|
17
|
+
email: sebastian@yo.lk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.textile
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/biggs
|
28
|
+
- lib/biggs/activerecord.rb
|
29
|
+
- lib/biggs/formatter.rb
|
30
|
+
- lib/biggs.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/yolk/biggs
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --inline-source
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: biggs is a small ruby tool to format postal addresses from over 60 countries. Use it as a standalone ruby gem or as a plugin for Rails.
|
58
|
+
test_files: []
|
59
|
+
|