addresslogic 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -1
- data/lib/addresslogic.rb +16 -25
- data/lib/addresslogic/version.rb +1 -1
- data/test/test_addresslogic.rb +60 -26
- data/test/test_helper.rb +7 -1
- metadata +1 -1
data/CHANGELOG.rdoc
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
== 1.
|
1
|
+
== 1.1.1
|
2
|
+
|
3
|
+
* Clean up code to use recursion and allow fields to be specified like: [:street1, :street2, [:city, [:state, :zip]], :country]. Which is the standard american way of displaying address. Which looks like: [street1, street2, "#{city}, #{state} #{zip}", country]
|
4
|
+
|
5
|
+
== 1.1.0
|
2
6
|
|
3
7
|
* Changed inclusion method to use apply_addresslogic instead of include Addresslogic. This allows you to pass options and specify alternate fields.
|
4
8
|
|
data/lib/addresslogic.rb
CHANGED
@@ -41,39 +41,30 @@ module Addresslogic
|
|
41
41
|
#
|
42
42
|
# * <tt>only:</tt> fields you want included in the result
|
43
43
|
# * <tt>except:</tt> any fields you want excluded from the result
|
44
|
-
def address_parts(
|
44
|
+
def address_parts(*args)
|
45
|
+
options = args.extract_options!
|
45
46
|
options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
|
46
47
|
options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
|
47
|
-
|
48
|
+
fields = args[0] || address_parts_fields
|
49
|
+
level = args[1] || 0
|
50
|
+
|
48
51
|
parts = []
|
49
|
-
|
50
|
-
if
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
next if value.to_s.blank? || (options[:only] && !options[:only].include?(subpart)) || (options[:except] && options[:except].include?(subpart))
|
57
|
-
subparts << value
|
58
|
-
end
|
59
|
-
parts << subparts unless subparts.compact.empty?
|
52
|
+
fields.each do |field|
|
53
|
+
if field.is_a?(Array)
|
54
|
+
has_sub_array = field.find { |item| item.is_a?(Array) }
|
55
|
+
separator = has_sub_array ? ", " : " "
|
56
|
+
sub_parts = address_parts(field, level + 1, options).join(separator)
|
57
|
+
next if sub_parts.blank?
|
58
|
+
parts << sub_parts
|
60
59
|
else
|
61
|
-
next if !respond_to?(
|
62
|
-
value = send(
|
63
|
-
next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(
|
60
|
+
next if !respond_to?(field)
|
61
|
+
value = send(field)
|
62
|
+
next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(field)) || (options[:except] && options[:except].include?(field))
|
64
63
|
parts << value
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
68
|
-
|
69
|
-
if part.is_a?(Array)
|
70
|
-
part.collect{|sub| sub.to_s.strip.blank? ? nil : sub}.join(" ")
|
71
|
-
else
|
72
|
-
part.to_s.strip.blank? ? nil : part
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
return result.compact
|
67
|
+
parts
|
77
68
|
end
|
78
69
|
|
79
70
|
private
|
data/lib/addresslogic/version.rb
CHANGED
data/test/test_addresslogic.rb
CHANGED
@@ -1,41 +1,75 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
|
-
class
|
4
|
-
def
|
5
|
-
assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"],
|
3
|
+
class TestAddresslogic < Test::Unit::TestCase
|
4
|
+
def test_uk_uk_address_parts
|
5
|
+
assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
|
6
6
|
|
7
|
-
|
8
|
-
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"],
|
7
|
+
uk_address.street2 = ""
|
8
|
+
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
|
9
9
|
|
10
|
-
|
11
|
-
assert_equal ["12 Fancy House", "London W1 8AJ"],
|
10
|
+
uk_address.country = ""
|
11
|
+
assert_equal ["12 Fancy House", "London W1 8AJ"], uk_address.address_parts
|
12
12
|
|
13
|
-
|
14
|
-
assert_equal ["12 Fancy House", "W1 8AJ"],
|
13
|
+
uk_address.city = ""
|
14
|
+
assert_equal ["12 Fancy House", "W1 8AJ"], uk_address.address_parts
|
15
15
|
|
16
|
-
|
17
|
-
assert_equal ["W1 8AJ"],
|
16
|
+
uk_address.street1 = ""
|
17
|
+
assert_equal ["W1 8AJ"], uk_address.address_parts
|
18
18
|
|
19
|
-
|
20
|
-
assert_equal [],
|
19
|
+
uk_address.zip = ""
|
20
|
+
assert_equal [], uk_address.address_parts
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_american_address_parts
|
24
|
+
assert_equal ["12 Fancy House", "Bond Street", "Tampa, Florida 45334", "United States"], american_address.address_parts
|
25
|
+
|
26
|
+
american_address.street2 = ""
|
27
|
+
assert_equal ["12 Fancy House", "Tampa, Florida 45334", "United States"], american_address.address_parts
|
28
|
+
|
29
|
+
american_address.country = ""
|
30
|
+
assert_equal ["12 Fancy House", "Tampa, Florida 45334"], american_address.address_parts
|
31
|
+
|
32
|
+
american_address.city = ""
|
33
|
+
assert_equal ["12 Fancy House", "Florida 45334"], american_address.address_parts
|
34
|
+
|
35
|
+
american_address.state = ""
|
36
|
+
assert_equal ["12 Fancy House", "45334"], american_address.address_parts
|
37
|
+
|
38
|
+
american_address.street1 = ""
|
39
|
+
assert_equal ["45334"], american_address.address_parts
|
40
|
+
|
41
|
+
american_address.zip = ""
|
42
|
+
assert_equal [], american_address.address_parts
|
21
43
|
end
|
22
44
|
|
23
45
|
def test_options
|
24
|
-
assert_equal ["Bond Street"],
|
25
|
-
assert_equal ["12 Fancy House", "Bond Street"],
|
26
|
-
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"],
|
27
|
-
assert_equal ["London W1 8AJ", "United Kingdom"],
|
46
|
+
assert_equal ["Bond Street"], uk_address.address_parts(:only => :street2)
|
47
|
+
assert_equal ["12 Fancy House", "Bond Street"], uk_address.address_parts(:only => [:street1, :street2])
|
48
|
+
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => :street2)
|
49
|
+
assert_equal ["London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => [:street1, :street2])
|
28
50
|
end
|
29
51
|
|
30
52
|
private
|
31
|
-
def
|
32
|
-
return @
|
33
|
-
@
|
34
|
-
@
|
35
|
-
@
|
36
|
-
@
|
37
|
-
@
|
38
|
-
@
|
39
|
-
@
|
53
|
+
def uk_address
|
54
|
+
return @uk_address if @uk_address
|
55
|
+
@uk_address = UKAddress.new
|
56
|
+
@uk_address.street1 = "12 Fancy House"
|
57
|
+
@uk_address.street2 = "Bond Street"
|
58
|
+
@uk_address.city = "London"
|
59
|
+
@uk_address.zip = "W1 8AJ"
|
60
|
+
@uk_address.country = "United Kingdom"
|
61
|
+
@uk_address
|
62
|
+
end
|
63
|
+
|
64
|
+
def american_address
|
65
|
+
return @american_address if @american_address
|
66
|
+
@american_address = AmericanAddress.new
|
67
|
+
@american_address.street1 = "12 Fancy House"
|
68
|
+
@american_address.street2 = "Bond Street"
|
69
|
+
@american_address.city = "Tampa"
|
70
|
+
@american_address.zip = "45334"
|
71
|
+
@american_address.state = "Florida"
|
72
|
+
@american_address.country = "United States"
|
73
|
+
@american_address
|
40
74
|
end
|
41
75
|
end
|
data/test/test_helper.rb
CHANGED
@@ -3,8 +3,14 @@ require "rubygems"
|
|
3
3
|
require "ruby-debug"
|
4
4
|
require File.dirname(__FILE__) + "/../lib/addresslogic"
|
5
5
|
|
6
|
-
class
|
6
|
+
class UKAddress
|
7
7
|
attr_accessor :street1, :street2, :city, :zip, :country
|
8
8
|
include Addresslogic
|
9
9
|
apply_addresslogic :fields => [:street1, :street2, [:city, :zip], :country]
|
10
|
+
end
|
11
|
+
|
12
|
+
class AmericanAddress
|
13
|
+
attr_accessor :street1, :street2, :city, :state, :zip, :country
|
14
|
+
include Addresslogic
|
15
|
+
apply_addresslogic :fields => [:street1, :street2, [:city, [:state, :zip]], :country]
|
10
16
|
end
|