pico_phone-rails 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -2
- data/lib/pico_phone/rails/railtie.rb +7 -0
- data/lib/pico_phone/rails/serializers/phone_number_serializer.rb +34 -0
- data/lib/pico_phone/rails/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 84738b06cba145a9e86bedc83516cd76511f8eeb275157a6807b22bc8b646c99
|
|
4
|
+
data.tar.gz: be4865cf109ea61e834dc4f69d24c7937de912872915bc7014947d12f53f2b3c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d13dae58efa40a2d05affceb83b3da6c2b5f15576c9ee45c07856a1e0f31c83d7b483f2abed929ba1b205f6b7534cd615d0a3edb2958a39cd6e5a4741cbfb581
|
|
7
|
+
data.tar.gz: 1636311abdb2f94b9e7adac696d3ef6f13bcc7f3d840a8d40c935e4a9d6d498559c6df830dd1e11660d0c2ce455128864b930a1719815d6caf09450265850164
|
data/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# pico_phone-rails
|
|
2
2
|
|
|
3
3
|
Rails integration for [`pico_phone`](https://github.com/gjack/pico_phone): an
|
|
4
|
-
ActiveRecord attribute type, an ActiveModel validator,
|
|
5
|
-
normalizer for phone numbers.
|
|
4
|
+
ActiveRecord attribute type, an ActiveModel validator, a before-validation
|
|
5
|
+
normalizer, and an ActiveJob serializer for phone numbers.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -52,6 +52,17 @@ A `before_validation` callback that rewrites the column to E.164 when it parses
|
|
|
52
52
|
as valid, so formatting noise ("(510) 274-5656") is stripped before the
|
|
53
53
|
validator runs. Pairs naturally with the validator above.
|
|
54
54
|
|
|
55
|
+
### ActiveJob serializer
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
MyJob.perform_later(phone: PicoPhone.parse("+15102745656", "US"))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A `PicoPhone::PhoneNumber` passed as a job argument survives the round trip
|
|
62
|
+
through your queue backend (Sidekiq, Solid Queue, GoodJob, etc.) without
|
|
63
|
+
manually converting to/from a string — `perform` receives back a real
|
|
64
|
+
`PhoneNumber`, not the E.164 string it was serialized as.
|
|
65
|
+
|
|
55
66
|
## Development
|
|
56
67
|
|
|
57
68
|
```bash
|
|
@@ -17,6 +17,13 @@ module PicoPhone
|
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
initializer "pico_phone_rails.active_job_serializer" do
|
|
21
|
+
ActiveSupport.on_load(:active_job) do
|
|
22
|
+
require "pico_phone/rails/serializers/phone_number_serializer"
|
|
23
|
+
ActiveJob::Serializers.add_serializers(PicoPhone::Rails::Serializers::PhoneNumberSerializer)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
20
27
|
initializer "pico_phone_rails.i18n" do |app|
|
|
21
28
|
app.config.i18n.load_path += Dir[File.expand_path("locale/*.yml", __dir__)]
|
|
22
29
|
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PicoPhone
|
|
4
|
+
module Rails
|
|
5
|
+
module Serializers
|
|
6
|
+
# Registered with ActiveJob::Serializers in the railtie, only once
|
|
7
|
+
# ActiveJob itself has loaded. Lets a PhoneNumber survive as a job
|
|
8
|
+
# argument without the caller manually converting to/from a string:
|
|
9
|
+
#
|
|
10
|
+
# MyJob.perform_later(phone: PicoPhone.parse("+15102745656", "US"))
|
|
11
|
+
#
|
|
12
|
+
# #to_s alone is enough to round-trip losslessly: E.164 is
|
|
13
|
+
# region-independent for valid numbers, and for invalid/unparseable
|
|
14
|
+
# input #to_s already falls back to the original string, which
|
|
15
|
+
# reparses to the same #to_s/#valid? regardless of region.
|
|
16
|
+
class PhoneNumberSerializer < ActiveJob::Serializers::ObjectSerializer
|
|
17
|
+
def serialize(phone_number)
|
|
18
|
+
super("value" => phone_number.to_s)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def deserialize(hash)
|
|
22
|
+
PicoPhone.parse(hash["value"])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Public since ActiveJob 8.1: Serializers.serializer_for builds a
|
|
26
|
+
# lookup index keyed by this, and warns (raising from 8.2 on) if
|
|
27
|
+
# it's private.
|
|
28
|
+
def klass
|
|
29
|
+
PicoPhone::PhoneNumber
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pico_phone-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabi Jack
|
|
@@ -67,6 +67,7 @@ files:
|
|
|
67
67
|
- lib/pico_phone/rails/locale/en.yml
|
|
68
68
|
- lib/pico_phone/rails/normalizer.rb
|
|
69
69
|
- lib/pico_phone/rails/railtie.rb
|
|
70
|
+
- lib/pico_phone/rails/serializers/phone_number_serializer.rb
|
|
70
71
|
- lib/pico_phone/rails/type.rb
|
|
71
72
|
- lib/pico_phone/rails/version.rb
|
|
72
73
|
homepage: https://github.com/gjack/pico_phone-rails
|