Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now you can specify your own fields using a 2 dimensional array. Test…

…s updated to work. This is mostly for UK people.

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
commit 3e14effc9a7e62ed51068e48db3f7a433d31e85a 1 parent 9ce1cc3
Mooktakim Ahmed mooktakim authored committed
Showing with 89 additions and 48 deletions.
  1. +72 −28 lib/addresslogic.rb
  2. +15 −19 test/test_addresslogic.rb
  3. +2 −1  test/test_helper.rb
100 lib/addresslogic.rb
View
@@ -1,42 +1,86 @@
+require 'activerecord'
+
# = Address Logic
#
# This is a simple module that you can include into any classm as long as it has a street1, street2, city, state, zip, and country (optional)
# methods. Just include it into your class like so:
#
# class Address
-# include AddressLogic
+# apply_addresslogic :fields => [:street1, :street2, :city, [:state, :zip], :country]
# end
#
+# The above will return:
+# ["Street1", "Street2", "City", "State Zip", "Country"]
+#
# This adds a sigle method: address_parts. More on this method below...
module Addresslogic
- # Returns the parts of an address in an array. Example:
- #
- # ["Street1", "Street2", "City, State, Zip", "Country"]
- #
- # This makes displaying addresses on your view pretty simple:
- #
- # address.address_parts.join("<br />")
- #
- # === Options
- #
- # * <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 = {})
- options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
- options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ attr_accessor :address_parts_fields
- parts = {}
- [:street1, :street2, :city, :state, :zip, :country].each do |part|
- 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))
- parts[part] = value
+ def apply_addresslogic(args = {})
+ # Is there a better way to do this??
+ class_eval <<-RUBY
+ def address_parts_fields
+ #{args[:fields].inspect}
+ end
+ RUBY
+ include Addresslogic::InstanceMethods
end
+ end
+
+ module InstanceMethods
+ # Returns the parts of an address in an array. Example:
+ #
+ # ["Street1", "Street2", "City", "State Zip", "Country"]
+ #
+ # This makes displaying addresses on your view pretty simple:
+ #
+ # address.address_parts.join("<br />")
+ #
+ # === Options
+ #
+ # * <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 = {})
+ options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
+ options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
- state_parts = [parts[:state], parts[:zip]].compact.join(" ")
- state_parts = nil if state_parts.strip == ""
- city_parts = [parts[:city], state_parts].compact.join(", ")
- city_parts = nil if city_parts.strip == ""
- [parts[:street1], parts[:street2], city_parts, parts[:country]].compact
+ 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?
+ 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))
+ 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
+ end
end
-end
+end
+
+ActiveRecord::Base.send(:include, Addresslogic)
34 test/test_addresslogic.rb
View
@@ -2,44 +2,40 @@
class TestAddresslogix < Test::Unit::TestCase
def test_address_parts
- assert_equal ["Street1", "Street2", "City, State 22933", "United States of America"], address.address_parts
+ assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], address.address_parts
address.street2 = ""
- assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts
-
- address.state = ""
- assert_equal ["Street1", "City, 22933", "United States of America"], address.address_parts
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts
address.country = ""
- assert_equal ["Street1", "City, 22933"], address.address_parts
+ assert_equal ["12 Fancy House", "London W1 8AJ"], address.address_parts
address.city = ""
- assert_equal ["Street1", "22933"], address.address_parts
+ assert_equal ["12 Fancy House", "W1 8AJ"], address.address_parts
address.street1 = ""
- assert_equal ["22933"], address.address_parts
+ assert_equal ["W1 8AJ"], address.address_parts
- address.zip = ""
+ address.postcode = ""
assert_equal [], address.address_parts
end
def test_options
- assert_equal ["Street2"], address.address_parts(:only => :street2)
- assert_equal ["Street1", "Street2"], address.address_parts(:only => [:street1, :street2])
- assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts(:except => :street2)
- assert_equal ["City, State 22933", "United States of America"], address.address_parts(:except => [:street1, :street2])
+ 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])
end
private
def address
return @address if @address
@address = Address.new
- @address.street1 = "Street1"
- @address.street2 = "Street2"
- @address.city = "City"
- @address.state = "State"
- @address.zip = "22933"
- @address.country = "United States of America"
+ @address.street1 = "12 Fancy House"
+ @address.street2 = "Bond Street"
+ @address.city = "London"
+ @address.postcode = "W1 8AJ"
+ @address.country = "United Kingdom"
@address
end
end
3  test/test_helper.rb
View
@@ -4,6 +4,7 @@
require File.dirname(__FILE__) + "/../lib/addresslogic"
class Address
- attr_accessor :street1, :street2, :city, :state, :zip, :country
+ attr_accessor :street1, :street2, :city, :postcode, :country
include Addresslogic
+ apply_addresslogic :fields => [:street1, :street2, [:city, :postcode], :country]
end
Please sign in to comment.
Something went wrong with that request. Please try again.