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.

#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.

Examples:

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

Instance Method Summary collapse

Instance Method Details

#deserialize(hash) ⇒ PicoPhone::PhoneNumber

Parameters:

  • hash (Hash)

    payload previously produced by #serialize

Returns:

  • (PicoPhone::PhoneNumber)


26
27
28
# File 'lib/pico_phone/rails/serializers/phone_number_serializer.rb', line 26

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

#klassClass

The class this serializer handles -- ActiveJob::Serializers uses it both for dispatch (default #serialize?) and, since ActiveJob 8.1, to build a lookup index, which is why it must be public: earlier ActiveJob versions accepted a private #klass, but 8.1+ warns (and 8.2+ will raise) if it isn't public.

Returns:

  • (Class)


36
37
38
# File 'lib/pico_phone/rails/serializers/phone_number_serializer.rb', line 36

def klass
  PicoPhone::PhoneNumber
end

#serialize(phone_number) ⇒ Hash

Returns JSON-safe payload; ActiveJob merges in its own reserved serializer-identity key.

Parameters:

  • phone_number (PicoPhone::PhoneNumber)

Returns:

  • (Hash)

    JSON-safe payload; ActiveJob merges in its own reserved serializer-identity key



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

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