uuid_validator 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uuid_validator.gemspec
4
+ gemspec
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,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ not_an_uuid: "is not an uuid"
5
+
@@ -0,0 +1,5 @@
1
+ ja:
2
+ errors:
3
+ messages:
4
+ not_an_uuid: "UUID を入力してください。"
5
+
@@ -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
@@ -0,0 +1,7 @@
1
+ require 'active_model/validations/uuid_validator'
2
+ require "uuid_validator/version"
3
+
4
+ module UuidValidator
5
+ end
6
+
7
+ I18n.load_path += Dir[File.expand_path(File.dirname(__FILE__) + '/active_model/locale/*.yml')]
@@ -0,0 +1,3 @@
1
+ module UuidValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -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