lib/phony_rails.rb in phony_rails-0.14.11 vs lib/phony_rails.rb in phony_rails-0.14.12
- old
+ new
@@ -44,22 +44,22 @@
# :extension => Include the extension. (default: true)
# 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 = {}, current_instance = nil)
return if number.nil?
+
original_number = number
number = number.dup # Just to be sure, we don't want to change the original.
number, ext = extract_extension(number)
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
# NOTE: do we need to force adding country code? Otherwise we can share logic with next block
- if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
- number = "#{_country_number}#{number}"
- end
+ number = "#{_country_number}#{number}" if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
elsif _default_country_number = extract_default_country_number(options, current_instance)
options[:add_plus] = true if options[:add_plus].nil?
number = normalize_number_default_country(number, _default_country_number)
end
normalized_number = Phony.normalize(number)
@@ -99,24 +99,27 @@
# Returns the country dail code (eg. '31') for a number (eg. +31612341234).
# Should probably be named 'country_number_from_number'.
def self.country_code_from_number(number)
return nil unless Phony.plausible?(number)
+
Phony.split(Phony.normalize(number)).first
end
# Returns the country (eg. 'NL') for a number (eg. +31612341234).
def self.country_from_number(number)
return nil unless Phony.plausible?(number)
+
country_codes_hash.select { |_country, hash| hash['country_code'] == country_code_from_number(number) }.keys[0]
end
# Wrapper for Phony.plausible?. Takes the same options as #normalize_number.
# NB: This method calls #normalize_number and passes _options_ directly to that method.
# It uses the 'cc' option for Phony. This was a required param before?
def self.plausible_number?(number, options = {})
return false if number.blank?
+
number = extract_extension(number).first
number = normalize_number(number, options)
country_number = options[:country_number] || country_number_for(options[:country_code]) ||
country_code_from_number(number) ||
options[:default_country_number] || country_number_for(options[:default_country_code]) ||
@@ -124,14 +127,15 @@
Phony.plausible? number, cc: country_number
rescue StandardError
false
end
- COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i
+ COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i.freeze
def self.extract_extension(number_and_ext)
return [nil, nil] if number_and_ext.nil?
+
subbed = number_and_ext.sub(COMMON_EXTENSIONS, '')
[subbed, Regexp.last_match(2)]
end
def self.format_extension(number, ext)
@@ -154,12 +158,14 @@
options[set_country_as] ||= country_code
end
attributes.each do |attribute|
attribute_name = options[:as] || attribute
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
+
+ cache_original_attribute(current_instance, attribute) if options[:normalize_when_valid]
new_value = PhonyRails.normalize_number(send(attribute), options, current_instance)
- send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
+ current_instance.public_send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
end
end
def assign_values_for_phony_symbol_options(options)
symbol_options = %i[country_number default_country_number country_code default_country_code]
@@ -167,19 +173,37 @@
options[option] = send(options[option]) if options[option].is_a?(Symbol)
end
end
end
+ def cache_original_attribute(current_instance, attribute)
+ current_instance.define_singleton_method("#{attribute}_original=") { |value| @original = value }
+ current_instance.define_singleton_method("#{attribute}_original") { @original }
+ current_instance.public_send("#{attribute}_original=", current_instance.public_send(attribute.to_s))
+ end
+
module ClassMethods
+ PHONY_RAILS_COLLECTION_VALID_KEYS = %i[
+ add_plus
+ as
+ country_code
+ country_number
+ default_country_code
+ default_country_number
+ enforce_record_country
+ if
+ normalize_when_valid
+ unless
+ ].freeze
# Use this method on the class level like:
# phony_normalize :phone_number, :fax_number, :default_country_code => 'NL'
#
# It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure
# you've geocoded before calling this method!
def phony_normalize(*attributes)
options = attributes.last.is_a?(Hash) ? attributes.pop : {}
- options.assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country, :if, :unless
+ options.assert_valid_keys(*PHONY_RAILS_COLLECTION_VALID_KEYS)
if options[:as].present?
raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1
end
options[:enforce_record_country] = true if options[:enforce_record_country].nil?
@@ -198,13 +222,15 @@
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 #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
+
define_method :"normalized_#{attribute}" do |*args|
options = main_options.merge(args.first || {})
assign_values_for_phony_symbol_options(options)
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), options)
end
end
end