postal_address 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/postal_address.rb +21 -3
- data/lib/postal_address/version.rb +1 -1
- data/spec/postal_address_spec.rb +24 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e2adb655210bf5988140022ca184b5e956a32f1c
|
|
4
|
+
data.tar.gz: c123706c063eec3009944f574e90ac7205bc44e3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3fbafa9585c991310586ee1136afd494f601924b44afb1da34cd3175785f2722199992d703c8885d93743681401698a1f1f47c26021eb32a8974aff26ba2f5ba
|
|
7
|
+
data.tar.gz: 0e0696d0f64664cf9e10b1b578e07365d577ddfcecb3fc4fbe41a9f27936eb32eb86332b89ca1109f7e48796a381a7e8547de35c2428102e1b05d7214bf988bb
|
data/lib/postal_address.rb
CHANGED
|
@@ -5,6 +5,10 @@ class PostalAddress
|
|
|
5
5
|
class << self
|
|
6
6
|
attr_reader :home_country
|
|
7
7
|
|
|
8
|
+
def home_country=(code)
|
|
9
|
+
@home_country = code && code.to_s.downcase
|
|
10
|
+
end
|
|
11
|
+
|
|
8
12
|
def formats
|
|
9
13
|
@formats ||= YAML.load_file("data/address_formats.yml")
|
|
10
14
|
end
|
|
@@ -13,12 +17,26 @@ class PostalAddress
|
|
|
13
17
|
@country_names ||= YAML.load_file("data/country_names.yml")
|
|
14
18
|
end
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def attribute(name, *aliases)
|
|
23
|
+
attr_accessor name
|
|
24
|
+
aliases.each { |a| define_attribute_alias(a, name) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def define_attribute_alias(new_name, original)
|
|
28
|
+
alias_method new_name, original
|
|
29
|
+
alias_method :"#{new_name}=", :"#{original}="
|
|
18
30
|
end
|
|
19
31
|
end
|
|
20
32
|
|
|
21
|
-
|
|
33
|
+
# define the postal address attributes and aliases for easier assignment
|
|
34
|
+
attribute :recipient
|
|
35
|
+
attribute :street, :street_address
|
|
36
|
+
attribute :zip, :zip_code, :postal_code, :postcode
|
|
37
|
+
attribute :state, :region, :province, :territory, :administrative_area_level_1
|
|
38
|
+
attribute :city, :locality
|
|
39
|
+
attribute :country_code, :country_id # expects ISO 3166 Alpha 2 codes
|
|
22
40
|
|
|
23
41
|
def initialize(attributes={})
|
|
24
42
|
attributes.each do |attr, value|
|
data/spec/postal_address_spec.rb
CHANGED
|
@@ -80,5 +80,29 @@ describe PostalAddress do
|
|
|
80
80
|
address.state = nil
|
|
81
81
|
address.to_s.must_equal "Tobias Füncke\n101 Broadway\n10002 New York City"
|
|
82
82
|
end
|
|
83
|
+
|
|
84
|
+
it "should respond to state aliases" do
|
|
85
|
+
[:region, :province, :territory, :administrative_area_level_1].each do |alias_name|
|
|
86
|
+
address.must_respond_to alias_name
|
|
87
|
+
address.must_respond_to :"#{alias_name}="
|
|
88
|
+
address.send(alias_name).must_equal address.state
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it "should respond to city aliases" do
|
|
93
|
+
[:locality].each do |alias_name|
|
|
94
|
+
address.must_respond_to alias_name
|
|
95
|
+
address.must_respond_to :"#{alias_name}="
|
|
96
|
+
address.send(alias_name).must_equal address.city
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "should respond to zip aliases" do
|
|
101
|
+
[:zip_code, :postal_code, :postcode].each do |alias_name|
|
|
102
|
+
address.must_respond_to alias_name
|
|
103
|
+
address.must_respond_to :"#{alias_name}="
|
|
104
|
+
address.send(alias_name).must_equal address.zip
|
|
105
|
+
end
|
|
106
|
+
end
|
|
83
107
|
end
|
|
84
108
|
end
|