lib/phony_rails.rb in phony_rails-0.1.9 vs lib/phony_rails.rb in phony_rails-0.1.10

- old
+ new

@@ -66,11 +66,11 @@ def phony_normalize(*attributes) options = attributes.last.is_a?(Hash) ? attributes.pop : {} options.assert_valid_keys :country_code, :default_country_code, :as if options[:as].present? raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1 - raise ArgumentError, "'#{options[:as]}' is not an attribute on #{self.class}. You might want to use 'phony_normalized_method :#{attributes.first}' (PhonyRails)" if not self.attribute_method?(options[:as]) + raise ArgumentError, "'#{options[:as]}' is not an attribute on #{self.name}. You might want to use 'phony_normalized_method :#{attributes.first}' (PhonyRails)" if not self.attribute_method?(options[:as]) end attributes.each do |attribute| raise ArgumentError, "No attribute #{attribute} found on #{self.name} (PhonyRails)" if not self.attribute_method?(attribute) # Add before validation that saves a normalized version of the phone number self.before_validation do @@ -84,11 +84,12 @@ # Creates a normalized_fax_number method. def phony_normalized_method(*attributes) main_options = attributes.last.is_a?(Hash) ? attributes.pop : {} main_options.assert_valid_keys :country_code, :default_country_code attributes.each do |attribute| - raise StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)" if self.instance_methods.include?(:"normalized_#{attribute}") - define_method :"normalized_#{attribute}" do |options = {}| + raise StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)" if method_defined?(:"normalized_#{attribute}") + define_method :"normalized_#{attribute}" do |*args| + options = args.first || {} raise ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)" if not self.respond_to?(attribute) options[:country_code] ||= self.country_code if self.respond_to?(:country_code) PhonyRails.normalize_number(self.send(attribute), main_options.merge(options)) end end