pico_phone-rails 0.2.0 → 0.2.1
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a727b58fa9b03d91f9fed795d5f9b4898c8b6c1d416fa67eed38dc881c8a738a
|
|
4
|
+
data.tar.gz: 2727b559da6ec1a963f6b12f032aeeb9458eaacb5522e3ee98aabbb289863c28
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f07be21c54bcd8f6f9f054af69875b7993f4428f6ef57a97d57252fcee3b71f540b1bcae6563568823c98960e4894afded9ec2e243178af10c36ff2a3dccca1d
|
|
7
|
+
data.tar.gz: 5b409b31ddbeaae09328eb1cbf6a98d392ad9cce01d5e047198c96149c6e985b9125baaa02c3557decbd1baacb6fc55e492203f9b9a76becd9386162474bad79
|
data/README.md
CHANGED
|
@@ -78,6 +78,11 @@ bundle exec appraisal install
|
|
|
78
78
|
bundle exec appraisal rake
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
+
## Contributing
|
|
82
|
+
|
|
83
|
+
Bug reports and pull requests are welcome on GitHub — see
|
|
84
|
+
[CONTRIBUTING.md](CONTRIBUTING.md) for setup and guidelines.
|
|
85
|
+
|
|
81
86
|
## License
|
|
82
87
|
|
|
83
88
|
MIT — see [LICENSE.txt](LICENSE.txt).
|
data/lib/phone_validator.rb
CHANGED
|
@@ -6,8 +6,20 @@
|
|
|
6
6
|
# including model's namespace up through Object, same convention the
|
|
7
7
|
# email_validator gem uses.
|
|
8
8
|
#
|
|
9
|
+
# @example
|
|
9
10
|
# validates :phone, phone: { region: "US", allow_blank: true }
|
|
11
|
+
#
|
|
12
|
+
# @example Looser "possible" check instead of full validity
|
|
13
|
+
# validates :phone, phone: { region: "US", possible: true }
|
|
10
14
|
class PhoneValidator < ActiveModel::EachValidator
|
|
15
|
+
# @param record [ActiveModel::Validations]
|
|
16
|
+
# @param attribute [Symbol]
|
|
17
|
+
# @param value [String, PicoPhone::PhoneNumber, nil]
|
|
18
|
+
# @option options [String] :region ISO 3166-1 alpha-2 default region used to interpret national-format input
|
|
19
|
+
# @option options [Boolean] :allow_blank skip validation when +value+ is +nil+ or empty
|
|
20
|
+
# @option options [Boolean] :possible use a looser possible?/possible_for_country? check instead of strict validity
|
|
21
|
+
# @option options [String] :message custom error message, passed through to +errors.add+
|
|
22
|
+
# @return [void]
|
|
11
23
|
def validate_each(record, attribute, value)
|
|
12
24
|
return if options[:allow_blank] && value.blank?
|
|
13
25
|
|
|
@@ -8,6 +8,7 @@ module PicoPhone
|
|
|
8
8
|
# class macro that rewrites the given attributes to E.164 before validation
|
|
9
9
|
# runs, so validators and persisted data see a consistent format.
|
|
10
10
|
#
|
|
11
|
+
# @example
|
|
11
12
|
# class Contact < ApplicationRecord
|
|
12
13
|
# normalize_phone :phone, region: "US"
|
|
13
14
|
# end
|
|
@@ -15,6 +16,13 @@ module PicoPhone
|
|
|
15
16
|
extend ActiveSupport::Concern
|
|
16
17
|
|
|
17
18
|
class_methods do
|
|
19
|
+
# @!method normalize_phone(*attributes, region: nil)
|
|
20
|
+
# Registers a +before_validation+ callback that rewrites each attribute
|
|
21
|
+
# to E.164 when it parses as valid, leaving unparseable input untouched
|
|
22
|
+
# so a validator can flag it.
|
|
23
|
+
# @param attributes [Array<Symbol>] attribute names to normalize
|
|
24
|
+
# @param region [String, nil] ISO 3166-1 alpha-2 default region used to interpret national-format input
|
|
25
|
+
# @return [void]
|
|
18
26
|
def normalize_phone(*attributes, region: nil)
|
|
19
27
|
before_validation do
|
|
20
28
|
attributes.each do |attribute|
|
|
@@ -5,26 +5,34 @@ module PicoPhone
|
|
|
5
5
|
module Serializers
|
|
6
6
|
# Registered with ActiveJob::Serializers in the railtie, only once
|
|
7
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"))
|
|
8
|
+
# argument without the caller manually converting to/from a string.
|
|
11
9
|
#
|
|
12
10
|
# #to_s alone is enough to round-trip losslessly: E.164 is
|
|
13
11
|
# region-independent for valid numbers, and for invalid/unparseable
|
|
14
12
|
# input #to_s already falls back to the original string, which
|
|
15
13
|
# reparses to the same #to_s/#valid? regardless of region.
|
|
14
|
+
#
|
|
15
|
+
# @example
|
|
16
|
+
# MyJob.perform_later(phone: PicoPhone.parse("+15102745656", "US"))
|
|
16
17
|
class PhoneNumberSerializer < ActiveJob::Serializers::ObjectSerializer
|
|
18
|
+
# @param phone_number [PicoPhone::PhoneNumber]
|
|
19
|
+
# @return [Hash] JSON-safe payload; ActiveJob merges in its own reserved serializer-identity key
|
|
17
20
|
def serialize(phone_number)
|
|
18
21
|
super("value" => phone_number.to_s)
|
|
19
22
|
end
|
|
20
23
|
|
|
24
|
+
# @param hash [Hash] payload previously produced by {#serialize}
|
|
25
|
+
# @return [PicoPhone::PhoneNumber]
|
|
21
26
|
def deserialize(hash)
|
|
22
27
|
PicoPhone.parse(hash["value"])
|
|
23
28
|
end
|
|
24
29
|
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
# it
|
|
30
|
+
# The class this serializer handles -- ActiveJob::Serializers uses it
|
|
31
|
+
# both for dispatch (default #serialize?) and, since ActiveJob 8.1, to
|
|
32
|
+
# build a lookup index, which is why it must be public: earlier
|
|
33
|
+
# ActiveJob versions accepted a private #klass, but 8.1+ warns (and
|
|
34
|
+
# 8.2+ will raise) if it isn't public.
|
|
35
|
+
# @return [Class]
|
|
28
36
|
def klass
|
|
29
37
|
PicoPhone::PhoneNumber
|
|
30
38
|
end
|
|
@@ -7,17 +7,25 @@ module PicoPhone
|
|
|
7
7
|
# Registered as `:phone_number` in the railtie. Casts a stored string to a
|
|
8
8
|
# PicoPhone::PhoneNumber on read and serializes back to E.164 on write.
|
|
9
9
|
#
|
|
10
|
+
# @example
|
|
10
11
|
# attribute :phone, :phone_number, region: "US"
|
|
12
|
+
#
|
|
13
|
+
# contact.phone # => #<PicoPhone::PhoneNumber ...>
|
|
14
|
+
# contact.phone.e164 # => "+15102745656"
|
|
11
15
|
class Type < ActiveModel::Type::Value
|
|
16
|
+
# @param region [String, nil] ISO 3166-1 alpha-2 default region used to interpret national-format input
|
|
12
17
|
def initialize(region: nil)
|
|
13
18
|
@region = region
|
|
14
19
|
super()
|
|
15
20
|
end
|
|
16
21
|
|
|
22
|
+
# @return [Symbol] always +:string+ -- the underlying column stores a plain string
|
|
17
23
|
def type
|
|
18
24
|
:string
|
|
19
25
|
end
|
|
20
26
|
|
|
27
|
+
# @param value [String, PicoPhone::PhoneNumber, nil]
|
|
28
|
+
# @return [PicoPhone::PhoneNumber, nil] +nil+ for +nil+ input; never raises on unparseable input
|
|
21
29
|
def cast(value)
|
|
22
30
|
return value if value.is_a?(PicoPhone::PhoneNumber)
|
|
23
31
|
return nil if value.nil?
|
|
@@ -25,6 +33,8 @@ module PicoPhone
|
|
|
25
33
|
PicoPhone.parse(value.to_s, @region)
|
|
26
34
|
end
|
|
27
35
|
|
|
36
|
+
# @param value [String, PicoPhone::PhoneNumber, nil]
|
|
37
|
+
# @return [String, nil] E.164 for a valid number, the original string for an invalid one, +nil+ for +nil+ input
|
|
28
38
|
def serialize(value)
|
|
29
39
|
return nil if value.nil?
|
|
30
40
|
|
|
@@ -32,6 +42,8 @@ module PicoPhone
|
|
|
32
42
|
phone_number.valid? ? phone_number.e164 : phone_number.to_s
|
|
33
43
|
end
|
|
34
44
|
|
|
45
|
+
# @param value [String, nil] raw value read from the database
|
|
46
|
+
# @return [PicoPhone::PhoneNumber, nil]
|
|
35
47
|
def deserialize(value)
|
|
36
48
|
cast(value)
|
|
37
49
|
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.2.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabi Jack
|
|
@@ -52,8 +52,9 @@ dependencies:
|
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '7.0'
|
|
54
54
|
description: 'pico_phone-rails wires the pico_phone gem into Rails: a :phone_number
|
|
55
|
-
ActiveRecord attribute type, a PhoneValidator for ActiveModel validations,
|
|
56
|
-
|
|
55
|
+
ActiveRecord attribute type, a PhoneValidator for ActiveModel validations, a normalize_phone
|
|
56
|
+
class macro that rewrites phone attributes to E.164 before validation, and an ActiveJob
|
|
57
|
+
serializer so a PhoneNumber survives being passed as a job argument.'
|
|
57
58
|
email:
|
|
58
59
|
- gabi@gabijack.com
|
|
59
60
|
executables: []
|
|
@@ -78,6 +79,7 @@ metadata:
|
|
|
78
79
|
source_code_uri: https://github.com/gjack/pico_phone-rails
|
|
79
80
|
changelog_uri: https://github.com/gjack/pico_phone-rails/releases
|
|
80
81
|
bug_tracker_uri: https://github.com/gjack/pico_phone-rails/issues
|
|
82
|
+
documentation_uri: https://rubydoc.info/gems/pico_phone-rails
|
|
81
83
|
rubygems_mfa_required: 'true'
|
|
82
84
|
rdoc_options: []
|
|
83
85
|
require_paths:
|
|
@@ -95,6 +97,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
95
97
|
requirements: []
|
|
96
98
|
rubygems_version: 3.6.9
|
|
97
99
|
specification_version: 4
|
|
98
|
-
summary: 'Rails integration for pico_phone: attribute type, validator,
|
|
99
|
-
|
|
100
|
+
summary: 'Rails integration for pico_phone: attribute type, validator, normalizer,
|
|
101
|
+
and ActiveJob serializer'
|
|
100
102
|
test_files: []
|