pico_phone-rails 0.1.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/lib/phone_validator.rb +30 -0
- data/lib/pico_phone/rails/locale/en.yml +4 -0
- data/lib/pico_phone/rails/normalizer.rb +32 -0
- data/lib/pico_phone/rails/railtie.rb +25 -0
- data/lib/pico_phone/rails/type.rb +40 -0
- data/lib/pico_phone/rails/version.rb +7 -0
- data/lib/pico_phone/rails.rb +14 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a77136e5c4b12dd9a039e2fc5fb3a417bd6eae8e353c937f9c05b8c7b768fea4
|
|
4
|
+
data.tar.gz: 6979e2642a4793889171a5de42a7f5bf209b486a4845488fd296b8af54821425
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8ee3cebc17f0fa5fa98ad427b763b024ad97114738d40e774739a50c04abe2c5c9082177fdefb31bd6e9a1a89ef9d026da6c1a0a7aafbca5c1c30473dd456366
|
|
7
|
+
data.tar.gz: dffc493a5abe2b513629b9fa29cc7ac741d5181f7e128add7d592baeab92e4b3c757f3e9d5ecc2faf82067b76810498f681b15a5d0400dde53e5cd5e0d53d809
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gabi Jack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# pico_phone-rails
|
|
2
|
+
|
|
3
|
+
Rails integration for [`pico_phone`](https://github.com/gjack/pico_phone): an
|
|
4
|
+
ActiveRecord attribute type, an ActiveModel validator, and a before-validation
|
|
5
|
+
normalizer for phone numbers.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "pico_phone-rails"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Validator
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
class Contact < ApplicationRecord
|
|
19
|
+
validates :phone, phone: { region: "US", allow_blank: true }
|
|
20
|
+
end
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Options:
|
|
24
|
+
|
|
25
|
+
- `region:` — ISO 3166-1 alpha-2 default region used to interpret national-format input
|
|
26
|
+
- `allow_blank:` — skip validation when the value is `nil` or empty
|
|
27
|
+
- `possible:` — use a looser "possible" check instead of full validity
|
|
28
|
+
|
|
29
|
+
### Attribute type
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
class Contact < ApplicationRecord
|
|
33
|
+
attribute :phone, :phone_number, region: "US"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
contact.phone # => #<PicoPhone::PhoneNumber ...>
|
|
37
|
+
contact.phone.e164 # => "+15102745656"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Reads deserialize to a `PicoPhone::PhoneNumber`; writes serialize back to E.164
|
|
41
|
+
when valid. `nil` and unparseable input survive the round-trip without raising.
|
|
42
|
+
|
|
43
|
+
### Normalizer
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class Contact < ApplicationRecord
|
|
47
|
+
normalize_phone :phone, region: "US"
|
|
48
|
+
end
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
A `before_validation` callback that rewrites the column to E.164 when it parses
|
|
52
|
+
as valid, so formatting noise ("(510) 274-5656") is stripped before the
|
|
53
|
+
validator runs. Pairs naturally with the validator above.
|
|
54
|
+
|
|
55
|
+
## Development
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
bundle install
|
|
59
|
+
bundle exec rspec
|
|
60
|
+
bundle exec rubocop
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The Rails/Ruby support matrix is exercised via [Appraisal](https://github.com/thoughtbot/appraisal):
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
bundle exec appraisal install
|
|
67
|
+
bundle exec appraisal rake
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT — see [LICENSE.txt](LICENSE.txt).
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Defined at the top level (not under PicoPhone::Rails) so ActiveModel's
|
|
4
|
+
# validator lookup resolves `validates :phone, phone: { ... }` to this class —
|
|
5
|
+
# EachValidator constantizes "#{key.to_s.camelize}Validator" against the
|
|
6
|
+
# including model's namespace up through Object, same convention the
|
|
7
|
+
# email_validator gem uses.
|
|
8
|
+
#
|
|
9
|
+
# validates :phone, phone: { region: "US", allow_blank: true }
|
|
10
|
+
class PhoneValidator < ActiveModel::EachValidator
|
|
11
|
+
def validate_each(record, attribute, value)
|
|
12
|
+
return if options[:allow_blank] && value.blank?
|
|
13
|
+
|
|
14
|
+
return if phone_valid?(value.to_s)
|
|
15
|
+
|
|
16
|
+
record.errors.add(attribute, :invalid_phone, **options.slice(:message))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def phone_valid?(string)
|
|
22
|
+
region = options[:region]
|
|
23
|
+
|
|
24
|
+
if options[:possible]
|
|
25
|
+
region ? PicoPhone.possible_for_country?(string, region) : PicoPhone.possible?(string)
|
|
26
|
+
else
|
|
27
|
+
region ? PicoPhone.valid_for_country?(string, region) : PicoPhone.valid?(string)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
|
|
5
|
+
module PicoPhone
|
|
6
|
+
module Rails
|
|
7
|
+
# Included into ActiveRecord::Base by the railtie. Adds a `normalize_phone`
|
|
8
|
+
# class macro that rewrites the given attributes to E.164 before validation
|
|
9
|
+
# runs, so validators and persisted data see a consistent format.
|
|
10
|
+
#
|
|
11
|
+
# class Contact < ApplicationRecord
|
|
12
|
+
# normalize_phone :phone, region: "US"
|
|
13
|
+
# end
|
|
14
|
+
module Normalizer
|
|
15
|
+
extend ActiveSupport::Concern
|
|
16
|
+
|
|
17
|
+
class_methods do
|
|
18
|
+
def normalize_phone(*attributes, region: nil)
|
|
19
|
+
before_validation do
|
|
20
|
+
attributes.each do |attribute|
|
|
21
|
+
value = public_send(attribute)
|
|
22
|
+
next if value.blank?
|
|
23
|
+
|
|
24
|
+
phone_number = PicoPhone.parse(value.to_s, region)
|
|
25
|
+
public_send("#{attribute}=", phone_number.e164) if phone_number.valid?
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module PicoPhone
|
|
6
|
+
module Rails
|
|
7
|
+
class Railtie < ::Rails::Railtie
|
|
8
|
+
initializer "pico_phone_rails.type" do
|
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
|
10
|
+
ActiveRecord::Type.register(:phone_number, PicoPhone::Rails::Type)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
initializer "pico_phone_rails.normalizer" do
|
|
15
|
+
ActiveSupport.on_load(:active_record) do
|
|
16
|
+
include PicoPhone::Rails::Normalizer
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
initializer "pico_phone_rails.i18n" do |app|
|
|
21
|
+
app.config.i18n.load_path += Dir[File.expand_path("locale/*.yml", __dir__)]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_model"
|
|
4
|
+
|
|
5
|
+
module PicoPhone
|
|
6
|
+
module Rails
|
|
7
|
+
# Registered as `:phone_number` in the railtie. Casts a stored string to a
|
|
8
|
+
# PicoPhone::PhoneNumber on read and serializes back to E.164 on write.
|
|
9
|
+
#
|
|
10
|
+
# attribute :phone, :phone_number, region: "US"
|
|
11
|
+
class Type < ActiveModel::Type::Value
|
|
12
|
+
def initialize(region: nil)
|
|
13
|
+
@region = region
|
|
14
|
+
super()
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def type
|
|
18
|
+
:string
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def cast(value)
|
|
22
|
+
return value if value.is_a?(PicoPhone::PhoneNumber)
|
|
23
|
+
return nil if value.nil?
|
|
24
|
+
|
|
25
|
+
PicoPhone.parse(value.to_s, @region)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def serialize(value)
|
|
29
|
+
return nil if value.nil?
|
|
30
|
+
|
|
31
|
+
phone_number = value.is_a?(PicoPhone::PhoneNumber) ? value : PicoPhone.parse(value.to_s, @region)
|
|
32
|
+
phone_number.valid? ? phone_number.e164 : phone_number.to_s
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def deserialize(value)
|
|
36
|
+
cast(value)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pico_phone"
|
|
4
|
+
require "pico_phone/rails/version"
|
|
5
|
+
require "phone_validator"
|
|
6
|
+
require "pico_phone/rails/type"
|
|
7
|
+
require "pico_phone/rails/normalizer"
|
|
8
|
+
require "pico_phone/rails/railtie" if defined?(Rails::Railtie)
|
|
9
|
+
|
|
10
|
+
module PicoPhone
|
|
11
|
+
module Rails
|
|
12
|
+
class Error < StandardError; end
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pico_phone-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gabi Jack
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activemodel
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '7.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '7.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: pico_phone
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.6'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.6'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: railties
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '7.0'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '7.0'
|
|
54
|
+
description: 'pico_phone-rails wires the pico_phone gem into Rails: a :phone_number
|
|
55
|
+
ActiveRecord attribute type, a PhoneValidator for ActiveModel validations, and a
|
|
56
|
+
normalize_phone class macro that rewrites phone attributes to E.164 before validation.'
|
|
57
|
+
email:
|
|
58
|
+
- gabi@gabijack.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- LICENSE.txt
|
|
64
|
+
- README.md
|
|
65
|
+
- lib/phone_validator.rb
|
|
66
|
+
- lib/pico_phone/rails.rb
|
|
67
|
+
- lib/pico_phone/rails/locale/en.yml
|
|
68
|
+
- lib/pico_phone/rails/normalizer.rb
|
|
69
|
+
- lib/pico_phone/rails/railtie.rb
|
|
70
|
+
- lib/pico_phone/rails/type.rb
|
|
71
|
+
- lib/pico_phone/rails/version.rb
|
|
72
|
+
homepage: https://github.com/gjack/pico_phone-rails
|
|
73
|
+
licenses:
|
|
74
|
+
- MIT
|
|
75
|
+
metadata:
|
|
76
|
+
homepage_uri: https://github.com/gjack/pico_phone-rails
|
|
77
|
+
source_code_uri: https://github.com/gjack/pico_phone-rails
|
|
78
|
+
changelog_uri: https://github.com/gjack/pico_phone-rails/releases
|
|
79
|
+
bug_tracker_uri: https://github.com/gjack/pico_phone-rails/issues
|
|
80
|
+
rubygems_mfa_required: 'true'
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: 3.1.0
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
requirements: []
|
|
95
|
+
rubygems_version: 3.6.9
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: 'Rails integration for pico_phone: attribute type, validator, and normalizer
|
|
98
|
+
for phone numbers'
|
|
99
|
+
test_files: []
|