Browse files

* Added :composition_namespace option.

  • Loading branch information...
1 parent 1107a9f commit ec985c63804c9c298379de82d6e917a94aefee89 @binarylogic committed Aug 16, 2009
Showing with 20 additions and 21 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +16 −21 lib/addresslogic.rb
View
4 CHANGELOG.rdoc
@@ -1,3 +1,7 @@
+== 1.1.4
+
+* Added :composition_namespace option.
+
== 1.1.3
* Include into Object so that we can use this in any class we want.
View
37 lib/addresslogic.rb
@@ -1,16 +1,4 @@
-# = 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
-# 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...
+# Provides common methods and tools for using addresses
module Addresslogic
def self.included(base)
base.extend ClassMethods
@@ -19,8 +7,20 @@ def self.included(base)
module ClassMethods
attr_accessor :address_parts_fields
- def apply_addresslogic(args = {})
- self.address_parts_fields = args[:fields] || [:street1, :street2, [:city, [:state, :zip]], :country]
+ # Mixes in useful methods for handling addresses.
+ #
+ # === Options
+ #
+ # * <tt>fields:</tt> array of fields (default: [:street1, :street2, [:city, [:state, :zip]], :country])
+ # * <tt>composition_namespace:</tt> prefixes fields names with this, great for use with composed_of in ActiveRecord.
+ def apply_addresslogic(options = {})
+ n = options[:composition_namespace]
+ self.address_parts_fields = options[:fields] || [
+ "#{n}street1".to_sym,
+ "#{n}street2".to_sym,
+ ["#{n}city".to_sym, ["#{n}state".to_sym, "#{n}zip".to_sym]],
+ "#{n}country".to_sym
+ ]
include Addresslogic::InstanceMethods
end
end
@@ -42,7 +42,7 @@ def address_parts(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
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
+ fields = args[0] || self.class.address_parts_fields
level = args[1] || 0
parts = []
@@ -63,11 +63,6 @@ def address_parts(*args)
parts
end
-
- private
- def address_parts_fields
- self.class.address_parts_fields
- end
end
end

0 comments on commit ec985c6

Please sign in to comment.