uuid_validator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +11 -0
- data/lib/active_model/locale/en.yml +5 -0
- data/lib/active_model/locale/ja.yml +5 -0
- data/lib/active_model/validations/uuid_validator.rb +14 -0
- data/lib/uuid_validator.rb +7 -0
- data/lib/uuid_validator/version.rb +3 -0
- data/test/test_helper.rb +15 -0
- data/test/uuid_validator_test.rb +84 -0
- data/uuid_validator.gemspec +23 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 HORII Keima
|
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,35 @@
|
|
1
|
+
# UuidValidator
|
2
|
+
|
3
|
+
UUID validator for ActiveModel
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'uuid_validator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install uuid_validator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class SomeModel
|
23
|
+
include ActiveModel::Validations
|
24
|
+
attr_accessor :key
|
25
|
+
validates :key, :uuid => true
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class UuidValidator < ActiveModel::EachValidator
|
6
|
+
RE = /\A[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\z/i
|
7
|
+
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
record.errors.add(attribute, :not_an_uuid) unless value =~ RE
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
require 'active_model'
|
5
|
+
require 'uuid_validator'
|
6
|
+
|
7
|
+
class TestModel
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :key
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
@key = options[:key]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'securerandom'
|
3
|
+
require 'uuidtools'
|
4
|
+
|
5
|
+
describe ActiveModel::Validations::UuidValidator do
|
6
|
+
|
7
|
+
before do
|
8
|
+
TestModel.reset_callbacks(:validate)
|
9
|
+
I18n.locale = 'en'
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "validates :key, :uuid => true" do
|
13
|
+
before do
|
14
|
+
TestModel.validates :key, :uuid => true
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "passes for wikipedia sample" do
|
18
|
+
it "is valid" do
|
19
|
+
key = "550e8400-e29b-41d4-a716-446655440000"
|
20
|
+
model = TestModel.new(key: key)
|
21
|
+
model.valid?.must_equal true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "test for SecureRandom.uuid" do
|
26
|
+
it "is valid" do
|
27
|
+
model = TestModel.new(key: SecureRandom.uuid)
|
28
|
+
model.valid?.must_equal true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "test for UUIDTools" do
|
33
|
+
it "is valid for md5_create" do
|
34
|
+
key = UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.example.com").to_s
|
35
|
+
model = TestModel.new(key: key)
|
36
|
+
model.valid?.must_equal true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "is valid for sha1_create" do
|
40
|
+
key = UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.example.com").to_s
|
41
|
+
model = TestModel.new(key: key)
|
42
|
+
model.valid?.must_equal true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "is valid for timestamp_create" do
|
46
|
+
key = UUIDTools::UUID.timestamp_create.to_s
|
47
|
+
model = TestModel.new(key: key)
|
48
|
+
model.valid?.must_equal true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "is valid for random_create" do
|
52
|
+
key = UUIDTools::UUID.random_create.to_s
|
53
|
+
model = TestModel.new(key: key)
|
54
|
+
model.valid?.must_equal true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def error_in(model)
|
59
|
+
model.valid?.must_equal false
|
60
|
+
model.errors[:key].must_equal ["is not an uuid"]
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "when nil" do
|
64
|
+
it "is invalid" do
|
65
|
+
error_in TestModel.new(key: nil)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "when empty string" do
|
70
|
+
it "is invalid" do
|
71
|
+
error_in TestModel.new(key: '')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "when non number" do
|
76
|
+
it "is invalid" do
|
77
|
+
key = "550e8400-e29b-41d4-a716-XXXXXXXXXXXX"
|
78
|
+
error_in TestModel.new(key: key)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'uuid_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "uuid_validator"
|
8
|
+
gem.version = UuidValidator::VERSION
|
9
|
+
gem.authors = ["HORII Keima"]
|
10
|
+
gem.email = ["holysugar@gmail.com"]
|
11
|
+
gem.description = %q{UUID Validator for ActiveModel}
|
12
|
+
gem.summary = %q{UUID Validator for ActiveModel}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "activemodel", ">= 3.0.0"
|
21
|
+
|
22
|
+
gem.add_development_dependency "uuidtools"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uuid_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- HORII Keima
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: uuidtools
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: UUID Validator for ActiveModel
|
47
|
+
email:
|
48
|
+
- holysugar@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/active_model/locale/en.yml
|
59
|
+
- lib/active_model/locale/ja.yml
|
60
|
+
- lib/active_model/validations/uuid_validator.rb
|
61
|
+
- lib/uuid_validator.rb
|
62
|
+
- lib/uuid_validator/version.rb
|
63
|
+
- test/test_helper.rb
|
64
|
+
- test/uuid_validator_test.rb
|
65
|
+
- uuid_validator.gemspec
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.24
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: UUID Validator for ActiveModel
|
90
|
+
test_files:
|
91
|
+
- test/test_helper.rb
|
92
|
+
- test/uuid_validator_test.rb
|