postal_address 0.0.2 → 0.0.3

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: e2adb655210bf5988140022ca184b5e956a32f1c
4
- data.tar.gz: c123706c063eec3009944f574e90ac7205bc44e3
3
+ metadata.gz: f32866bfaa46d951c32b91d92188bc0bf59b12ea
4
+ data.tar.gz: 27bcd0cff098480e72fd8111318bb4848131ce72
5
5
  SHA512:
6
- metadata.gz: 3fbafa9585c991310586ee1136afd494f601924b44afb1da34cd3175785f2722199992d703c8885d93743681401698a1f1f47c26021eb32a8974aff26ba2f5ba
7
- data.tar.gz: 0e0696d0f64664cf9e10b1b578e07365d577ddfcecb3fc4fbe41a9f27936eb32eb86332b89ca1109f7e48796a381a7e8547de35c2428102e1b05d7214bf988bb
6
+ metadata.gz: f84883c634e3d01eae501d0155a5ce12886a8ff2dfb63fd8dc6a784e9f93a08861d10ca01bec9da88e4414eb8fcaf5b16b97cb15dceba2f1c37407df2135d21e
7
+ data.tar.gz: 5823c60c68513d2a3dd17402b4042c8008183dd7a0ff6ffb04ecb6ccea87bf2ab4ec88375d377393cdcff70dc82a28be10555083976d6a88fc7945efacace734
@@ -1,72 +1,17 @@
1
1
  require "postal_address/version"
2
- require "yaml"
2
+ require "postal_address/class_methods"
3
+ require "postal_address/instance_methods"
3
4
 
4
5
  class PostalAddress
5
- class << self
6
- attr_reader :home_country
7
-
8
- def home_country=(code)
9
- @home_country = code && code.to_s.downcase
10
- end
11
-
12
- def formats
13
- @formats ||= YAML.load_file("data/address_formats.yml")
14
- end
6
+ extend ClassMethods
7
+ include InstanceMethods
15
8
 
16
- def country_names
17
- @country_names ||= YAML.load_file("data/country_names.yml")
18
- end
19
-
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}="
30
- end
31
- end
32
-
33
9
  # 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
40
-
41
- def initialize(attributes={})
42
- attributes.each do |attr, value|
43
- self.public_send("#{attr}=", value) if self.respond_to?("#{attr}=")
44
- end if attributes
45
- end
46
-
47
- def to_s
48
- (format % to_h).strip
49
- end
50
-
51
- def to_h
52
- { :recipient=>recipient, :street=>street, :zip=>zip, :city=>city, :state=>state, :country=>country }
53
- end
54
-
55
- def country_code=(code)
56
- @country_code = code && code.to_s.downcase
57
- end
58
-
59
- def country
60
- self.class.country_names[country_code] unless in_home_country?
61
- end
62
-
63
- private
64
-
65
- def format
66
- self.class.formats[country_code] || self.class.formats[state ? 'us' : 'de']
67
- end
68
-
69
- def in_home_country?
70
- self.class.home_country == country_code
71
- end
72
- end
10
+ attribute :recipient, itemprop: 'name'
11
+ attribute :street, itemprop: 'streetAddress', alias: [:street_address]
12
+ attribute :zip, itemprop: 'postalCode', alias: [:zip_code, :postal_code, :postcode]
13
+ attribute :state, itemprop: 'addressRegion', alias: [:region, :province, :territory, :administrative_area_level_1]
14
+ attribute :city, itemprop: 'addressLocality', alias: [:locality]
15
+ attribute :country_code, itemprop: 'addressCountry', alias: [:country_id], writer: false
16
+ attribute :country, itemprop: 'addressCountry', writer: false, reader: false
17
+ end
@@ -0,0 +1,36 @@
1
+ require "yaml"
2
+
3
+ class PostalAddress
4
+ module ClassMethods
5
+ attr_reader :home_country
6
+
7
+ def home_country=(code)
8
+ @home_country = code && code.to_s.downcase
9
+ end
10
+
11
+ def formats
12
+ @formats ||= YAML.load_file("data/address_formats.yml")
13
+ end
14
+
15
+ def country_names
16
+ @country_names ||= YAML.load_file("data/country_names.yml")
17
+ end
18
+
19
+ def attributes
20
+ @attributes ||= {}
21
+ end
22
+
23
+ private
24
+
25
+ def attribute(name, params={})
26
+ attributes[name] = params
27
+ attr_reader name unless params[:reader] === false
28
+ attr_writer name unless params[:writer] === false
29
+ alias_attribute_writer(name, Array(params[:alias])) if params[:alias]
30
+ end
31
+
32
+ def alias_attribute_writer(original, aliases)
33
+ aliases.each { |name| alias_method :"#{name}=", :"#{original}=" }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,58 @@
1
+ class PostalAddress
2
+ module InstanceMethods
3
+ def initialize(params={})
4
+ params.each do |attr, value|
5
+ self.public_send("#{attr}=", value) if self.respond_to?("#{attr}=")
6
+ end if params
7
+ end
8
+
9
+ def to_h
10
+ attributes
11
+ end
12
+
13
+ def to_s
14
+ format(attributes)
15
+ end
16
+
17
+ def to_html(params={})
18
+ address = format(attributes(:html)).gsub("\n", "<br>")
19
+ content_tag((params.delete(:tag) || :div), address, params.merge(itemscope: nil, itemtype: 'http://schema.org/PostalAddress'))
20
+ end
21
+
22
+ def country_code=(code)
23
+ @country_code = code && code.to_s.downcase
24
+ end
25
+
26
+ def country
27
+ @country = self.class.country_names[country_code] unless in_home_country?
28
+ end
29
+
30
+ private
31
+
32
+ def format(hash)
33
+ (address_format % hash).strip
34
+ end
35
+
36
+ def address_format
37
+ self.class.formats[country_code] || self.class.formats[state ? 'us' : 'de']
38
+ end
39
+
40
+ def in_home_country?
41
+ self.class.home_country == country_code
42
+ end
43
+
44
+ def attributes(html=false)
45
+ self.class.attributes.inject({}) do |hash, (key, attribute)|
46
+ value = public_send(key)
47
+ value = value && content_tag(:span, value, itemprop: attribute[:itemprop]) if html
48
+ hash[key] = value
49
+ hash
50
+ end
51
+ end
52
+
53
+ def content_tag(tag, content='', attributes={})
54
+ attrs = attributes.map {|k,v| k.to_s + (v ? "=\"#{v}\"" : '') }.unshift('').join(' ')
55
+ "<#{tag}#{attrs}>#{content}</#{tag}>"
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  class PostalAddress
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -41,7 +41,7 @@ describe PostalAddress do
41
41
  it "should respond to to_h" do
42
42
  r = address.to_h
43
43
  r.must_be_kind_of Hash
44
- r.keys.length.must_equal 6
44
+ r.keys.length.must_equal 7
45
45
  end
46
46
 
47
47
  it "should format to US format" do
@@ -83,26 +83,34 @@ describe PostalAddress do
83
83
 
84
84
  it "should respond to state aliases" do
85
85
  [:region, :province, :territory, :administrative_area_level_1].each do |alias_name|
86
- address.must_respond_to alias_name
86
+ #address.must_respond_to alias_name
87
87
  address.must_respond_to :"#{alias_name}="
88
- address.send(alias_name).must_equal address.state
88
+ #address.send(alias_name).must_equal address.state
89
89
  end
90
90
  end
91
91
 
92
92
  it "should respond to city aliases" do
93
93
  [:locality].each do |alias_name|
94
- address.must_respond_to alias_name
94
+ #address.must_respond_to alias_name
95
95
  address.must_respond_to :"#{alias_name}="
96
- address.send(alias_name).must_equal address.city
96
+ #address.send(alias_name).must_equal address.city
97
97
  end
98
98
  end
99
99
 
100
100
  it "should respond to zip aliases" do
101
101
  [:zip_code, :postal_code, :postcode].each do |alias_name|
102
- address.must_respond_to alias_name
102
+ #address.must_respond_to alias_name
103
103
  address.must_respond_to :"#{alias_name}="
104
- address.send(alias_name).must_equal address.zip
104
+ #address.send(alias_name).must_equal address.zip
105
105
  end
106
106
  end
107
+
108
+ it "should return html" do
109
+ address.to_html.must_equal "<div itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></div>"
110
+ end
111
+
112
+ it "should return custom html" do
113
+ address.to_html(tag: :section, itemprop: 'address').must_equal "<section itemprop=\"address\" itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></section>"
114
+ end
107
115
  end
108
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postal_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Melchert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-24 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,8 @@ files:
82
82
  - data/address_formats.yml
83
83
  - data/country_names.yml
84
84
  - lib/postal_address.rb
85
+ - lib/postal_address/class_methods.rb
86
+ - lib/postal_address/instance_methods.rb
85
87
  - lib/postal_address/version.rb
86
88
  - postal_address.gemspec
87
89
  - spec/postal_address_spec.rb