Module: PhonyRails::Extension::ClassMethods
- Defined in:
- lib/phony_rails.rb
Instance Method Summary collapse
-
#phony_normalize(*attributes) ⇒ Object
Use this method on the class level like: phony_normalize :phone_number, :fax_number, :default_country_code => ‘NL’.
-
#phony_normalized_method(*attributes) ⇒ Object
Usage: phony_normalized_method :fax_number, :default_country_code => ‘US’ Creates a normalized_fax_number method.
Instance Method Details
#phony_normalize(*attributes) ⇒ Object
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!
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/phony_rails.rb', line 98 def phony_normalize(*attributes) = attributes.last.is_a?(Hash) ? attributes.pop : {} .assert_valid_keys :country_number, :default_country_number, :country_code, :default_country_code, :add_plus, :as, :enforce_record_country if [:as].present? raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1 end [:enforce_record_country] = true if [:enforce_record_country].nil? # Add before validation that saves a normalized version of the phone number self.before_validation do set_phony_normalized_numbers(attributes, ) end end |
#phony_normalized_method(*attributes) ⇒ Object
Usage:
phony_normalized_method :fax_number, :default_country_code => 'US'
Creates a normalized_fax_number method.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/phony_rails.rb', line 116 def phony_normalized_method(*attributes) = attributes.last.is_a?(Hash) ? attributes.pop : {} .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 method_defined?(:"normalized_#{attribute}") define_method :"normalized_#{attribute}" do |*args| = args.first || {} raise ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)" if not self.respond_to?(attribute) [:country_code] ||= self.country_code if self.respond_to?(:country_code) PhonyRails.normalize_number(self.send(attribute), .merge()) end end end |