eropple-biggs 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.rvmrc +1 -0
- data/CHANGES.md +61 -0
- data/Gemfile +6 -0
- data/LICENSE +20 -0
- data/README.md +149 -0
- data/Rakefile +7 -0
- data/biggs.gemspec +28 -0
- data/init.rb +1 -0
- data/lib/biggs/activerecord.rb +66 -0
- data/lib/biggs/format.rb +24 -0
- data/lib/biggs/formatter.rb +33 -0
- data/lib/biggs/version.rb +3 -0
- data/lib/biggs.rb +16 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/unit/activerecord_spec.rb +147 -0
- data/spec/unit/biggs_spec.rb +80 -0
- data/spec/unit/format_spec.rb +33 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e85e26e3b8e91e6ad36b4a977eb0612533919305
|
4
|
+
data.tar.gz: 531cbd742daa5ba9a44363ddb23c7714e5997be5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 22ff1be8108399d33269fcf39c29648e0302d7857839fe26a94be1e80c8267244488721da1dad40ea9c241d2be1a3dd9acb1c7fbc559b976759269c7e9b1f20c
|
7
|
+
data.tar.gz: c03358b221cc0b23d5cc1d56737fb7f0b602fa65314604a09429222acbe0122c00d81fb35f1d42adb0020c039ca45facb8efd237436091f557f8fe66368833fe
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3@biggs --create
|
data/CHANGES.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
### dev
|
2
|
+
|
3
|
+
### 0.3.3 / 2013-05-06
|
4
|
+
|
5
|
+
* Added support for Rails 4 (by [mdemare](https://github.com/mdemare))
|
6
|
+
|
7
|
+
### 0.3.2 / 2013-02-19
|
8
|
+
|
9
|
+
* Fixed warning in Biggs::Format
|
10
|
+
|
11
|
+
### 0.3.1 / 2012-08-02
|
12
|
+
|
13
|
+
* Fixed LI format
|
14
|
+
|
15
|
+
### 0.3.0 / 2012-06-15
|
16
|
+
|
17
|
+
* Added country names for countries with default/unknown address format
|
18
|
+
* Simplified inclusion of Biggs::ActiveRecordAdapter
|
19
|
+
* Changed doc-format from textile to markdown
|
20
|
+
|
21
|
+
### 0.2.2 / 2012-02-06
|
22
|
+
|
23
|
+
* Compatibility with ActiveRecord/Rails 3.2
|
24
|
+
|
25
|
+
### 0.2.1 / 2011-11-09
|
26
|
+
|
27
|
+
* Fixed gemspec activerecord dependency
|
28
|
+
|
29
|
+
### 0.2.0 / 2011-10-12
|
30
|
+
|
31
|
+
* Added Bundler & removed jewler
|
32
|
+
* Fixed Specs
|
33
|
+
* Added NC (New Caledonia)
|
34
|
+
|
35
|
+
### 0.1.5 / 2010-11-24
|
36
|
+
|
37
|
+
* Fixed address format for Norway
|
38
|
+
* Added TH (Thailand)
|
39
|
+
|
40
|
+
### 0.1.4 / 2009-5-21
|
41
|
+
|
42
|
+
* Fixed Specs
|
43
|
+
|
44
|
+
### 0.1.3 / 2009-3-6
|
45
|
+
|
46
|
+
* Correct japanese address format. (by [hiroshi](https://github.com/hiroshi))
|
47
|
+
* Fixed docs for current API. (by [hiroshi](https://github.com/hiroshi))
|
48
|
+
|
49
|
+
### 0.1.2 / 2009-3-4
|
50
|
+
|
51
|
+
* Values can now be specified by an array of symbols.
|
52
|
+
|
53
|
+
### 0.1.1 / 2009-3-3
|
54
|
+
|
55
|
+
* Refactored activerecord-adapter to include only basic setup method in ActiveRecord:Base
|
56
|
+
|
57
|
+
### 0.1.0 / 2009-3-3
|
58
|
+
|
59
|
+
* Allow Procs as params in biggs-activerecord-setup.
|
60
|
+
* Cleanup
|
61
|
+
|
data/Gemfile
ADDED
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.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.
|
2
|
+
|
3
|
+
### Install
|
4
|
+
|
5
|
+
As a ruby gem:
|
6
|
+
|
7
|
+
sudo gem install 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
|
+
### 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
|
+
:postalcode => 10999,
|
22
|
+
:region => "Berlin" # <= region/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
|
+
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
|
+
### 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, postalcode, region, 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
|
+
:postalcode => :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 region specified, and the french/german format for addresses without an region.
|
77
|
+
|
78
|
+
### 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 Republic
|
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
|
+
* Korea, Republic of
|
112
|
+
* Kuwait
|
113
|
+
* Lebanon
|
114
|
+
* Liechtenstein
|
115
|
+
* Luxembourg
|
116
|
+
* Macedonia, the Former Yugoslav Republic Of
|
117
|
+
* Mexico
|
118
|
+
* Netherlands
|
119
|
+
* New Caledonia
|
120
|
+
* New Zealand
|
121
|
+
* Norway
|
122
|
+
* Oman
|
123
|
+
* Philippines
|
124
|
+
* Poland
|
125
|
+
* Portugal
|
126
|
+
* Qatar
|
127
|
+
* Romania
|
128
|
+
* Russian Federation
|
129
|
+
* Saudi Arabia
|
130
|
+
* Singapore
|
131
|
+
* Slovakia
|
132
|
+
* Slovenia
|
133
|
+
* South Africa
|
134
|
+
* Spain
|
135
|
+
* Sweden
|
136
|
+
* Switzerland
|
137
|
+
* Syrian Arab Republic
|
138
|
+
* Taiwan, Republic Of China
|
139
|
+
* Thailand
|
140
|
+
* Turkey
|
141
|
+
* Ukraine
|
142
|
+
* United Arab Emirates
|
143
|
+
* United Kingdom
|
144
|
+
* United States
|
145
|
+
* Yemen
|
146
|
+
|
147
|
+
biggs is tested to behave well with Rails 3.0, 3.1, 3.2 and 4.0
|
148
|
+
|
149
|
+
Copyright (c) 2009-2013 Yolk Sebastian Munz & Julia Soergel GbR
|
data/Rakefile
ADDED
data/biggs.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'biggs/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'eropple-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 = 'biggs is a small ruby gem/rails plugin for formatting postal addresses from over 60 countries.'
|
13
|
+
s.description = '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_dependency 'countries', '>= 0.9.3'
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
s.add_development_dependency 'rspec', '>= 3.0.0'
|
26
|
+
s.add_development_dependency 'rspec-its', '>= 1.1.0'
|
27
|
+
s.add_development_dependency 'sqlite3', '>= 1.3.5'
|
28
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'biggs'
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute'
|
2
|
+
|
3
|
+
module Biggs
|
4
|
+
module ActiveRecordAdapter
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(InitialClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module InitialClassMethods
|
10
|
+
def biggs(method_name = nil, options = {})
|
11
|
+
class_attribute :biggs_value_methods
|
12
|
+
class_attribute :biggs_instance
|
13
|
+
|
14
|
+
send(:include, Biggs::ActiveRecordAdapter::InstanceMethods)
|
15
|
+
alias_method(method_name || :postal_address, :biggs_postal_address)
|
16
|
+
|
17
|
+
value_methods = {}
|
18
|
+
Biggs::Formatter::FIELDS.each do |field|
|
19
|
+
value_methods[field] = options.delete(field) if options[field]
|
20
|
+
end
|
21
|
+
|
22
|
+
self.biggs_value_methods = value_methods
|
23
|
+
self.biggs_instance = Biggs::Formatter.new(options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
def biggs_postal_address
|
29
|
+
self.class.biggs_instance.format(biggs_country, biggs_values)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def biggs_country
|
35
|
+
biggs_get_value(:country)
|
36
|
+
end
|
37
|
+
|
38
|
+
def biggs_values
|
39
|
+
values = {}
|
40
|
+
(Biggs::Formatter::FIELDS - [:country]).each do |field|
|
41
|
+
values[field] = biggs_get_value(field)
|
42
|
+
end
|
43
|
+
values
|
44
|
+
end
|
45
|
+
|
46
|
+
def biggs_get_value(field)
|
47
|
+
key = self.class.biggs_value_methods[field.to_sym] || field.to_sym
|
48
|
+
|
49
|
+
case key
|
50
|
+
when Symbol
|
51
|
+
send(key.to_sym)
|
52
|
+
when Proc
|
53
|
+
key.call(self).to_s
|
54
|
+
when Array
|
55
|
+
if key.all? { |it| it.is_a?(Symbol) }
|
56
|
+
key.map { |method| send(method) }.reject(&:blank?).join("\n")
|
57
|
+
else
|
58
|
+
fail "Biggs: Can't handle #{field} type Array with non-symbolic entries"
|
59
|
+
end
|
60
|
+
else
|
61
|
+
fail "Biggs: Can't handle #{field} type #{key.class}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/biggs/format.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Biggs
|
2
|
+
class Format
|
3
|
+
attr_reader :country_name, :iso_code, :format_string, :country
|
4
|
+
|
5
|
+
def initialize(iso_code)
|
6
|
+
@iso_code = iso_code.to_s.downcase
|
7
|
+
@country = Biggs.country[@iso_code]
|
8
|
+
@country_name = @country ? @country.name : nil
|
9
|
+
@format_string = @country ? @country.address_format : nil
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def find(iso_code)
|
14
|
+
entries_cache[iso_code] ||= new(iso_code)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def entries_cache
|
20
|
+
@entries_cache ||= {}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Biggs
|
2
|
+
class Formatter
|
3
|
+
FIELDS = [:recipient, :street, :city, :region, :postalcode, :country]
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
@blank_country_on = [options[:blank_country_on]].compact.flatten.map { |s| s.to_s.downcase }
|
7
|
+
end
|
8
|
+
|
9
|
+
def format(iso_code, values = {})
|
10
|
+
values.symbolize_keys! if values.respond_to?(:symbolize_keys!)
|
11
|
+
|
12
|
+
format = Biggs::Format.find(iso_code)
|
13
|
+
format_string = (format.format_string || default_format_string(values[:region])).dup.to_s
|
14
|
+
country_name = blank_country_on.include?(format.iso_code) ? '' : format.country_name || format.iso_code
|
15
|
+
|
16
|
+
(FIELDS - [:country]).each do |key|
|
17
|
+
format_string.gsub!(/\{\{#{key}\}\}/, (values[key] || '').to_s)
|
18
|
+
end
|
19
|
+
format_string.gsub!(/\{\{country\}\}/, country_name)
|
20
|
+
format_string.gsub(/\n$/, '')
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_accessor :blank_country_on, :default_country_without_region, :default_country_with_region
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def default_format_string(region)
|
28
|
+
region && region != '' ?
|
29
|
+
Biggs.country[default_country_with_region || 'us'].address_format :
|
30
|
+
Biggs.country[default_country_without_region || 'fr'].address_format
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/biggs.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'biggs/format'
|
2
|
+
require 'biggs/formatter'
|
3
|
+
require 'countries'
|
4
|
+
|
5
|
+
module Biggs
|
6
|
+
class << self
|
7
|
+
def country
|
8
|
+
@@formats ||= Country
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && !ActiveRecord::Base.respond_to?(:biggs_formatter)
|
14
|
+
require 'biggs/activerecord'
|
15
|
+
ActiveRecord::Base.send :include, Biggs::ActiveRecordAdapter
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH << 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 { |it| it.method_accessed_from_proc + 'XX' },
|
47
|
+
region: proc { |it| it.region.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
|
+
expect(FooBar.included_modules).to be_include(Biggs::ActiveRecordAdapter)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should set class value biggs_value_methods' do
|
82
|
+
expect(FooBar.class_eval('biggs_value_methods')).to be_is_a(Hash)
|
83
|
+
expect(FooBar.class_eval('biggs_value_methods').size).to be_zero
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should set class value biggs_instance' do
|
87
|
+
expect(FooBar.class_eval('biggs_instance')).to be_is_a(Biggs::Formatter)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should respond to biggs' do
|
91
|
+
expect(FooBar).to 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
|
+
expect(FooBarEmpty.new).not_to be_respond_to(:postal_address)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'Standard' do
|
104
|
+
it 'should have postal_address method' do
|
105
|
+
expect(FooBar.new).to be_respond_to(:postal_address)
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should return postal_address on postal_address' do
|
109
|
+
expect(FooBar.new.postal_address).to eql("RECIPIENT\nSTREET\nCITY REGION POSTALCODE\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
|
+
expect(FooBarCustomFields.new.postal_address).to eql("RECIPIENT\nSTREET\nPOSTALCODE 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
|
+
expect(FooBarCustomBlankDECountry.new.postal_address).to eql("RECIPIENT\nSTREET\nPOSTALCODE CITY")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'Customized Method name' do
|
126
|
+
it 'should have my_postal_address_method' do
|
127
|
+
expect(FooBarCustomMethod.new).to be_respond_to(:my_postal_address_method)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should return formatted address on my_postal_address_method' do
|
131
|
+
expect(FooBarCustomMethod.new.my_postal_address_method).to eql("RECIPIENT\nSTREET\nCITY REGION POSTALCODE\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
|
+
expect(FooBarCustomProc.new.postal_address).to eql("RECIPIENT\nSTREET\nCITY region POSTALCODE\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
|
+
expect(FooBarCustomArray.new.postal_address).to eql("RECIPIENT\nAddress line 1\nAddress line 2\nCITY REGION POSTALCODE\nUnited States")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
FAKE_ATTR_WITH_REGION = { region: 'REGION', city: 'CITY', postalcode: 12_345, street: 'STREET', recipient: 'MR. X' }
|
4
|
+
FAKE_ATTR_WO_REGION = { city: 'CITY', postalcode: 12_345, 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
|
+
expect(@biggs.format('us', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\nCITY REGION 12345\nUnited States")
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should format to de format' do
|
15
|
+
expect(@biggs.format('de', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nGermany")
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should format to british format' do
|
19
|
+
expect(@biggs.format('gb', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\nCITY\nREGION\n12345\nUnited Kingdom")
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should format to fr format' do
|
23
|
+
expect(@biggs.format('fr', FAKE_ATTR_WO_REGION)).to 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 REGION given' do
|
27
|
+
expect(@biggs.format('unknown', FAKE_ATTR_WO_REGION)).to 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 REGION given' do
|
31
|
+
expect(@biggs.format('unknown', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\nCITY REGION 12345\nunknown")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should format to no(rwegian) format' do
|
35
|
+
expect(@biggs.format('no', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nNorway")
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should format to NC format' do
|
39
|
+
expect(@biggs.format('nc', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nNew Caledonia")
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should use country name if Country is known but format not' do
|
43
|
+
expect(@biggs.format('af', FAKE_ATTR_WO_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nAfghanistan")
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should use ISO Code if Country is unknown' do
|
47
|
+
expect(@biggs.format('xx', FAKE_ATTR_WO_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nxx")
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
describe Biggs, 'with options' do
|
53
|
+
|
54
|
+
describe 'blank_country_on de' do
|
55
|
+
before { @biggs = Biggs::Formatter.new(blank_country_on: 'de') }
|
56
|
+
|
57
|
+
it "should have blank country in 'de' address" do
|
58
|
+
expect(@biggs.format('de', FAKE_ATTR_WO_REGION)).to eql("MR. X\nSTREET\n12345 CITY")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should have country in 'fr' address" do
|
62
|
+
expect(@biggs.format('fr', FAKE_ATTR_WO_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nFrance")
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'blank_country_on multiple (US,de)' do
|
68
|
+
before { @biggs = Biggs::Formatter.new(blank_country_on: %w(US de)) }
|
69
|
+
|
70
|
+
it "should have blank country in 'us' address" do
|
71
|
+
expect(@biggs.format('us', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\nCITY REGION 12345")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should have country in 'fr' address" do
|
75
|
+
expect(@biggs.format('fr', FAKE_ATTR_WITH_REGION)).to eql("MR. X\nSTREET\n12345 CITY\nFrance")
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
require 'rspec/its'
|
3
|
+
|
4
|
+
describe Biggs::Format do
|
5
|
+
describe '.find' do
|
6
|
+
context 'known country with format' do
|
7
|
+
subject { Biggs::Format.find('cn') }
|
8
|
+
|
9
|
+
it { is_expected.to be_kind_of(Biggs::Format) }
|
10
|
+
its(:country_name) { should eql('China') }
|
11
|
+
its(:iso_code) { should eql('cn') }
|
12
|
+
its(:format_string) { should eql("{{recipient}}\n{{street}}\n{{postalcode}} {{city}} {{region}}\n{{country}}") }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'known country with unknown format' do
|
16
|
+
subject { Biggs::Format.find('af') }
|
17
|
+
|
18
|
+
it { is_expected.to be_kind_of(Biggs::Format) }
|
19
|
+
its(:country_name) { should eql('Afghanistan') }
|
20
|
+
its(:iso_code) { should eql('af') }
|
21
|
+
its(:format_string) { should eql(nil) }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'unknown country' do
|
25
|
+
subject { Biggs::Format.find('xx') }
|
26
|
+
|
27
|
+
it { is_expected.to be_kind_of(Biggs::Format) }
|
28
|
+
its(:country_name) { should eql(nil) }
|
29
|
+
its(:iso_code) { should eql('xx') }
|
30
|
+
its(:format_string) { should eql(nil) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eropple-biggs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastian Munz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-its
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.5
|
83
|
+
description: biggs is a small ruby gem/rails plugin for formatting postal addresses
|
84
|
+
from over 60 countries.
|
85
|
+
email:
|
86
|
+
- sebastian@yo.lk
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rvmrc"
|
93
|
+
- CHANGES.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- biggs.gemspec
|
99
|
+
- init.rb
|
100
|
+
- lib/biggs.rb
|
101
|
+
- lib/biggs/activerecord.rb
|
102
|
+
- lib/biggs/format.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
|
+
- spec/unit/format_spec.rb
|
109
|
+
homepage: https://github.com/yolk/biggs
|
110
|
+
licenses: []
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project: biggs
|
128
|
+
rubygems_version: 2.4.6
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: biggs is a small ruby gem/rails plugin for formatting postal addresses from
|
132
|
+
over 60 countries.
|
133
|
+
test_files:
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/unit/activerecord_spec.rb
|
136
|
+
- spec/unit/biggs_spec.rb
|
137
|
+
- spec/unit/format_spec.rb
|