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 CHANGED
@@ -1,4 +1,8 @@
1
- == 1.0.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(options = {})
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
- address_parts_fields.each do |part|
50
- if part.is_a?(Array)
51
- # We only want to allow 2d arrays
52
- subparts = []
53
- part.flatten.each do |subpart|
54
- next if !respond_to?(subpart)
55
- value = send(subpart)
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?(part)
62
- value = send(part)
63
- next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(part)) || (options[:except] && options[:except].include?(part))
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
- result = parts.collect do |part|
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
@@ -44,7 +44,7 @@ module Addresslogic
44
44
 
45
45
  MAJOR = 1
46
46
  MINOR = 1
47
- TINY = 0
47
+ TINY = 1
48
48
 
49
49
  # The current version as a Version instance
50
50
  CURRENT = new(MAJOR, MINOR, TINY)
@@ -1,41 +1,75 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class TestAddresslogix < Test::Unit::TestCase
4
- def test_address_parts
5
- assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], address.address_parts
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
- address.street2 = ""
8
- assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts
7
+ uk_address.street2 = ""
8
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
9
9
 
10
- address.country = ""
11
- assert_equal ["12 Fancy House", "London W1 8AJ"], address.address_parts
10
+ uk_address.country = ""
11
+ assert_equal ["12 Fancy House", "London W1 8AJ"], uk_address.address_parts
12
12
 
13
- address.city = ""
14
- assert_equal ["12 Fancy House", "W1 8AJ"], address.address_parts
13
+ uk_address.city = ""
14
+ assert_equal ["12 Fancy House", "W1 8AJ"], uk_address.address_parts
15
15
 
16
- address.street1 = ""
17
- assert_equal ["W1 8AJ"], address.address_parts
16
+ uk_address.street1 = ""
17
+ assert_equal ["W1 8AJ"], uk_address.address_parts
18
18
 
19
- address.zip = ""
20
- assert_equal [], address.address_parts
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"], address.address_parts(:only => :street2)
25
- assert_equal ["12 Fancy House", "Bond Street"], address.address_parts(:only => [:street1, :street2])
26
- assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts(:except => :street2)
27
- assert_equal ["London W1 8AJ", "United Kingdom"], address.address_parts(:except => [:street1, :street2])
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 address
32
- return @address if @address
33
- @address = Address.new
34
- @address.street1 = "12 Fancy House"
35
- @address.street2 = "Bond Street"
36
- @address.city = "London"
37
- @address.zip = "W1 8AJ"
38
- @address.country = "United Kingdom"
39
- @address
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 Address
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addresslogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic