Class: PicoPhone::Rails::Serializers::PhoneNumberSerializer

Inherits:
ActiveJob::Serializers::ObjectSerializer
  • Object
show all
Defined in:
lib/pico_phone/rails/serializers/phone_number_serializer.rb

Overview

Registered with ActiveJob::Serializers in the railtie, only once ActiveJob itself has loaded. Lets a PhoneNumber survive as a job argument without the caller manually converting to/from a string:

MyJob.perform_later(phone: PicoPhone.parse("+15102745656", "US"))

#to_s alone is enough to round-trip losslessly: E.164 is region-independent for valid numbers, and for invalid/unparseable input #to_s already falls back to the original string, which reparses to the same #to_s/#valid? regardless of region.

Instance Method Summary collapse

Instance Method Details

#deserialize(hash) ⇒ Object



21
22
23
# File 'lib/pico_phone/rails/serializers/phone_number_serializer.rb', line 21

def deserialize(hash)
  PicoPhone.parse(hash["value"])
end

#klassObject

Public since ActiveJob 8.1: Serializers.serializer_for builds a lookup index keyed by this, and warns (raising from 8.2 on) if it's private.



28
29
30
# File 'lib/pico_phone/rails/serializers/phone_number_serializer.rb', line 28

def klass
  PicoPhone::PhoneNumber
end

#serialize(phone_number) ⇒ Object



17
18
19
# File 'lib/pico_phone/rails/serializers/phone_number_serializer.rb', line 17

def serialize(phone_number)
  super("value" => phone_number.to_s)
end