kana_validator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/kana_validator.gemspec +28 -0
- data/lib/kana_validator.rb +26 -0
- data/lib/kana_validator/version.rb +3 -0
- data/spec/kana_validator_spec.rb +186 -0
- data/spec/spec_helper.rb +22 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1f9ebb65fb537d27c38a581f508677e877aca92
|
4
|
+
data.tar.gz: 3b4c9e20c0c8b5ff1d19fe0dee5f4ba30a3a57e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c4834eea7dc4b4348cb37030fc34e1ef1a5ad76870174f373fdf486d035d197e8892ebb9ca8eca076d3e4775bac8c2dba4cf06c7e53e2482fcae8f9948a0dbe
|
7
|
+
data.tar.gz: 6631a81da87487b53d84130d316277704e7332776713f6f77bd8c611bfcfdbf9d051cea15c613ca24fd1d8bdbbb05a44419990f1071ae44b6653b177c383efbe
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Koichi Tanaka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# KanaValidator
|
2
|
+
|
3
|
+
Rails 3+ validator for ひらがな and かたかな.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'kana_validator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install kana_validator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class HiraganaUser < ActiveRecord::Base
|
23
|
+
validates :name, kana: {with: %i(hiragana)}
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
options are:
|
28
|
+
- `:hiragana` accepts ひらがな.
|
29
|
+
- `:katakana` accepts カタカナカタカナ.
|
30
|
+
- `:katakana_zen` accepts カタカナ.
|
31
|
+
- `:katakana_han` accepts カタカナ.
|
32
|
+
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( http://github.com/tanaka51-jp/kana_validator/fork )
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kana_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'kana_validator'
|
8
|
+
spec.version = KanaValidator::VERSION
|
9
|
+
spec.authors = ['Koichi Tanaka']
|
10
|
+
spec.email = ['tanaka51.jp@gmail.com']
|
11
|
+
spec.summary = %q{Rails 3+ validator for ひらがな and かたかな.}
|
12
|
+
spec.description = %q{Rails 3+ validator for ひらがな and かたかな.}
|
13
|
+
spec.homepage = 'http://github.com/tanaka51-jp'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_dependency 'activemodel'
|
23
|
+
spec.add_dependency 'moji'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'moji'
|
2
|
+
|
3
|
+
unless defined?(Moji::NONE)
|
4
|
+
# make null flag
|
5
|
+
Moji.make_flag_set([], :NONE)
|
6
|
+
# join existed set...
|
7
|
+
Moji::NONE.instance_variable_set("@flag_set", Moji::KATA.instance_variable_get("@flag_set"))
|
8
|
+
end
|
9
|
+
|
10
|
+
class KanaValidator < ActiveModel::EachValidator
|
11
|
+
def validate_each(record, attribute, value)
|
12
|
+
type = Moji::NONE
|
13
|
+
|
14
|
+
type |= Moji::HIRA if options[:with].include?(:hiragana)
|
15
|
+
# DISCUSS: we should include Moji::JSYNBOL?
|
16
|
+
type |= (Moji::KATA | Moji::JSYMBOL) if options[:with].include?(:katakana)
|
17
|
+
type |= Moji::ZEN_KATA if options[:with].include?(:katakana_zen)
|
18
|
+
type |= (Moji::HAN_KATA | Moji::JSYMBOL) if options[:with].include?(:katakana_han)
|
19
|
+
|
20
|
+
raise "Invalid option" if type == Moji::NONE
|
21
|
+
|
22
|
+
unless value.is_a?(String) && value.chars.all?{|c| Moji.type?(c, type) }
|
23
|
+
record.errors.add(attribute, options[:message] || :invalid)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class HiraganaUser < TestModel
|
4
|
+
validates :name, kana: {with: %i(hiragana)}
|
5
|
+
end
|
6
|
+
|
7
|
+
class KatakanaUser < TestModel
|
8
|
+
validates :name, kana: {with: %i(katakana)}
|
9
|
+
end
|
10
|
+
|
11
|
+
class KatakanaZenkakuUser < TestModel
|
12
|
+
validates :name, kana: {with: %i(katakana_zen)}
|
13
|
+
end
|
14
|
+
|
15
|
+
class KatakanaHankakuUser < TestModel
|
16
|
+
validates :name, kana: {with: %i(katakana_han)}
|
17
|
+
end
|
18
|
+
|
19
|
+
describe KanaValidator do
|
20
|
+
subject { klass.new(name: string).valid? }
|
21
|
+
|
22
|
+
describe 'with: [:hiragana]' do
|
23
|
+
|
24
|
+
let(:klass) { HiraganaUser }
|
25
|
+
|
26
|
+
context 'given hira-gana string' do
|
27
|
+
let(:string) { 'ひらがな' }
|
28
|
+
|
29
|
+
it { should be_true }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'given zenkaku kata-kana string' do
|
33
|
+
let(:string) { 'カタカナ' }
|
34
|
+
|
35
|
+
it { should be_false }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'given hankaku kata-kana string' do
|
39
|
+
let(:string) { 'カタカナーガダガダ' }
|
40
|
+
|
41
|
+
it { should be_false }
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'given zenkaku kata-kana and hira-gana string' do
|
45
|
+
let(:string) { 'カタカナひらがな' }
|
46
|
+
|
47
|
+
it { should be_false }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'given hira-gana and kata-kana string' do
|
51
|
+
let(:string) { 'ひらがなカタカナ' }
|
52
|
+
|
53
|
+
it { should be_false }
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'given alphabet string' do
|
57
|
+
let(:string) { 'abcdefg' }
|
58
|
+
|
59
|
+
it { should be_false }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'with: [:katakana]' do
|
64
|
+
|
65
|
+
let(:klass) { KatakanaUser }
|
66
|
+
|
67
|
+
context 'given hira-gana string' do
|
68
|
+
let(:string) { 'ひらがな' }
|
69
|
+
|
70
|
+
it { should be_false }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'given zenkaku kata-kana string' do
|
74
|
+
let(:string) { 'カタカナ' }
|
75
|
+
|
76
|
+
it { should be_true }
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'given hankaku kata-kana string' do
|
80
|
+
let(:string) { 'カタカナーガダガダ' }
|
81
|
+
|
82
|
+
it { should be_true }
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'given zenkaku kata-kana and hira-gana string' do
|
86
|
+
let(:string) { 'カタカナひらがな' }
|
87
|
+
|
88
|
+
it { should be_false }
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'given hira-gana and kata-kana string' do
|
92
|
+
let(:string) { 'ひらがなカタカナ' }
|
93
|
+
|
94
|
+
it { should be_false }
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'given alphabet string' do
|
98
|
+
let(:string) { 'abcdefg' }
|
99
|
+
|
100
|
+
it { should be_false }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'with: [:katakana_zen]' do
|
105
|
+
|
106
|
+
let(:klass) { KatakanaZenkakuUser }
|
107
|
+
|
108
|
+
context 'given hira-gana string' do
|
109
|
+
let(:string) { 'ひらがな' }
|
110
|
+
|
111
|
+
it { should be_false }
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'given zenkaku kata-kana string' do
|
115
|
+
let(:string) { 'カタカナ' }
|
116
|
+
|
117
|
+
it { should be_true }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'given hankaku kata-kana string' do
|
121
|
+
let(:string) { 'カタカナーガダガダ' }
|
122
|
+
|
123
|
+
it { should be_false }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'given zenkaku kata-kana and hira-gana string' do
|
127
|
+
let(:string) { 'カタカナひらがな' }
|
128
|
+
|
129
|
+
it { should be_false }
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'given hira-gana and kata-kana string' do
|
133
|
+
let(:string) { 'ひらがなカタカナ' }
|
134
|
+
|
135
|
+
it { should be_false }
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'given alphabet string' do
|
139
|
+
let(:string) { 'abcdefg' }
|
140
|
+
|
141
|
+
it { should be_false }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe 'with: [:katakana_han]' do
|
146
|
+
|
147
|
+
let(:klass) { KatakanaHankakuUser }
|
148
|
+
|
149
|
+
context 'given hira-gana string' do
|
150
|
+
let(:string) { 'ひらがな' }
|
151
|
+
|
152
|
+
it { should be_false }
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'given zenkaku kata-kana string' do
|
156
|
+
let(:string) { 'カタカナ' }
|
157
|
+
|
158
|
+
it { should be_false }
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'given hankaku kata-kana string' do
|
162
|
+
let(:string) { 'カタカナーガダガダ' }
|
163
|
+
|
164
|
+
it { should be_true }
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'given zenkaku kata-kana and hira-gana string' do
|
168
|
+
let(:string) { 'カタカナひらがな' }
|
169
|
+
|
170
|
+
it { should be_false }
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'given hira-gana and kata-kana string' do
|
174
|
+
let(:string) { 'ひらがなカタカナ' }
|
175
|
+
|
176
|
+
it { should be_false }
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'given alphabet string' do
|
180
|
+
let(:string) { 'abcdefg' }
|
181
|
+
|
182
|
+
it { should be_false }
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require 'kana_validator'
|
9
|
+
|
10
|
+
class TestModel
|
11
|
+
include ActiveModel::Validations
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
@attributes = attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def read_attribute_for_validation(key)
|
18
|
+
@attributes[key]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
I18n.enforce_available_locales = false
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kana_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koichi Tanaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: moji
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Rails 3+ validator for ひらがな and かたかな.
|
84
|
+
email:
|
85
|
+
- tanaka51.jp@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- kana_validator.gemspec
|
96
|
+
- lib/kana_validator.rb
|
97
|
+
- lib/kana_validator/version.rb
|
98
|
+
- spec/kana_validator_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: http://github.com/tanaka51-jp
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Rails 3+ validator for ひらがな and かたかな.
|
124
|
+
test_files:
|
125
|
+
- spec/kana_validator_spec.rb
|
126
|
+
- spec/spec_helper.rb
|