phonelib 0.6.47 → 0.6.48
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/data/extended_data.dat +0 -0
- data/data/phone_data.dat +0 -0
- data/lib/phonelib.rb +5 -1
- data/lib/phonelib/version.rb +1 -1
- data/lib/validators/phone_validator3.rb +130 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ab20ada741b6c807d528b1cd032b4e20ebfbde1bcc8fcd6eeeb7b351ddec3ed
|
4
|
+
data.tar.gz: cc73a6a56939c0ba09f8996fe17a6050db96af0c5e52b6a7b46c8c55bbf9869d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54375aea5dd1cf1ddd0d3c51d7ac9a6db4b745e152ef5d1a11c45571ef7f4bfb579e1021a1a9c13dc83ba77e39cca12e19904aba71f632456d15ec1d8e9ec97a
|
7
|
+
data.tar.gz: 85495f7efe0e2f9da9adbbe9b4b4f2e8219fc5f6842ae473a81626c9f8629557351ff025a98794f95ee79c1d661bf4f39ff5c3f4191db36d21d2c14849252d9c
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Built in integration with JetBrains RubyMine](https://github.com/daddyz/phonelib/blob/master/icon_RubyMine.png?raw=true)](https://www.jetbrains.com/ruby/)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/phonelib.svg)](http://badge.fury.io/rb/phonelib)
|
5
|
-
[![Build Status](https://travis-ci.
|
5
|
+
[![Build Status](https://travis-ci.com/daddyz/phonelib.svg?branch=master)](http://travis-ci.com/daddyz/phonelib)
|
6
6
|
[![](https://codeclimate.com/github/daddyz/phonelib/badges/coverage.svg)](https://codeclimate.com/github/daddyz/phonelib/coverage)
|
7
7
|
[![](https://codeclimate.com/github/daddyz/phonelib/badges/gpa.svg)](https://codeclimate.com/github/daddyz/phonelib)
|
8
8
|
[![Inline docs](http://inch-ci.org/github/daddyz/phonelib.svg?branch=master)](http://inch-ci.org/github/daddyz/phonelib)
|
data/data/extended_data.dat
CHANGED
Binary file
|
data/data/phone_data.dat
CHANGED
Binary file
|
data/lib/phonelib.rb
CHANGED
@@ -12,5 +12,9 @@ module Phonelib
|
|
12
12
|
end
|
13
13
|
|
14
14
|
if defined?(ActiveModel) || defined?(Rails)
|
15
|
-
|
15
|
+
if RUBY_VERSION >= '3.0.0'
|
16
|
+
autoload :PhoneValidator, 'validators/phone_validator3'
|
17
|
+
else
|
18
|
+
autoload :PhoneValidator, 'validators/phone_validator'
|
19
|
+
end
|
16
20
|
end
|
data/lib/phonelib/version.rb
CHANGED
@@ -0,0 +1,130 @@
|
|
1
|
+
# Validator class for phone validations
|
2
|
+
#
|
3
|
+
# ==== Examples
|
4
|
+
#
|
5
|
+
# Validates that attribute is a valid phone number.
|
6
|
+
# If empty value passed for attribute it fails.
|
7
|
+
#
|
8
|
+
# class Phone < ActiveRecord::Base
|
9
|
+
# attr_accessible :number
|
10
|
+
# validates :number, phone: true
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Validates that attribute is a possible phone number.
|
14
|
+
# If empty value passed for attribute it fails.
|
15
|
+
#
|
16
|
+
# class Phone < ActiveRecord::Base
|
17
|
+
# attr_accessible :number
|
18
|
+
# validates :number, phone: { possible: true }
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Validates that attribute is a valid phone number.
|
22
|
+
# Empty value is allowed to be passed.
|
23
|
+
#
|
24
|
+
# class Phone < ActiveRecord::Base
|
25
|
+
# attr_accessible :number
|
26
|
+
# validates :number, phone: { allow_blank: true }
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Validates that attribute is a valid phone number of specified type(s).
|
30
|
+
# It is also possible to check that attribute is a possible number of specified
|
31
|
+
# type(s). Symbol or array accepted.
|
32
|
+
#
|
33
|
+
# class Phone < ActiveRecord::Base
|
34
|
+
# attr_accessible :number, :mobile
|
35
|
+
# validates :number, phone: { types: [:mobile, :fixed], allow_blank: true }
|
36
|
+
# validates :mobile, phone: { possible: true, types: :mobile }
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# validates that phone is valid and it is from specified country or countries
|
40
|
+
#
|
41
|
+
# class Phone < ActiveRecord::Base
|
42
|
+
# attr_accessible :number
|
43
|
+
# validates :number, phone: { countries: [:us, :ca] }
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# Validates that attribute does not include an extension.
|
47
|
+
# The default setting is to allow extensions
|
48
|
+
#
|
49
|
+
# class Phone < ActiveRecord::Base
|
50
|
+
# attr_accessible :number
|
51
|
+
# validates :number, phone: { extensions: false }
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
class PhoneValidator < ActiveModel::EachValidator
|
55
|
+
# Include all core methods
|
56
|
+
include Phonelib::Core
|
57
|
+
|
58
|
+
# Validation method
|
59
|
+
def validate_each(record, attribute, value)
|
60
|
+
return if options[:allow_blank] && value.blank?
|
61
|
+
|
62
|
+
@phone = parse(value, specified_country(record))
|
63
|
+
valid = phone_valid? && valid_types? && valid_country? && valid_extensions?
|
64
|
+
|
65
|
+
record.errors.add(attribute, message, **options) unless valid
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def message
|
71
|
+
options[:message] || :invalid
|
72
|
+
end
|
73
|
+
|
74
|
+
def phone_valid?
|
75
|
+
@phone.send(options[:possible] ? :possible? : :valid?)
|
76
|
+
end
|
77
|
+
|
78
|
+
def valid_types?
|
79
|
+
return true unless options[:types]
|
80
|
+
(phone_types & types).size > 0
|
81
|
+
end
|
82
|
+
|
83
|
+
def valid_country?
|
84
|
+
return true unless options[:countries]
|
85
|
+
(phone_countries & countries).size > 0
|
86
|
+
end
|
87
|
+
|
88
|
+
def valid_extensions?
|
89
|
+
return true if !options.has_key?(:extensions) || options[:extensions]
|
90
|
+
@phone.extension.empty?
|
91
|
+
end
|
92
|
+
|
93
|
+
def specified_country(record)
|
94
|
+
return unless options[:country_specifier]
|
95
|
+
|
96
|
+
if options[:country_specifier].is_a?(Symbol)
|
97
|
+
record.send(options[:country_specifier])
|
98
|
+
else
|
99
|
+
options[:country_specifier].call(record)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# @private
|
104
|
+
def phone_types
|
105
|
+
method = options[:possible] ? :possible_types : :types
|
106
|
+
phone_types = @phone.send(method)
|
107
|
+
if (phone_types & [Phonelib::Core::FIXED_OR_MOBILE]).size > 0
|
108
|
+
phone_types += [Phonelib::Core::FIXED_LINE, Phonelib::Core::MOBILE]
|
109
|
+
end
|
110
|
+
phone_types
|
111
|
+
end
|
112
|
+
|
113
|
+
# @private
|
114
|
+
def phone_countries
|
115
|
+
method = options[:possible] ? :countries : :valid_countries
|
116
|
+
@phone.send(method)
|
117
|
+
end
|
118
|
+
|
119
|
+
# @private
|
120
|
+
def types
|
121
|
+
types = options[:types].is_a?(Array) ? options[:types] : [options[:types]]
|
122
|
+
types.map(&:to_sym)
|
123
|
+
end
|
124
|
+
|
125
|
+
# @private
|
126
|
+
def countries
|
127
|
+
countries = options[:countries].is_a?(Array) ? options[:countries] : [options[:countries]]
|
128
|
+
countries.map { |c| c.to_s.upcase }
|
129
|
+
end
|
130
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phonelib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.48
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vadim Senderovich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/phonelib/version.rb
|
149
149
|
- lib/tasks/phonelib_tasks.rake
|
150
150
|
- lib/validators/phone_validator.rb
|
151
|
+
- lib/validators/phone_validator3.rb
|
151
152
|
homepage: https://github.com/daddyz/phonelib
|
152
153
|
licenses:
|
153
154
|
- MIT
|