normalize_digits 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/.gitignore +20 -0
- data/.rspec +3 -0
- data/.rubocop.yml +11 -0
- data/.travis.yml +20 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +174 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/gemfiles/activemodel-6.0.gemfile +7 -0
- data/gemfiles/activemodel-6.1.gemfile +7 -0
- data/gemfiles/activemodel-7.0.gemfile +7 -0
- data/gemfiles/activemodel-7.1.gemfile +7 -0
- data/gemfiles/activemodel-7.2.gemfile +7 -0
- data/gemfiles/activemodel-8.0.gemfile +7 -0
- data/gemfiles/activemodel-main.gemfile +7 -0
- data/lib/normalize_digits/matchers.rb +53 -0
- data/lib/normalize_digits/version.rb +5 -0
- data/lib/normalize_digits.rb +87 -0
- data/normalize_digits.gemspec +38 -0
- data/spec/normalize_digits_spec.rb +147 -0
- data/spec/spec_helper.rb +21 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b71f47e65081d82f5024e730b131c9ba7d2b8e969761f0ec542cc5e5d1f4e96b
|
4
|
+
data.tar.gz: d1e42c7c0c1452c552ca2e8fd8b918bd7e5b41bc6c5ab292651da78601ed2a4e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0f10717beb08f93a12d9c759e2218c5df6df9a381215b0f589af92cdd39e11e52831e312f0beda8a9e0c2bc0a37117dd17a5859a7085989ba740553a73e6de0
|
7
|
+
data.tar.gz: 907e8a91a39c8224c54484e563045582f4db0dcb36b1303c3fdf3de0d4322892df712cab7d8cfbe63c93c95f71f3d80c2d9cf955107f2d85b78f27c33fae5a8f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
rvm:
|
6
|
+
- 2.3.6
|
7
|
+
- 2.4.3
|
8
|
+
- 2.5.0
|
9
|
+
- 2.5.1
|
10
|
+
before_install: gem install bundler -v 1.16.4
|
11
|
+
gemfile:
|
12
|
+
- Gemfile
|
13
|
+
- gemfiles/activemodel-4.2.gemfile
|
14
|
+
- gemfiles/activemodel-5.1.gemfile
|
15
|
+
- gemfiles/activemodel-5.2.gemfile
|
16
|
+
- gemfiles/activemodel-master.gemfile
|
17
|
+
matrix:
|
18
|
+
allow_failures:
|
19
|
+
- gemfile: gemfiles/activemodel-master.gemfile
|
20
|
+
fast_finish: true
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at alyahya@morniksa.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
normalize_digits (0.2.0)
|
5
|
+
activemodel (>= 3.0, < 9.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (7.2.2)
|
11
|
+
actionview (= 7.2.2)
|
12
|
+
activesupport (= 7.2.2)
|
13
|
+
nokogiri (>= 1.8.5)
|
14
|
+
racc
|
15
|
+
rack (>= 2.2.4, < 3.2)
|
16
|
+
rack-session (>= 1.0.1)
|
17
|
+
rack-test (>= 0.6.3)
|
18
|
+
rails-dom-testing (~> 2.2)
|
19
|
+
rails-html-sanitizer (~> 1.6)
|
20
|
+
useragent (~> 0.16)
|
21
|
+
actionview (7.2.2)
|
22
|
+
activesupport (= 7.2.2)
|
23
|
+
builder (~> 3.1)
|
24
|
+
erubi (~> 1.11)
|
25
|
+
rails-dom-testing (~> 2.2)
|
26
|
+
rails-html-sanitizer (~> 1.6)
|
27
|
+
active_attr (0.17.1)
|
28
|
+
actionpack (>= 3.0.2, < 8.1)
|
29
|
+
activemodel (>= 3.0.2, < 8.1)
|
30
|
+
activesupport (>= 3.0.2, < 8.1)
|
31
|
+
activemodel (7.2.2)
|
32
|
+
activesupport (= 7.2.2)
|
33
|
+
activesupport (7.2.2)
|
34
|
+
base64
|
35
|
+
benchmark (>= 0.3)
|
36
|
+
bigdecimal
|
37
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
38
|
+
connection_pool (>= 2.2.5)
|
39
|
+
drb
|
40
|
+
i18n (>= 1.6, < 2)
|
41
|
+
logger (>= 1.4.2)
|
42
|
+
minitest (>= 5.1)
|
43
|
+
securerandom (>= 0.3)
|
44
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
45
|
+
ast (2.4.2)
|
46
|
+
base64 (0.2.0)
|
47
|
+
benchmark (0.4.0)
|
48
|
+
bigdecimal (3.1.8)
|
49
|
+
builder (3.3.0)
|
50
|
+
concurrent-ruby (1.3.4)
|
51
|
+
connection_pool (2.4.1)
|
52
|
+
crass (1.0.6)
|
53
|
+
diff-lcs (1.5.1)
|
54
|
+
drb (2.2.1)
|
55
|
+
erubi (1.13.0)
|
56
|
+
i18n (1.14.6)
|
57
|
+
concurrent-ruby (~> 1.0)
|
58
|
+
io-console (0.7.2)
|
59
|
+
irb (1.14.1)
|
60
|
+
rdoc (>= 4.0.0)
|
61
|
+
reline (>= 0.4.2)
|
62
|
+
json (2.8.2)
|
63
|
+
language_server-protocol (3.17.0.3)
|
64
|
+
logger (1.6.1)
|
65
|
+
loofah (2.23.1)
|
66
|
+
crass (~> 1.0.2)
|
67
|
+
nokogiri (>= 1.12.0)
|
68
|
+
minitest (5.25.1)
|
69
|
+
nokogiri (1.16.7-arm64-darwin)
|
70
|
+
racc (~> 1.4)
|
71
|
+
parallel (1.26.3)
|
72
|
+
parser (3.3.6.0)
|
73
|
+
ast (~> 2.4.1)
|
74
|
+
racc
|
75
|
+
psych (5.2.0)
|
76
|
+
stringio
|
77
|
+
racc (1.8.1)
|
78
|
+
rack (3.1.8)
|
79
|
+
rack-session (2.0.0)
|
80
|
+
rack (>= 3.0.0)
|
81
|
+
rack-test (2.1.0)
|
82
|
+
rack (>= 1.3)
|
83
|
+
rackup (2.2.1)
|
84
|
+
rack (>= 3)
|
85
|
+
rails-dom-testing (2.2.0)
|
86
|
+
activesupport (>= 5.0.0)
|
87
|
+
minitest
|
88
|
+
nokogiri (>= 1.6)
|
89
|
+
rails-html-sanitizer (1.6.0)
|
90
|
+
loofah (~> 2.21)
|
91
|
+
nokogiri (~> 1.14)
|
92
|
+
railties (7.2.2)
|
93
|
+
actionpack (= 7.2.2)
|
94
|
+
activesupport (= 7.2.2)
|
95
|
+
irb (~> 1.13)
|
96
|
+
rackup (>= 1.0.0)
|
97
|
+
rake (>= 12.2)
|
98
|
+
thor (~> 1.0, >= 1.2.2)
|
99
|
+
zeitwerk (~> 2.6)
|
100
|
+
rainbow (3.1.1)
|
101
|
+
rake (13.2.1)
|
102
|
+
rdoc (6.7.0)
|
103
|
+
psych (>= 4.0.0)
|
104
|
+
regexp_parser (2.9.2)
|
105
|
+
reline (0.5.11)
|
106
|
+
io-console (~> 0.5)
|
107
|
+
rspec-core (3.13.2)
|
108
|
+
rspec-support (~> 3.13.0)
|
109
|
+
rspec-expectations (3.13.3)
|
110
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
111
|
+
rspec-support (~> 3.13.0)
|
112
|
+
rspec-mocks (3.13.2)
|
113
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
114
|
+
rspec-support (~> 3.13.0)
|
115
|
+
rspec-rails (7.1.0)
|
116
|
+
actionpack (>= 7.0)
|
117
|
+
activesupport (>= 7.0)
|
118
|
+
railties (>= 7.0)
|
119
|
+
rspec-core (~> 3.13)
|
120
|
+
rspec-expectations (~> 3.13)
|
121
|
+
rspec-mocks (~> 3.13)
|
122
|
+
rspec-support (~> 3.13)
|
123
|
+
rspec-support (3.13.1)
|
124
|
+
rubocop (1.68.0)
|
125
|
+
json (~> 2.3)
|
126
|
+
language_server-protocol (>= 3.17.0)
|
127
|
+
parallel (~> 1.10)
|
128
|
+
parser (>= 3.3.0.2)
|
129
|
+
rainbow (>= 2.2.2, < 4.0)
|
130
|
+
regexp_parser (>= 2.4, < 3.0)
|
131
|
+
rubocop-ast (>= 1.32.2, < 2.0)
|
132
|
+
ruby-progressbar (~> 1.7)
|
133
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
134
|
+
rubocop-ast (1.36.1)
|
135
|
+
parser (>= 3.3.1.0)
|
136
|
+
rubocop-minitest (0.36.0)
|
137
|
+
rubocop (>= 1.61, < 2.0)
|
138
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
139
|
+
rubocop-performance (1.23.0)
|
140
|
+
rubocop (>= 1.48.1, < 2.0)
|
141
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
142
|
+
rubocop-rails (2.27.0)
|
143
|
+
activesupport (>= 4.2.0)
|
144
|
+
rack (>= 1.1)
|
145
|
+
rubocop (>= 1.52.0, < 2.0)
|
146
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
147
|
+
rubocop-rails-omakase (1.0.0)
|
148
|
+
rubocop
|
149
|
+
rubocop-minitest
|
150
|
+
rubocop-performance
|
151
|
+
rubocop-rails
|
152
|
+
ruby-progressbar (1.13.0)
|
153
|
+
securerandom (0.3.2)
|
154
|
+
stringio (3.1.2)
|
155
|
+
thor (1.3.2)
|
156
|
+
tzinfo (2.0.6)
|
157
|
+
concurrent-ruby (~> 1.0)
|
158
|
+
unicode-display_width (2.6.0)
|
159
|
+
useragent (0.16.10)
|
160
|
+
zeitwerk (2.7.1)
|
161
|
+
|
162
|
+
PLATFORMS
|
163
|
+
arm64-darwin-20
|
164
|
+
|
165
|
+
DEPENDENCIES
|
166
|
+
active_attr (~> 0.17.1)
|
167
|
+
bundler (~> 2.0)
|
168
|
+
normalize_digits!
|
169
|
+
rake (~> 13.0)
|
170
|
+
rspec-rails (~> 7.1.0)
|
171
|
+
rubocop-rails-omakase (~> 1.0.0)
|
172
|
+
|
173
|
+
BUNDLED WITH
|
174
|
+
2.5.15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 aalyahya
|
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,43 @@
|
|
1
|
+
# NormalizeDigits
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/normalize_digits`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'normalize_digits'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install normalize_digits
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/normalize_digits. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the NormalizeDigits project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/normalize_digits/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "normalize_digits"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NormalizeDigits
|
4
|
+
# Matcher Module
|
5
|
+
module Matchers
|
6
|
+
# RSpec Examples:
|
7
|
+
#
|
8
|
+
# it { is_expected.to normalize_digits_for(:phone_number) }
|
9
|
+
# it { is_expected.not_to normalize_digits_for(:password) }
|
10
|
+
def normalize_digits_for(attribute)
|
11
|
+
NormalizeDigitsForMatcher.new(attribute)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Matcher class
|
15
|
+
class NormalizeDigitsForMatcher
|
16
|
+
def initialize(attribute)
|
17
|
+
@attribute = attribute
|
18
|
+
@options = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def matches?(subject)
|
22
|
+
return false unless subject.send(@attribute).is_a?(String)
|
23
|
+
|
24
|
+
subject.send("#{@attribute}=", "०۰٠")
|
25
|
+
subject.valid?
|
26
|
+
subject.send(@attribute) == "000"
|
27
|
+
end
|
28
|
+
|
29
|
+
# RSpec 3.x
|
30
|
+
def failure_message
|
31
|
+
"Expected to be englishized from #{@attribute}, but it was not"
|
32
|
+
end
|
33
|
+
|
34
|
+
# RSpec 1.2, 2.x
|
35
|
+
alias failure_message_for_should failure_message
|
36
|
+
|
37
|
+
# RSpec 3.x
|
38
|
+
def failure_message_when_negated
|
39
|
+
"Expected to remain on #{@attribute}, but it was englishized"
|
40
|
+
end
|
41
|
+
|
42
|
+
# RSpec 1.2, 2.x
|
43
|
+
alias failure_message_for_should_not failure_message_when_negated
|
44
|
+
|
45
|
+
# RSpec 1.1
|
46
|
+
alias negative_failure_message failure_message_when_negated
|
47
|
+
|
48
|
+
def description
|
49
|
+
"Englishize non-english digits from #{@attribute}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "normalize_digits/version"
|
4
|
+
require "active_model"
|
5
|
+
|
6
|
+
module ActiveModel::Validations::HelperMethods
|
7
|
+
# Convert non-English digits from model fields to English.
|
8
|
+
def normalize_digits(options = nil)
|
9
|
+
NormalizeDigits.validate_options(options) unless options.nil? || options.keys.empty?
|
10
|
+
|
11
|
+
before_validation do |record|
|
12
|
+
NormalizeDigits.convert(record, options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module NormalizeDigits
|
18
|
+
VALID_OPTIONS = %i[only except].freeze
|
19
|
+
EN_DIGITS = "0123456789"
|
20
|
+
|
21
|
+
AR_DIGITS = "٠١٢٣٤٥٦٧٨٩"
|
22
|
+
HI_DIGITS = "०१२३४५६७८९"
|
23
|
+
UR_DIGITS = "۰۱۲۳۴۵۶۷۸۹"
|
24
|
+
AVAILABLE_LANGUAGES = [AR_DIGITS, HI_DIGITS, UR_DIGITS].freeze
|
25
|
+
|
26
|
+
def self.convert(record_or_attribute, options = nil)
|
27
|
+
if record_or_attribute.respond_to?(:attributes)
|
28
|
+
convert_record(record_or_attribute, options)
|
29
|
+
elsif record_or_attribute.is_a?(String)
|
30
|
+
convert_string(record_or_attribute)
|
31
|
+
else
|
32
|
+
record_or_attribute
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.convert_record(record, options = nil)
|
37
|
+
attributes = narrow(record.attributes, options)
|
38
|
+
|
39
|
+
attributes.each do |attr, value|
|
40
|
+
if value.is_a?(String) && !attr.include?("password")
|
41
|
+
record[attr] = convert_string(value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
record
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.convert_string(value)
|
49
|
+
AVAILABLE_LANGUAGES.each do |lang|
|
50
|
+
begin
|
51
|
+
value = value.try(:force_encoding, "UTF-8")
|
52
|
+
rescue FrozenError
|
53
|
+
value = value.dup.try(:force_encoding, "UTF-8")
|
54
|
+
end
|
55
|
+
|
56
|
+
value = value.try(:tr, lang, EN_DIGITS)
|
57
|
+
end
|
58
|
+
|
59
|
+
value
|
60
|
+
end
|
61
|
+
|
62
|
+
# Necessary because Rails has removed the narrowing of attributes using :only
|
63
|
+
# and :except on Base#attributes
|
64
|
+
def self.narrow(attributes, options = {})
|
65
|
+
return attributes if options.blank?
|
66
|
+
|
67
|
+
if options[:except]
|
68
|
+
except = options[:except]
|
69
|
+
except = Array(except).map(&:to_s)
|
70
|
+
attributes.except(*except)
|
71
|
+
|
72
|
+
elsif options[:only]
|
73
|
+
only = options[:only]
|
74
|
+
only = Array(only).map(&:to_s)
|
75
|
+
attributes.slice(*only)
|
76
|
+
|
77
|
+
else
|
78
|
+
attributes
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.validate_options(options)
|
83
|
+
keys = options.keys
|
84
|
+
invalid_options = (keys - VALID_OPTIONS)
|
85
|
+
raise ArgumentError, "Options does not specify #{invalid_options.inspect}" unless invalid_options.empty?
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "normalize_digits/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "normalize_digits"
|
9
|
+
spec.version = NormalizeDigits::VERSION
|
10
|
+
spec.authors = ["aalyahya"]
|
11
|
+
spec.email = ["abdullah@alyahya.cc"]
|
12
|
+
|
13
|
+
spec.summary = "Non-English digits converter for ActiveModel attributes"
|
14
|
+
spec.description = "An ActiveModel extension that automatically converts all non-English digits to English digits " \
|
15
|
+
"before validation. https://github.com/aalyahya/normalize_digits"
|
16
|
+
spec.homepage = "https://github.com/aalyahya/normalize_digits"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.test_files = `git ls-files -- {test,spec}/*`.split("\n")
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency "activemodel", ">= 3.0", "< 9.0"
|
31
|
+
spec.add_development_dependency "active_attr", "~> 0.17.1"
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
34
|
+
spec.add_development_dependency "rspec-rails", "~> 7.1.0"
|
35
|
+
spec.add_development_dependency "rubocop-rails-omakase", "~> 1.0.0"
|
36
|
+
|
37
|
+
spec.required_ruby_version = ">= 2.7"
|
38
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe NormalizeDigits do
|
4
|
+
include NormalizeDigits::Matchers
|
5
|
+
|
6
|
+
module MockAttributes
|
7
|
+
def self.included(base)
|
8
|
+
base.include ActiveAttr::BasicModel
|
9
|
+
base.include ActiveAttr::TypecastedAttributes
|
10
|
+
base.include ActiveAttr::Serialization
|
11
|
+
base.include ActiveModel::Validations::Callbacks
|
12
|
+
base.attribute :a
|
13
|
+
base.attribute :b
|
14
|
+
base.attribute :c
|
15
|
+
base.attribute :d
|
16
|
+
base.attribute :e
|
17
|
+
base.attribute :f
|
18
|
+
base.attribute :g
|
19
|
+
base.attribute :h
|
20
|
+
base.attribute :access_token
|
21
|
+
base.attribute :current_password
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class EnglishizeAll
|
26
|
+
include MockAttributes
|
27
|
+
normalize_digits
|
28
|
+
end
|
29
|
+
|
30
|
+
class EnglishizeOnlyOne
|
31
|
+
include MockAttributes
|
32
|
+
normalize_digits only: :a
|
33
|
+
end
|
34
|
+
|
35
|
+
class EnglishizeOnlyTwo
|
36
|
+
include MockAttributes
|
37
|
+
normalize_digits only: %i[a b]
|
38
|
+
end
|
39
|
+
|
40
|
+
class EnglishizeExceptOne
|
41
|
+
include MockAttributes
|
42
|
+
normalize_digits except: :a
|
43
|
+
end
|
44
|
+
|
45
|
+
class EnglishizeExceptTwo
|
46
|
+
include MockAttributes
|
47
|
+
normalize_digits except: %i[a b]
|
48
|
+
end
|
49
|
+
|
50
|
+
def instance_for(klass)
|
51
|
+
klass.new(attrs).tap(&:valid?)
|
52
|
+
end
|
53
|
+
|
54
|
+
let(:attrs) { { a: "٩", b: " 1٦1 ", c: :val, d: "०۰٠", e: "", f: 5, g: now, h: nil, current_password: "١٢٣٤٥٦٧٨٩" } }
|
55
|
+
let(:now) { Time.now }
|
56
|
+
|
57
|
+
|
58
|
+
context "main gem / module" do
|
59
|
+
it "must be defined" do
|
60
|
+
expect(Object.const_defined?(:NormalizeDigits)).to be_truthy
|
61
|
+
end
|
62
|
+
|
63
|
+
it "has a version number" do
|
64
|
+
expect(NormalizeDigits::VERSION).not_to be nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "Englishize all attrs" do
|
69
|
+
subject { instance_for(EnglishizeAll) }
|
70
|
+
|
71
|
+
it "should englishize strings" do
|
72
|
+
is_expected.to normalize_digits_for(:a)
|
73
|
+
is_expected.to normalize_digits_for(:b)
|
74
|
+
is_expected.to normalize_digits_for(:d)
|
75
|
+
is_expected.to normalize_digits_for(:e)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should ignore non-string attributes" do
|
79
|
+
is_expected.not_to normalize_digits_for(:c)
|
80
|
+
is_expected.not_to normalize_digits_for(:f)
|
81
|
+
is_expected.not_to normalize_digits_for(:g)
|
82
|
+
is_expected.not_to normalize_digits_for(:h)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should ignore *password* attributes" do
|
86
|
+
is_expected.not_to normalize_digits_for(:current_password)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "Englishize only one attr" do
|
91
|
+
subject { instance_for(EnglishizeOnlyOne) }
|
92
|
+
|
93
|
+
it "should englishize included strings" do
|
94
|
+
is_expected.to normalize_digits_for(:a)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should ignore excluded strings" do
|
98
|
+
is_expected.not_to normalize_digits_for(:b)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "Englishize only two attrs" do
|
103
|
+
subject { instance_for(EnglishizeOnlyTwo) }
|
104
|
+
|
105
|
+
it "should englishize included strings" do
|
106
|
+
is_expected.to normalize_digits_for(:a)
|
107
|
+
is_expected.to normalize_digits_for(:b)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should ignore excluded strings" do
|
111
|
+
is_expected.to_not normalize_digits_for(:d)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "Englishize except one attr" do
|
116
|
+
subject { instance_for(EnglishizeExceptOne) }
|
117
|
+
|
118
|
+
it "should englishize included strings" do
|
119
|
+
is_expected.to normalize_digits_for(:b)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should ignore excluded strings" do
|
123
|
+
is_expected.to_not normalize_digits_for(:a)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "Englishize except two attrs" do
|
128
|
+
subject { instance_for(EnglishizeExceptTwo) }
|
129
|
+
|
130
|
+
it "should englishize included strings" do
|
131
|
+
is_expected.to normalize_digits_for(:d)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should ignore excluded strings" do
|
135
|
+
is_expected.to_not normalize_digits_for(:a)
|
136
|
+
is_expected.to_not normalize_digits_for(:b)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "Englishize with invalid options" do
|
141
|
+
it "should raise ArgumentError exception" do
|
142
|
+
expect {
|
143
|
+
NormalizeDigits.validate_options(invalid_option: :x)
|
144
|
+
}.to raise_error(ArgumentError)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "normalize_digits"
|
5
|
+
require "active_attr"
|
6
|
+
require "normalize_digits/matchers"
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
# Enable flags like --only-failures and --next-failure
|
10
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
11
|
+
|
12
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
13
|
+
config.disable_monkey_patching!
|
14
|
+
|
15
|
+
config.expect_with :rspec do |c|
|
16
|
+
c.syntax = :expect
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Avoid annoying deprecation warning
|
21
|
+
# I18n.enforce_available_locales = true
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normalize_digits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aalyahya
|
8
|
+
bindir: exe
|
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: '3.0'
|
19
|
+
- - "<"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '9.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '3.0'
|
29
|
+
- - "<"
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '9.0'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: active_attr
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - "~>"
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 0.17.1
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - "~>"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.17.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2.0'
|
53
|
+
type: :development
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '2.0'
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rake
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '13.0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '13.0'
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: rspec-rails
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 7.1.0
|
81
|
+
type: :development
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 7.1.0
|
88
|
+
- !ruby/object:Gem::Dependency
|
89
|
+
name: rubocop-rails-omakase
|
90
|
+
requirement: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.0.0
|
95
|
+
type: :development
|
96
|
+
prerelease: false
|
97
|
+
version_requirements: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.0.0
|
102
|
+
description: An ActiveModel extension that automatically converts all non-English
|
103
|
+
digits to English digits before validation. https://github.com/aalyahya/normalize_digits
|
104
|
+
email:
|
105
|
+
- abdullah@alyahya.cc
|
106
|
+
executables: []
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- ".gitignore"
|
111
|
+
- ".rspec"
|
112
|
+
- ".rubocop.yml"
|
113
|
+
- ".travis.yml"
|
114
|
+
- CODE_OF_CONDUCT.md
|
115
|
+
- Gemfile
|
116
|
+
- Gemfile.lock
|
117
|
+
- LICENSE.txt
|
118
|
+
- README.md
|
119
|
+
- Rakefile
|
120
|
+
- bin/console
|
121
|
+
- bin/setup
|
122
|
+
- gemfiles/activemodel-6.0.gemfile
|
123
|
+
- gemfiles/activemodel-6.1.gemfile
|
124
|
+
- gemfiles/activemodel-7.0.gemfile
|
125
|
+
- gemfiles/activemodel-7.1.gemfile
|
126
|
+
- gemfiles/activemodel-7.2.gemfile
|
127
|
+
- gemfiles/activemodel-8.0.gemfile
|
128
|
+
- gemfiles/activemodel-main.gemfile
|
129
|
+
- lib/normalize_digits.rb
|
130
|
+
- lib/normalize_digits/matchers.rb
|
131
|
+
- lib/normalize_digits/version.rb
|
132
|
+
- normalize_digits.gemspec
|
133
|
+
- spec/normalize_digits_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
homepage: https://github.com/aalyahya/normalize_digits
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '2.7'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubygems_version: 3.6.9
|
154
|
+
specification_version: 4
|
155
|
+
summary: Non-English digits converter for ActiveModel attributes
|
156
|
+
test_files:
|
157
|
+
- spec/normalize_digits_spec.rb
|
158
|
+
- spec/spec_helper.rb
|