activemodel-behavior_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.
- checksums.yaml +7 -0
- data/.gitignore +34 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/README.md +31 -0
- data/Rakefile +5 -0
- data/activemodel-behavior_validator.gemspec +22 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/ja.yml +4 -0
- data/lib/activemodel-behavior_validator.rb +3 -0
- data/lib/behavior_validator.rb +22 -0
- data/lib/behavior_validator/engine.rb +6 -0
- data/lib/behavior_validator/version.rb +3 -0
- data/spec/behavior_validator_spec.rb +102 -0
- data/spec/spec_helper.rb +26 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 373144806bd7fd0470a5b5764b1cf862f6ad576f
|
4
|
+
data.tar.gz: 4f1c003a2da2f5208730d64f6143d021e231cd72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d7d9a8811fab322f7f229be06bf7552c0941c0550d31931cfbdc3560e230a058a3e479ecab75a9e2472d21c4d4927742129316becbfe346b38203b43d7463a49
|
7
|
+
data.tar.gz: bec71dc50270f0c4a1f41e3cb28926bdaa258daf1a1a992c2528898a076b1bd3c358324cd519da6402eee68a19b6e5cc37e98883dc3fd945cdd931ecf9d60abb
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
Gemfile.lock
|
30
|
+
.ruby-version
|
31
|
+
.ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# activemodel-behavior_validator
|
2
|
+
|
3
|
+
[](https://travis-ci.org/increments/activemodel-behavior_validator) [](https://codeclimate.com/github/increments/activemodel-behavior_validator) [](https://coveralls.io/r/increments/activemodel-behavior_validator) [](https://gemnasium.com/increments/activemodel-behavior_validator)
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add to your Gemfile:
|
8
|
+
|
9
|
+
```rb
|
10
|
+
gem 'activemodel-behavior_validator'
|
11
|
+
```
|
12
|
+
|
13
|
+
Run:
|
14
|
+
|
15
|
+
```
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Then add the following to your model:
|
20
|
+
|
21
|
+
```rb
|
22
|
+
validates :my_attribute, behavior: { active?: true }
|
23
|
+
```
|
24
|
+
|
25
|
+
## Validation outside a model
|
26
|
+
|
27
|
+
If you need to validate a outside a model, you can do that:
|
28
|
+
|
29
|
+
```rb
|
30
|
+
BehaviorValidator.valid?(object, { method_name: expected_result }
|
31
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
file = File.open(File.expand_path('../lib/behavior_validator/version.rb', __FILE__))
|
2
|
+
version = file.read.scan(/\d+\.\d+\.\d+/).first
|
3
|
+
file.close
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'activemodel-behavior_validator'
|
7
|
+
spec.version = version
|
8
|
+
spec.authors = ['Yuku Takahashi']
|
9
|
+
spec.email = ['yuku@qiita.com']
|
10
|
+
spec.summary = 'A behavior validator for Rails 3 and 4.'
|
11
|
+
spec.homepage = 'https://github.com/increments/activemodel-behavior_validator'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.require_paths = ['lib']
|
16
|
+
|
17
|
+
spec.add_dependency 'activemodel'
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
21
|
+
spec.add_development_dependency 'rspec'
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class BehaviorValidator < ActiveModel::EachValidator
|
2
|
+
class << self
|
3
|
+
def valid?(value, options = {})
|
4
|
+
options = options.reject { |option| reserved_options.include?(option) }
|
5
|
+
options.each_pair.all? do |method, expected_result|
|
6
|
+
value.__send__(method) == expected_result
|
7
|
+
end
|
8
|
+
rescue NoMethodError
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def reserved_options
|
13
|
+
[:allow_nil, :allow_blank, :message, :on]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate_each(record, attribute, value)
|
18
|
+
unless self.class.valid?(value, options)
|
19
|
+
record.errors.add(attribute, options[:message] || :invalid_behavior)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BehaviorValidator do
|
4
|
+
describe '.valid?' do
|
5
|
+
subject do
|
6
|
+
described_class.valid?(0, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'when the object responds to option keys' do
|
10
|
+
context 'and returns the corresponding value' do
|
11
|
+
let(:options) do
|
12
|
+
{ zero?: true }
|
13
|
+
end
|
14
|
+
|
15
|
+
it { should be_truthy }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'and does not return the corresponding value' do
|
19
|
+
let(:options) do
|
20
|
+
{ zero?: false }
|
21
|
+
end
|
22
|
+
|
23
|
+
it { should be_falsey }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when the object does not respond to option keys' do
|
28
|
+
let(:options) do
|
29
|
+
{ unknown_method: false }
|
30
|
+
end
|
31
|
+
|
32
|
+
it { should be_falsey }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when the options have reserved keys' do
|
36
|
+
let(:options) do
|
37
|
+
{ zero?: true }
|
38
|
+
end
|
39
|
+
|
40
|
+
before do
|
41
|
+
described_class.reserved_options.each do |reserved_option|
|
42
|
+
options[reserved_option] = true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'does not affect to the result' do
|
47
|
+
expect(described_class.valid?(0, options)).to be_truthy
|
48
|
+
expect(described_class.valid?(1, options)).to be_falsey
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'validations' do
|
54
|
+
subject do
|
55
|
+
model_class.new(attr: 0)
|
56
|
+
end
|
57
|
+
|
58
|
+
let(:model_class) do
|
59
|
+
opts = options
|
60
|
+
Class.new(TestModel) do
|
61
|
+
validates :attr, behavior: opts
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when the object responds to option keys' do
|
66
|
+
context 'and returns the corresponding value' do
|
67
|
+
let(:options) do
|
68
|
+
{ zero?: true }
|
69
|
+
end
|
70
|
+
|
71
|
+
it { should be_valid }
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'and does not return the corresponding value' do
|
75
|
+
let(:options) do
|
76
|
+
{ zero?: false }
|
77
|
+
end
|
78
|
+
|
79
|
+
it { should be_invalid }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when the options have reserved keys' do
|
84
|
+
let(:options) do
|
85
|
+
{
|
86
|
+
zero?: true,
|
87
|
+
|
88
|
+
# Reserved by ActiveModel.
|
89
|
+
allow_nil: true,
|
90
|
+
allow_blank: true,
|
91
|
+
message: 'hello'
|
92
|
+
# on: :create
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'does not affect to the result' do
|
97
|
+
expect(model_class.new(attr: 0)).to be_valid
|
98
|
+
expect(model_class.new(attr: 1)).to be_invalid
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
|
4
|
+
require 'active_model'
|
5
|
+
require 'behavior_validator'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.color = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestModel
|
13
|
+
include ActiveModel::Validations
|
14
|
+
|
15
|
+
def self.name
|
16
|
+
'TestModel'
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(attributes = {})
|
20
|
+
@attributes = attributes
|
21
|
+
end
|
22
|
+
|
23
|
+
def read_attribute_for_validation(key)
|
24
|
+
@attributes[key]
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activemodel-behavior_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuku Takahashi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-07 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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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
|
+
description:
|
70
|
+
email:
|
71
|
+
- yuku@qiita.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- activemodel-behavior_validator.gemspec
|
82
|
+
- config/locales/en.yml
|
83
|
+
- config/locales/ja.yml
|
84
|
+
- lib/activemodel-behavior_validator.rb
|
85
|
+
- lib/behavior_validator.rb
|
86
|
+
- lib/behavior_validator/engine.rb
|
87
|
+
- lib/behavior_validator/version.rb
|
88
|
+
- spec/behavior_validator_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/increments/activemodel-behavior_validator
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: A behavior validator for Rails 3 and 4.
|
114
|
+
test_files: []
|