Permalink
Browse files

Release v1.1.1

  • Loading branch information...
1 parent c98beb0 commit 997bd339a7467fa7c4540f12c120c41086558b2b @binarylogic committed May 14, 2009
Showing with 98 additions and 54 deletions.
  1. +9 −0 .gitignore
  2. +5 −1 CHANGELOG.rdoc
  3. +16 −25 lib/addresslogic.rb
  4. +1 −1 lib/addresslogic/version.rb
  5. +60 −26 test/test_addresslogic.rb
  6. +7 −1 test/test_helper.rb
View
@@ -0,0 +1,9 @@
+.DS_Store
+.swp
+*.log
+*.sqlite3
+pkg/*
+coverage/*
+doc/*
+benchmarks/*
+
View
@@ -1,4 +1,8 @@
-== 1.0.1
+== 1.1.1
+
+* 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]
+
+== 1.1.0
* Changed inclusion method to use apply_addresslogic instead of include Addresslogic. This allows you to pass options and specify alternate fields.
View
@@ -41,39 +41,30 @@ module InstanceMethods
#
# * <tt>only:</tt> fields you want included in the result
# * <tt>except:</tt> any fields you want excluded from the result
- def address_parts(options = {})
+ def address_parts(*args)
+ options = args.extract_options!
options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
-
+ fields = args[0] || address_parts_fields
+ level = args[1] || 0
+
parts = []
- address_parts_fields.each do |part|
- if part.is_a?(Array)
- # We only want to allow 2d arrays
- subparts = []
- part.flatten.each do |subpart|
- next if !respond_to?(subpart)
- value = send(subpart)
- next if value.to_s.blank? || (options[:only] && !options[:only].include?(subpart)) || (options[:except] && options[:except].include?(subpart))
- subparts << value
- end
- parts << subparts unless subparts.compact.empty?
+ fields.each do |field|
+ if field.is_a?(Array)
+ has_sub_array = field.find { |item| item.is_a?(Array) }
+ separator = has_sub_array ? ", " : " "
+ sub_parts = address_parts(field, level + 1, options).join(separator)
+ next if sub_parts.blank?
+ parts << sub_parts
else
- next if !respond_to?(part)
- value = send(part)
- next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(part)) || (options[:except] && options[:except].include?(part))
+ next if !respond_to?(field)
+ value = send(field)
+ next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(field)) || (options[:except] && options[:except].include?(field))
parts << value
end
end
- result = parts.collect do |part|
- if part.is_a?(Array)
- part.collect{|sub| sub.to_s.strip.blank? ? nil : sub}.join(" ")
- else
- part.to_s.strip.blank? ? nil : part
- end
- end
-
- return result.compact
+ parts
end
private
@@ -44,7 +44,7 @@ def to_a
MAJOR = 1
MINOR = 1
- TINY = 0
+ TINY = 1
# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
@@ -1,41 +1,75 @@
require File.dirname(__FILE__) + '/test_helper.rb'
-class TestAddresslogix < Test::Unit::TestCase
- def test_address_parts
- assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], address.address_parts
+class TestAddresslogic < Test::Unit::TestCase
+ def test_uk_uk_address_parts
+ assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
- address.street2 = ""
- assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts
+ uk_address.street2 = ""
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
- address.country = ""
- assert_equal ["12 Fancy House", "London W1 8AJ"], address.address_parts
+ uk_address.country = ""
+ assert_equal ["12 Fancy House", "London W1 8AJ"], uk_address.address_parts
- address.city = ""
- assert_equal ["12 Fancy House", "W1 8AJ"], address.address_parts
+ uk_address.city = ""
+ assert_equal ["12 Fancy House", "W1 8AJ"], uk_address.address_parts
- address.street1 = ""
- assert_equal ["W1 8AJ"], address.address_parts
+ uk_address.street1 = ""
+ assert_equal ["W1 8AJ"], uk_address.address_parts
- address.zip = ""
- assert_equal [], address.address_parts
+ uk_address.zip = ""
+ assert_equal [], uk_address.address_parts
+ end
+
+ def test_american_address_parts
+ assert_equal ["12 Fancy House", "Bond Street", "Tampa, Florida 45334", "United States"], american_address.address_parts
+
+ american_address.street2 = ""
+ assert_equal ["12 Fancy House", "Tampa, Florida 45334", "United States"], american_address.address_parts
+
+ american_address.country = ""
+ assert_equal ["12 Fancy House", "Tampa, Florida 45334"], american_address.address_parts
+
+ american_address.city = ""
+ assert_equal ["12 Fancy House", "Florida 45334"], american_address.address_parts
+
+ american_address.state = ""
+ assert_equal ["12 Fancy House", "45334"], american_address.address_parts
+
+ american_address.street1 = ""
+ assert_equal ["45334"], american_address.address_parts
+
+ american_address.zip = ""
+ assert_equal [], american_address.address_parts
end
def test_options
- assert_equal ["Bond Street"], address.address_parts(:only => :street2)
- assert_equal ["12 Fancy House", "Bond Street"], address.address_parts(:only => [:street1, :street2])
- assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts(:except => :street2)
- assert_equal ["London W1 8AJ", "United Kingdom"], address.address_parts(:except => [:street1, :street2])
+ assert_equal ["Bond Street"], uk_address.address_parts(:only => :street2)
+ assert_equal ["12 Fancy House", "Bond Street"], uk_address.address_parts(:only => [:street1, :street2])
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => :street2)
+ assert_equal ["London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => [:street1, :street2])
end
private
- def address
- return @address if @address
- @address = Address.new
- @address.street1 = "12 Fancy House"
- @address.street2 = "Bond Street"
- @address.city = "London"
- @address.zip = "W1 8AJ"
- @address.country = "United Kingdom"
- @address
+ def uk_address
+ return @uk_address if @uk_address
+ @uk_address = UKAddress.new
+ @uk_address.street1 = "12 Fancy House"
+ @uk_address.street2 = "Bond Street"
+ @uk_address.city = "London"
+ @uk_address.zip = "W1 8AJ"
+ @uk_address.country = "United Kingdom"
+ @uk_address
+ end
+
+ def american_address
+ return @american_address if @american_address
+ @american_address = AmericanAddress.new
+ @american_address.street1 = "12 Fancy House"
+ @american_address.street2 = "Bond Street"
+ @american_address.city = "Tampa"
+ @american_address.zip = "45334"
+ @american_address.state = "Florida"
+ @american_address.country = "United States"
+ @american_address
end
end
View
@@ -3,8 +3,14 @@
require "ruby-debug"
require File.dirname(__FILE__) + "/../lib/addresslogic"
-class Address
+class UKAddress
attr_accessor :street1, :street2, :city, :zip, :country
include Addresslogic
apply_addresslogic :fields => [:street1, :street2, [:city, :zip], :country]
+end
+
+class AmericanAddress
+ attr_accessor :street1, :street2, :city, :state, :zip, :country
+ include Addresslogic
+ apply_addresslogic :fields => [:street1, :street2, [:city, [:state, :zip]], :country]
end

0 comments on commit 997bd33

Please sign in to comment.