postal_address 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4b666d6627a5f91c38df6261e7422c38433c618
4
- data.tar.gz: fbb5d39591acd9e33f0d201902c8ec2615595098
3
+ metadata.gz: e2adb655210bf5988140022ca184b5e956a32f1c
4
+ data.tar.gz: c123706c063eec3009944f574e90ac7205bc44e3
5
5
  SHA512:
6
- metadata.gz: 3ff76d7b99f8be39c853d78510e3ac1ff6b6ba3499f86c50fe64bddccc0cccb63aa1108d25a1dd77be03af4f163784de2871b13e9326d11fcd9498d7a27c2023
7
- data.tar.gz: 0f1148e1e66eebc674b4bfb0c8badca106fbd7a033e46d33001c1db1a5b2fc92a417fcbeb6889e67fa17be1a986acbc3eb09b549b989a6e26008f1262ddcb3e7
6
+ metadata.gz: 3fbafa9585c991310586ee1136afd494f601924b44afb1da34cd3175785f2722199992d703c8885d93743681401698a1f1f47c26021eb32a8974aff26ba2f5ba
7
+ data.tar.gz: 0e0696d0f64664cf9e10b1b578e07365d577ddfcecb3fc4fbe41a9f27936eb32eb86332b89ca1109f7e48796a381a7e8547de35c2428102e1b05d7214bf988bb
@@ -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
- def home_country=(code)
17
- @home_country = code && code.to_s.downcase
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
- attr_accessor :recipient, :street, :zip, :city, :state, :country_code
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|
@@ -1,3 +1,3 @@
1
1
  class PostalAddress
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postal_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Melchert