taiwan_validator 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 703ad9bb354608bfc242004c83265d7766e5906c
4
- data.tar.gz: dcbe80a5adeeba61a91e2cd135b37ce3606b06f2
3
+ metadata.gz: f8274120e717b39b9ef50095a2be264d2dbb39b9
4
+ data.tar.gz: 5856193a1ad24adec02746dd473f3266cc7ec293
5
5
  SHA512:
6
- metadata.gz: 2fc19ae931502832f7649ec716e01dd7f4bd9a5a96ce01b521233f1c3e065c567e10bc78f70bab0c2e250290e60d3abb2abdb4997498f14857e28ff6d67db840
7
- data.tar.gz: 37f9b0afe85037d71fa8c4c0b94ba123fe08161f8739f1f10e2e317c5d034028db8aa5cd9ab12c42fc058f30722af81f8f339c32c516f09ee64b7c866c41e874
6
+ metadata.gz: be76ee5933a03493cb19f2f64b248e78ec2141602805231902e63b3a164e2af940151c1f1d9188f311212c6212e3768eaa3733d46a536289d7c7a0821f629772
7
+ data.tar.gz: 26d6036eba49fd409d15c10792e322ebe8f216e0a4b4d36b15a506a2934d836323d37bc02147e9f85299e7a6afd74c030eed9d4060f289454a383912490ed8f0
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
 
27
27
  ```ruby
28
28
  validates :ubn, ubn: true
29
+ validates :id, id: true
29
30
  ```
30
31
 
31
32
  ## Development
@@ -0,0 +1,32 @@
1
+ class IdValidator < ActiveModel::EachValidator
2
+ MULTIPLIER = [1,9,8,7,6,5,4,3,2,1,1].freeze
3
+ FIRST_LETTER = HashWithIndifferentAccess.new(
4
+ A: 10, B: 11, C: 12, D: 13, E: 14,
5
+ F: 15, G: 16, H: 17, I: 34, J: 18,
6
+ K: 19, M: 21, N: 22, O: 35, P: 23,
7
+ Q: 24, T: 27, U: 28, V: 29, W: 32,
8
+ X: 30, Z: 33
9
+ ).freeze
10
+ DEPRECATED_FIRST_LETTER = HashWithIndifferentAccess.new(
11
+ L: 20, R: 25, S: 26, Y: 31
12
+ ).freeze
13
+ VALID_FIRST_LETTER = FIRST_LETTER.merge(DEPRECATED_FIRST_LETTER).freeze
14
+
15
+ class << self
16
+ def valid?(id)
17
+ id = id.to_s
18
+ return false if id.size != 10 || !(VALID_FIRST_LETTER.keys.include?(id[0])) || (id[1..9] =~ /\A\d+\Z/).nil?
19
+
20
+ digits = (VALID_FIRST_LETTER[id[0]].to_s.chars + id[1..9].to_s.chars).map(&:to_i)
21
+ results = digits.zip(MULTIPLIER).map { |r| r.inject(&:*) }.inject(&:+)
22
+
23
+ results % 10 == 0 ? true : false
24
+ end
25
+ end
26
+
27
+ def validate_each(record, attribute, value)
28
+ unless self.class.valid?(value)
29
+ record.errors.add(attribute, options[:message] || :invalid)
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,8 @@
1
1
  require "taiwan_validator/version"
2
2
  require "active_model"
3
+ require "active_support/hash_with_indifferent_access"
3
4
  require "ubn_validator"
5
+ require "id_validator"
4
6
 
5
7
  module TaiwanValidator
6
8
  end
@@ -1,3 +1,3 @@
1
1
  module TaiwanValidator
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["abookyun@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Collection of useful custom validators in Taiwan for Rails applications}
13
- spec.description = %q{taiwan_validator provides a set of commonly validators for Rails applications}
13
+ spec.description = %q{taiwan_validator provides a set of commonly used validators in Taiwan for Rails applications}
14
14
  spec.homepage = "https://github.com/abookyun/taiwan_validator"
15
15
  spec.license = "MIT"
16
16
 
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency "activemodel", "~> 4.2"
23
+ spec.add_dependency "activesupport", "~> 4.2"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.10"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taiwan_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Yun
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +80,8 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.3'
69
- description: taiwan_validator provides a set of commonly validators for Rails applications
83
+ description: taiwan_validator provides a set of commonly used validators in Taiwan
84
+ for Rails applications
70
85
  email:
71
86
  - abookyun@gmail.com
72
87
  executables: []
@@ -82,6 +97,7 @@ files:
82
97
  - Rakefile
83
98
  - bin/console
84
99
  - bin/setup
100
+ - lib/id_validator.rb
85
101
  - lib/taiwan_validator.rb
86
102
  - lib/taiwan_validator/version.rb
87
103
  - lib/ubn_validator.rb