lib/phony_rails.rb in phony_rails-0.13.0 vs lib/phony_rails.rb in phony_rails-0.13.1
- old
+ new
@@ -25,11 +25,11 @@
# This idea came from:
# http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
def self.normalize_number(number, options = {})
return if number.nil?
original_number = number
- number = number.clone # Just to be sure, we don't want to change the original.
+ number = number.dup # Just to be sure, we don't want to change the original.
number.gsub!(/[^\(\)\d\+]/, '') # Strips weird stuff from the number
return if number.blank?
if _country_number = options[:country_number] || country_number_for(options[:country_code])
options[:add_plus] = true if options[:add_plus].nil?
# (Force) add country_number if missing
@@ -84,19 +84,19 @@
private
# This methods sets the attribute to the normalized version.
# It also adds the country_code (number), eg. 31 for NL numbers.
def set_phony_normalized_numbers(attributes, options = {})
- options = options.clone
- if self.respond_to?(:country_code)
+ options = options.dup
+ if respond_to?(:country_code)
set_country_as = options[:enforce_record_country] ? :country_code : :default_country_code
- options[set_country_as] ||= self.country_code
+ options[set_country_as] ||= country_code
end
attributes.each do |attribute|
attribute_name = options[:as] || attribute
- fail(RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
- new_value= PhonyRails.normalize_number(send(attribute), options)
+ raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
+ new_value = PhonyRails.normalize_number(send(attribute), options)
send("#{attribute_name}=", new_value) if new_value
end
end
end
@@ -126,14 +126,14 @@
# 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|
- fail(StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
+ raise(StandardError, "Instance method normalized_#{attribute} already exists on #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
define_method :"normalized_#{attribute}" do |*args|
options = args.first || {}
- fail(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless self.respond_to?(attribute)
- options[:country_code] ||= country_code if self.respond_to?(:country_code)
+ raise(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless respond_to?(attribute)
+ options[:country_code] ||= country_code if respond_to?(:country_code)
PhonyRails.normalize_number(send(attribute), main_options.merge(options))
end
end
end
end