Class: Phonofy::Model::InstanceDynamicMethods
- Inherits:
-
Module
- Object
- Module
- Phonofy::Model::InstanceDynamicMethods
- Defined in:
- lib/model/instance_dynamic_methods.rb
Constant Summary collapse
- DEFAULT_PHONE_FORMAT =
:national- SUPPORTED_FORMATS =
i[ international national local_number extension full_e164 e164 full_international ].freeze
Instance Method Summary collapse
- #included(base) ⇒ Object
-
#initialize(column_name, options) ⇒ InstanceDynamicMethods
constructor
A new instance of InstanceDynamicMethods.
Constructor Details
#initialize(column_name, options) ⇒ InstanceDynamicMethods
Returns a new instance of InstanceDynamicMethods.
23 24 25 26 27 |
# File 'lib/model/instance_dynamic_methods.rb', line 23 def initialize(column_name, ) super() @column_name = column_name = end |
Instance Method Details
#included(base) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/model/instance_dynamic_methods.rb', line 29 def included(base) column_name = @column_name = [:phonelib] || {} phonelib_validate_enabled = != false = .delete(:validation_options) || {} base.class_eval do if phonelib_validate_enabled validate lambda { validator = Validators::ExtendedPhoneValidator.new(.merge(attributes: [column_name.to_s])) validator.validate_each(self, column_name, send("#{column_name}_before_type_cast")) }, ** end before_save :"#{column_name}_to_e164!", if: :"will_save_change_to_#{column_name}?" define_method(column_name.to_s) do |format = DEFAULT_PHONE_FORMAT| phone = read_attribute(column_name) phone = Phonelib.parse(phone) raise Phonelib::FormatNotSupported, format unless SUPPORTED_FORMATS.include? format phone.send(format) end define_method("#{column_name}_is_valid?") do phone = read_attribute(column_name) phone = Phonelib.parse(phone) phone.valid? end define_method("#{column_name}_object") do phone = read_attribute(column_name) Phonelib.parse(phone) end define_method("#{column_name}_to_e164!") do phone = read_attribute(column_name) write_attribute(column_name, Phonelib.parse(phone).e164) end end end |