Class: PicoPhone::Rails::Type
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- PicoPhone::Rails::Type
- Defined in:
- lib/pico_phone/rails/type.rb
Overview
Registered as :phone_number in the railtie. Casts a stored string to a
PicoPhone::PhoneNumber on read and serializes back to E.164 on write.
attribute :phone, :phone_number, region: "US"
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(region: nil) ⇒ Type
constructor
A new instance of Type.
- #serialize(value) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(region: nil) ⇒ Type
Returns a new instance of Type.
12 13 14 15 |
# File 'lib/pico_phone/rails/type.rb', line 12 def initialize(region: nil) @region = region super() end |
Instance Method Details
#cast(value) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/pico_phone/rails/type.rb', line 21 def cast(value) return value if value.is_a?(PicoPhone::PhoneNumber) return nil if value.nil? PicoPhone.parse(value.to_s, @region) end |
#deserialize(value) ⇒ Object
35 36 37 |
# File 'lib/pico_phone/rails/type.rb', line 35 def deserialize(value) cast(value) end |
#serialize(value) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/pico_phone/rails/type.rb', line 28 def serialize(value) return nil if value.nil? phone_number = value.is_a?(PicoPhone::PhoneNumber) ? value : PicoPhone.parse(value.to_s, @region) phone_number.valid? ? phone_number.e164 : phone_number.to_s end |
#type ⇒ Object
17 18 19 |
# File 'lib/pico_phone/rails/type.rb', line 17 def type :string end |