knight 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/knight/result.rb +1 -1
- data/lib/knight/rule.rb +1 -1
- data/lib/knight/validator.rb +35 -0
- data/lib/knight/version.rb +1 -1
- data/spec/unit/knight/result/errors_spec.rb +2 -0
- data/spec/unit/knight/result/on_spec.rb +1 -0
- data/spec/unit/knight/validator/add_spec.rb +2 -0
- data/spec/unit/knight/validator/on_spec.rb +13 -0
- data/spec/unit/knight/validator/run_on_spec.rb +17 -0
- metadata +7 -3
data/README.md
CHANGED
@@ -50,6 +50,7 @@ result.valid? # => true
|
|
50
50
|
|
51
51
|
result = validator.run(:login)
|
52
52
|
result.valid? # => false
|
53
|
+
rresult.on(:password) # => #<Set: {#<Knight::Error rule=#<Knight::Rule::Presence attribute_name=:password>>}>esult.valid? # => true
|
53
54
|
|
54
55
|
user = User.new('', 'password')
|
55
56
|
validator = UserValidator.new(user)
|
data/lib/knight/result.rb
CHANGED
data/lib/knight/rule.rb
CHANGED
data/lib/knight/validator.rb
CHANGED
@@ -59,5 +59,40 @@ module Knight
|
|
59
59
|
def run(resource)
|
60
60
|
Result.new(resource, rules)
|
61
61
|
end
|
62
|
+
|
63
|
+
# Run the validator on specific attribute
|
64
|
+
#
|
65
|
+
# @example
|
66
|
+
# user = User.new(username: 'john')
|
67
|
+
#
|
68
|
+
# validator = Validator.new(Rule::Presence.new(:username))
|
69
|
+
# validator.run_on(user, :username)
|
70
|
+
#
|
71
|
+
# @param [Object] resource
|
72
|
+
# @param [Symbol] attribute
|
73
|
+
#
|
74
|
+
# @return [Result]
|
75
|
+
#
|
76
|
+
# @api public
|
77
|
+
def run_on(resource, attribute)
|
78
|
+
Result.new(resource, on(attribute))
|
79
|
+
end
|
80
|
+
|
81
|
+
# Run the rules on specific attribute
|
82
|
+
#
|
83
|
+
# @example
|
84
|
+
# user = User.new(username: 'john')
|
85
|
+
#
|
86
|
+
# validator = Validator.new(Rule::Presence.new(:username))
|
87
|
+
# validator.on(:username)
|
88
|
+
#
|
89
|
+
# @param [Symbol] attribute
|
90
|
+
#
|
91
|
+
# @return [Set(Rules)]
|
92
|
+
#
|
93
|
+
# @api public
|
94
|
+
def on(attribute)
|
95
|
+
rules.select { |rule| rule.attribute_name == attribute }.to_set
|
96
|
+
end
|
62
97
|
end
|
63
98
|
end
|
data/lib/knight/version.rb
CHANGED
@@ -32,6 +32,8 @@ describe Result, '#errors' do
|
|
32
32
|
let(:rules) do
|
33
33
|
[Rule::Presence.new(:foo), Rule::ExactLength.new(:bar, 3)]
|
34
34
|
end
|
35
|
+
let(:resource) { double('resource') }
|
36
|
+
it { should be_instance_of(Set) }
|
35
37
|
|
36
38
|
context 'empty string' do
|
37
39
|
let(:resource) { klass.new('') }
|
@@ -8,9 +8,11 @@ describe Validator, '#add' do
|
|
8
8
|
let(:validator) { described_class.new(rule1) }
|
9
9
|
let(:rule1) { Rule::ExactLength.new(:username, 10) }
|
10
10
|
let(:rule2) { Rule::Presence.new(:username) }
|
11
|
+
let(:rule3) { Rule::Presence.new(:username, message: 'message') }
|
11
12
|
|
12
13
|
before do
|
13
14
|
validator.add(rule2)
|
15
|
+
validator.add(rule3)
|
14
16
|
validator.add(nil)
|
15
17
|
end
|
16
18
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Validator, '#on' do
|
6
|
+
subject { validator.on(:username) }
|
7
|
+
|
8
|
+
let(:validator) { described_class.new(*rules) }
|
9
|
+
let(:rules) { [Rule::Presence.new(:username), Rule::Presence.new(:password)] }
|
10
|
+
|
11
|
+
it { should be_instance_of(Set) }
|
12
|
+
specify { expect(subject.size).to eql(1) }
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Validator, '#run_on' do
|
6
|
+
subject { validator.run_on(resource, attribute) }
|
7
|
+
|
8
|
+
let(:validator) { described_class.new }
|
9
|
+
let(:resource) { double('resource') }
|
10
|
+
let(:attribute) { :username }
|
11
|
+
|
12
|
+
it do
|
13
|
+
validator.should_receive(:on).with(attribute)
|
14
|
+
should be_instance_of(Result)
|
15
|
+
end
|
16
|
+
its(:resource) { should eql(resource) }
|
17
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knight
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Handi Wiguna
|
@@ -113,6 +113,8 @@ files:
|
|
113
113
|
- spec/unit/knight/rule/to_hash_spec.rb
|
114
114
|
- spec/unit/knight/validator/add_spec.rb
|
115
115
|
- spec/unit/knight/validator/class_methods/new_spec.rb
|
116
|
+
- spec/unit/knight/validator/on_spec.rb
|
117
|
+
- spec/unit/knight/validator/run_on_spec.rb
|
116
118
|
- spec/unit/knight/validator/run_spec.rb
|
117
119
|
homepage: ''
|
118
120
|
licenses: []
|
@@ -126,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
128
|
- !ruby/object:Gem::Version
|
127
129
|
segments:
|
128
130
|
- 0
|
129
|
-
hash:
|
131
|
+
hash: -3627835457547741889
|
130
132
|
version: '0'
|
131
133
|
none: false
|
132
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -135,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
137
|
- !ruby/object:Gem::Version
|
136
138
|
segments:
|
137
139
|
- 0
|
138
|
-
hash:
|
140
|
+
hash: -3627835457547741889
|
139
141
|
version: '0'
|
140
142
|
none: false
|
141
143
|
requirements: []
|
@@ -183,4 +185,6 @@ test_files:
|
|
183
185
|
- spec/unit/knight/rule/to_hash_spec.rb
|
184
186
|
- spec/unit/knight/validator/add_spec.rb
|
185
187
|
- spec/unit/knight/validator/class_methods/new_spec.rb
|
188
|
+
- spec/unit/knight/validator/on_spec.rb
|
189
|
+
- spec/unit/knight/validator/run_on_spec.rb
|
186
190
|
- spec/unit/knight/validator/run_spec.rb
|