kawaii_validation 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 +18 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/kawaii_validation.gemspec +27 -0
- data/lib/kawaii_validation/attributed_validator.rb +31 -0
- data/lib/kawaii_validation/non_attributed_validator.rb +31 -0
- data/lib/kawaii_validation/validates_with_block.rb +21 -0
- data/lib/kawaii_validation/version.rb +3 -0
- data/lib/kawaii_validation.rb +6 -0
- data/spec/fake_app.rb +19 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/validations_spec.rb +83 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f8181a5c93ba7ecf73c7ea67f5f3f4402b75a4f
|
4
|
+
data.tar.gz: f5cecbbf80288e433d22a1a98822e6c2c857c5b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 315ed3de1fdc0814cbc278309a928628bb9fc9380a604f5e0e8e4f056d510f4f7456349e7ff305701c33ec11d25307bb612cc22107a5b96f9762e37ffe5a37cb
|
7
|
+
data.tar.gz: 81577f6b0e0f2fa26ef8c232b5d4571b6de6f21b31407741191a4a0bd5b77aa0f638529387a9c148cad51bd8f6f2576446420e3d303066f9a83c21827eb8f826
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Akira Matsuda
|
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,67 @@
|
|
1
|
+
# KawaiiValidation
|
2
|
+
|
3
|
+
An ActiveRecord extension that adds more kawaii validation syntax.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Gemfile:
|
8
|
+
|
9
|
+
gem 'kawaii_validation'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
With this gem bundled, the `validates` method takes a block argument, and the following two new DSLs will be enabled:
|
18
|
+
|
19
|
+
* Original code (so-called "Sexy Validation")
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
# validations begin
|
24
|
+
validates :name, :age, presence: true
|
25
|
+
validates :name, length: {maximum: 255}
|
26
|
+
validates :age, numericality: {greater_than: 0}
|
27
|
+
# validations end
|
28
|
+
|
29
|
+
...
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
* validates + block
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
validates do
|
38
|
+
presence_of :name, :age
|
39
|
+
length_of :name, maximum: 255
|
40
|
+
numericality_of :age, greater_than: 0
|
41
|
+
end
|
42
|
+
|
43
|
+
...
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
* validates + attributes + block
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
validates :name do
|
52
|
+
presence
|
53
|
+
length maximum: 255
|
54
|
+
end
|
55
|
+
|
56
|
+
validates :age do
|
57
|
+
presence
|
58
|
+
numericality greater_than: 0
|
59
|
+
end
|
60
|
+
|
61
|
+
...
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
* Send me your pull requests!
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "kawaii_validation"
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ["Akira Matsuda"]
|
9
|
+
spec.email = ["ronnie@dio.jp"]
|
10
|
+
spec.description = 'Model.validates { presence_of :name }'
|
11
|
+
spec.summary = 'An ActiveRecord extension that adds more kawaii validation syntax'
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'railties'
|
21
|
+
spec.add_dependency 'activerecord'
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency 'rails'
|
25
|
+
spec.add_development_dependency 'rspec-rails'
|
26
|
+
spec.add_development_dependency 'sqlite3'
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'active_model/validations'
|
3
|
+
require 'active_model/validations/with'
|
4
|
+
|
5
|
+
module KawaiiValidation
|
6
|
+
# validates(:title) do
|
7
|
+
# presence :if => -> { true }
|
8
|
+
# end
|
9
|
+
class AttributedValidator < (Rails::VERSION::MAJOR == 4) ? ::ActiveSupport::ProxyObject : ::ActiveSupport::BasicObject
|
10
|
+
include ::ActiveModel::Validations::HelperMethods
|
11
|
+
|
12
|
+
def initialize(klass, *attributes)
|
13
|
+
@klass, @attributes = klass, *attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
if @klass.respond_to? "validates_#{name}_of"
|
18
|
+
@klass.send "validates_#{name}_of", *(args.any? ? @attributes + args : @attributes)
|
19
|
+
else
|
20
|
+
key = "#{name}Validator"
|
21
|
+
validator = begin
|
22
|
+
key.include?('::') ? key.constantize : @klass.const_get(key)
|
23
|
+
rescue NameError
|
24
|
+
raise ArgumentError, "Unknown validator: '#{key}'"
|
25
|
+
end
|
26
|
+
|
27
|
+
@klass.validates_with validator, _merge_attributes(@attributes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'active_model/validations'
|
3
|
+
require 'active_model/validations/with'
|
4
|
+
|
5
|
+
module KawaiiValidation
|
6
|
+
# validates do
|
7
|
+
# presence_of :title, :if => -> { true }
|
8
|
+
# end
|
9
|
+
class NonAttributedValidator < (Rails::VERSION::MAJOR == 4) ? ::ActiveSupport::ProxyObject : ::ActiveSupport::BasicObject
|
10
|
+
include ::ActiveModel::Validations::HelperMethods
|
11
|
+
|
12
|
+
def initialize(klass)
|
13
|
+
@klass = klass
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *attributes)
|
17
|
+
if @klass.respond_to? "validates_#{name}"
|
18
|
+
@klass.send "validates_#{name}", *attributes
|
19
|
+
else
|
20
|
+
key = "#{name.to_s.sub(/_of\Z/, '').camelize}Validator"
|
21
|
+
validator = begin
|
22
|
+
key.include?('::') ? key.constantize : @klass.const_get(key)
|
23
|
+
rescue NameError
|
24
|
+
raise ArgumentError, "Unknown validator: '#{key}'"
|
25
|
+
end
|
26
|
+
|
27
|
+
@klass.validates_with validator, _merge_attributes(attributes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'active_record'
|
3
|
+
require 'kawaii_validation/attributed_validator'
|
4
|
+
require 'kawaii_validation/non_attributed_validator'
|
5
|
+
|
6
|
+
module KawaiiValidation
|
7
|
+
module ValidatesWithBlock
|
8
|
+
def validates(*attributes, &block)
|
9
|
+
if block
|
10
|
+
validates_block_validator = if attributes.any?
|
11
|
+
KawaiiValidation::AttributedValidator.new self, attributes
|
12
|
+
else
|
13
|
+
KawaiiValidation::NonAttributedValidator.new self
|
14
|
+
end
|
15
|
+
validates_block_validator.instance_eval &block
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/fake_app.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# config
|
2
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
3
|
+
|
4
|
+
module KawaiiValidationTestApp
|
5
|
+
Application = Class.new(Rails::Application) do
|
6
|
+
config.eager_load = false
|
7
|
+
config.active_support.deprecation = :log
|
8
|
+
end.initialize!
|
9
|
+
end
|
10
|
+
|
11
|
+
# models
|
12
|
+
class User < ActiveRecord::Base; end
|
13
|
+
|
14
|
+
# migrations
|
15
|
+
class CreateAllTables < ActiveRecord::Migration
|
16
|
+
def self.up
|
17
|
+
create_table(:users) {|t| t.string :name; t.string :email; t.integer :age}
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(__dir__)
|
3
|
+
|
4
|
+
require 'rails'
|
5
|
+
require 'kawaii_validation'
|
6
|
+
require 'fake_app'
|
7
|
+
require 'action_controller/railtie' # needed for Rails 4 + RSpec...
|
8
|
+
require 'rspec/rails'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.before :all do
|
12
|
+
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
|
13
|
+
end
|
14
|
+
config.before :each do
|
15
|
+
User.delete_all
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Model#validates with a block' do
|
4
|
+
subject { User.new }
|
5
|
+
before do
|
6
|
+
User.reset_callbacks(:validate)
|
7
|
+
User._validators.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'no validation' do
|
11
|
+
it { should be_valid }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'without attributes' do
|
15
|
+
context 'without options' do
|
16
|
+
before do
|
17
|
+
User.validates do
|
18
|
+
presence_of :name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it { should_not be_valid }
|
23
|
+
it { should have(1).error_on(:name) }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with an option' do
|
27
|
+
before do
|
28
|
+
User.validates do
|
29
|
+
numericality_of :age, greater_than: 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
subject { User.new age: -1 }
|
34
|
+
it { should_not be_valid }
|
35
|
+
it { should have(1).error_on(:age) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with an attribute' do
|
40
|
+
context 'without options' do
|
41
|
+
before do
|
42
|
+
User.validates :email do
|
43
|
+
presence
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it { should_not be_valid }
|
48
|
+
it { should have(1).error_on(:email) }
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with an option' do
|
52
|
+
before do
|
53
|
+
User.validates :age do
|
54
|
+
numericality greater_than: 0
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
subject { User.new age: -1 }
|
59
|
+
it { should_not be_valid }
|
60
|
+
it { should have(1).error_on(:age) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'mixtures' do
|
65
|
+
before do
|
66
|
+
User.class_eval do
|
67
|
+
validates :email do
|
68
|
+
size maximum: 100
|
69
|
+
format with: /@/
|
70
|
+
end
|
71
|
+
validates :name, :age do
|
72
|
+
presence
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
subject { User.new email: 'a' * 101 }
|
78
|
+
it { should_not be_valid }
|
79
|
+
it { should have(2).errors_on(:email) }
|
80
|
+
it { should have(1).error_on(:name) }
|
81
|
+
it { should have(1).error_on(:age) }
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kawaii_validation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akira Matsuda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
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: activerecord
|
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.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
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: rails
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Model.validates { presence_of :name }
|
112
|
+
email:
|
113
|
+
- ronnie@dio.jp
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- MIT-LICENSE
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- kawaii_validation.gemspec
|
124
|
+
- lib/kawaii_validation.rb
|
125
|
+
- lib/kawaii_validation/attributed_validator.rb
|
126
|
+
- lib/kawaii_validation/non_attributed_validator.rb
|
127
|
+
- lib/kawaii_validation/validates_with_block.rb
|
128
|
+
- lib/kawaii_validation/version.rb
|
129
|
+
- spec/fake_app.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/validations_spec.rb
|
132
|
+
homepage: ''
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.0.2
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: An ActiveRecord extension that adds more kawaii validation syntax
|
156
|
+
test_files:
|
157
|
+
- spec/fake_app.rb
|
158
|
+
- spec/spec_helper.rb
|
159
|
+
- spec/validations_spec.rb
|