roshi 0.4.0 → 0.5.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: b1b243c0594115e764884a75140bb7bc99cf4b66
4
- data.tar.gz: 1f0ac997d40fac5c327f29507e2e8ed981b3e47e
3
+ metadata.gz: 619ef8d2899a55d75de84d88ac85bbe7eb3f4ad3
4
+ data.tar.gz: 47ddfc446d64e0334ba3725da5347bd82b0485f2
5
5
  SHA512:
6
- metadata.gz: ee13cbf189fa64f70ae5fe326ff838b7064db46b76372ebf68330971c70ae7f062833941b9b20836176dfbc5c107d7c610b3dcd4205b2e2bfd5e765cc8096685
7
- data.tar.gz: f62ab9a5f3ac21c34f176e32dab3f59b2658820fb01644af3b6b03c3a544bd093b112186ac8ac384a86462d05c97981b7bfa4898345cdda276e60fd2f5aa39fc
6
+ metadata.gz: 7c9f1a79932a06bb913d26ff64892cc8f52e0bcb253b50269e5af03fb655f801fc3c3f57db85f2c5f95cd378a75395206e9d84c33edbfaf7aa92640379856a07
7
+ data.tar.gz: 2b985a3248e924e490585022bdfe32a574486789f208d5f2e3a469d799b82a369197f1e0936dde7a20b689aa726925678175c72b08835cf73adc4420195d4b13
data/README.md CHANGED
@@ -27,6 +27,7 @@ Or install it yourself as:
27
27
  class TestModel
28
28
  validates :email, email: true
29
29
  validates :zip_code, zip_code: true # zip_code: { hiphenation: true }
30
+ validates :phone_number, phone_number: true # phone_number: { hiphenation: true }
30
31
  validates :hiragana, hiragana: true
31
32
  validates :version_number, version_number: true
32
33
  end
data/lib/roshi.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'active_model'
2
2
  require 'roshi/version'
3
- %w(email zip_code hiragana version_number).each do |validator_name|
3
+ %w(email zip_code phone_number hiragana version_number).each do |validator_name|
4
4
  require "roshi/active_model/validations/#{validator_name}_validator"
5
5
  end
@@ -0,0 +1,26 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class PhoneNumberValidator < EachValidator
4
+ REGEXES = {
5
+ default: /\A\d{2,4}\-?\d{2,4}\-?\d{4}\z/i,
6
+ with_hiphenation: /\A\d{2,4}\-\d{2,4}\-\d{4}\z/i,
7
+ without_hiphenation: /\A\d{6,12}\z/i
8
+ }
9
+
10
+ def validate_each(record, attribute, value)
11
+ regexp = case
12
+ when options[:hiphenation].nil?
13
+ REGEXES[:default]
14
+ when options[:hiphenation]
15
+ REGEXES[:with_hiphenation]
16
+ when options[:hiphenation] == false
17
+ REGEXES[:without_hiphenation]
18
+ end
19
+
20
+ unless value =~ regexp
21
+ record.errors.add(attribute, options[:message] || I18n.t('errors.messages.invalid'))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/roshi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Roshi
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roshi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kawahiro311
@@ -98,6 +98,7 @@ files:
98
98
  - lib/roshi.rb
99
99
  - lib/roshi/active_model/validations/email_validator.rb
100
100
  - lib/roshi/active_model/validations/hiragana_validator.rb
101
+ - lib/roshi/active_model/validations/phone_number_validator.rb
101
102
  - lib/roshi/active_model/validations/version_number_validator.rb
102
103
  - lib/roshi/active_model/validations/zip_code_validator.rb
103
104
  - lib/roshi/version.rb