validate_japanese 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cec650ea68939253db44b7af6d4d773ee238480fcbcbc72623a3f704967a76e5
4
- data.tar.gz: 8ba384ccd7964eee845155f44cba576d6624338ab3e44412b2b2277288790411
3
+ metadata.gz: f23f115e40c7bd4b713fe9d9c2ef28517cc6ad764c8599492000202808e379f6
4
+ data.tar.gz: ba56c45e85a420e57fd5e838a0e6a799325149029a1d32fff18ebcad42f5a4fe
5
5
  SHA512:
6
- metadata.gz: 71dd21a5408d9024c19bddfafff37e68f46e5d6e821e0553d71773cc3db81acff9f62cbe397b9c9f4cdc172ed496001b22753be1a7aeef3c0f44029532b65127
7
- data.tar.gz: 5cd4c91cf3d8a74521e203747e4f90f0dd72f80e2923d34f1b8ab48945a749e331962372f00b4854d0306e85dc0210adc5588725e57d50c55a10626d783b6a81
6
+ metadata.gz: bc166b08fe163b7a25432e9f0cdd4824b438b159b79a0711bd566be83900c3410c435bd0824f61b791944ad9cc4ef12728f6fef2c3d11432eabe265ac84bd9bd
7
+ data.tar.gz: 9bc69cdcfa60c1a15429fd82ca9bbb70b355a1d9afb28aabcebb8aa32af8f94deab09792e913aa8c2fb4d4b77145c755fc5cea80d2537717b8240ec0c04cd3e4
data/.travis.yml CHANGED
@@ -12,10 +12,6 @@ rvm:
12
12
  env:
13
13
  global:
14
14
  - TRAVIS=true
15
- matrix:
16
- - DB=mysql
17
- - DB=postgres
18
- - DB=sqlite
19
15
 
20
16
  # We want to use `sudo: false` because the container infrastructure is supposed
21
17
  # to be faster, but Travis is having issues with containers lately ..
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate_japanese (0.2.0)
4
+ validate_japanese (0.3.0)
5
5
  activerecord (>= 4.2, < 6.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -16,13 +16,34 @@ gem 'validate_japanese'
16
16
  Add a validation rule to your model.
17
17
 
18
18
  ```ruby
19
- validates :name, hiragana: true
19
+ validates :name, japanese: true
20
20
  ```
21
21
 
22
- or
22
+ ## Usage
23
23
 
24
24
  ```ruby
25
- validates_japanese :name
25
+ class User < ApplicationRecord
26
+ # hiragana katakana hankaku_kana kanji suji zenkaku_suji choonpu
27
+ validates :name, japanese: true
28
+
29
+ # only hiragana "ぁ-ん"
30
+ validates :name, japanese: {hiragana: true}
31
+
32
+ # only katakana "ァ-ン"
33
+ validates :name, japanese: {katakana: true}
34
+
35
+ # only hankaku_kana "ァ-ン゙゚"
36
+ validates :name, japanese: {hankaku_kana: true}
37
+
38
+ # only kanji "一-龠々"
39
+ validates :name, japanese: {kanji: true}
40
+
41
+ # only suji "0-9"
42
+ validates :name, japanese: {suji: true}
43
+
44
+ # only zenkaku_suji "0-9"
45
+ validates :name, japanese: {zenkaku_suji: true}
46
+ end
26
47
  ```
27
48
 
28
49
  ## Implementation
data/lib/locale/en.yml ADDED
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_japanese: is not a valid Japanese
@@ -0,0 +1,50 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class JapaneseValidator < ::ActiveModel::EachValidator
4
+ PREFIX = "\\A"
5
+ SUFFIX = "\\z"
6
+ KUHAKU = "\\s"
7
+ CHOONPU = "ー-"
8
+
9
+ ALPHABET = "a-zA-Z"
10
+ ZENKAKU_ALPHABET = "a-zA-Z"
11
+ HIRAGANA = "ぁ-ん"
12
+ KATAKANA = "ァ-ン"
13
+ HANKAKU_KANA = "ァ-ン゙゚"
14
+ KANJI = "一-龠々"
15
+ SUJI = "0-9"
16
+ ZENKAKU_SUJI = "0-9"
17
+
18
+ def validate_each(record, attribute, value)
19
+ unless value.to_s.match? build_regexp(options)
20
+ record.errors.add(attribute, options[:message] || :invalid)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def build_regexp(options)
27
+ options = options.except(:message)
28
+ japanese = %i(hiragana katakana hankaku_kana kanji suji zenkaku_suji choonpu)
29
+ keys = []
30
+
31
+ if options.empty?
32
+ keys.concat japanese
33
+ elsif options.has_key?(:only)
34
+ keys.concat options[:only]
35
+ elsif options.has_key?(:concat)
36
+ keys.concat japanese
37
+ keys.concat options[:concat].map(&:to_sym).except(*japanese)
38
+ else
39
+ keys.concat options.select {|_, v| v}.keys
40
+ end
41
+
42
+ Regexp.new("#{PREFIX}[#{keys.map(&method(:const)).join}]+#{SUFFIX}")
43
+ end
44
+
45
+ def const(name)
46
+ self.class.const_get(name.upcase)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidateJapanese
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,12 +1,9 @@
1
1
  require "active_support"
2
2
  require "active_record"
3
+ require 'active_support/i18n'
4
+ I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
3
5
  require "validate_japanese/version"
4
- require "validate_japanese/validations/hiragana_validator"
5
- require "validate_japanese/model"
6
-
7
- ActiveSupport.on_load(:active_record) do
8
- include(ValidateJapanese::Model)
9
- end
6
+ require "validate_japanese/japanese_validator"
10
7
 
11
8
  module ValidateJapanese
12
9
  class Error < StandardError; end
@@ -31,6 +31,9 @@ Gem::Specification.new do |spec|
31
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
32
  spec.require_paths = ["lib"]
33
33
 
34
+ spec.required_rubygems_version = ">= 1.3.6"
35
+ spec.required_ruby_version = ">= 2.3.0"
36
+
34
37
  spec.add_dependency "activerecord", [">= 4.2", "< 6.0"]
35
38
 
36
39
  spec.add_development_dependency "bundler"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_japanese
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-21 00:00:00.000000000 Z
11
+ date: 2018-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -105,9 +105,9 @@ files:
105
105
  - gemfiles/ar_4.2.gemfile
106
106
  - gemfiles/ar_5.1.gemfile
107
107
  - gemfiles/ar_5.2.gemfile
108
+ - lib/locale/en.yml
108
109
  - lib/validate_japanese.rb
109
- - lib/validate_japanese/model.rb
110
- - lib/validate_japanese/validations/hiragana_validator.rb
110
+ - lib/validate_japanese/japanese_validator.rb
111
111
  - lib/validate_japanese/version.rb
112
112
  - validate_japanese.gemspec
113
113
  homepage: https://github.com/ts-3156/validate_japanese
@@ -125,12 +125,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  requirements:
126
126
  - - ">="
127
127
  - !ruby/object:Gem::Version
128
- version: '0'
128
+ version: 2.3.0
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: '0'
133
+ version: 1.3.6
134
134
  requirements: []
135
135
  rubyforge_project:
136
136
  rubygems_version: 2.7.6
@@ -1,13 +0,0 @@
1
- require "active_support/concern"
2
-
3
- module ValidateJapanese
4
- module Model
5
- extend ActiveSupport::Concern
6
-
7
- class_methods do
8
- def validates_japanese(*attr_names)
9
- validates_with ::ActiveModel::Validations::HiraganaValidator, _merge_attributes(attr_names)
10
- end
11
- end
12
- end
13
- end
@@ -1,11 +0,0 @@
1
- module ActiveModel
2
- module Validations
3
- class HiraganaValidator < ::ActiveModel::EachValidator
4
- def validate_each(record, attribute, value)
5
- unless value =~ /\A[ぁ-んー-]+\z/
6
- record.errors.add(attribute, options[:message] || :invalid)
7
- end
8
- end
9
- end
10
- end
11
- end