knight 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +33 -1
- data/lib/knight/error.rb +11 -5
- data/lib/knight/instance_methods.rb +44 -6
- data/lib/knight/result.rb +17 -1
- data/lib/knight/validator.rb +17 -45
- data/lib/knight/version.rb +1 -1
- data/spec/integration/knight_spec.rb +17 -7
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/knight/instance_methods/class_methods/context_spec.rb +15 -0
- data/spec/unit/knight/instance_methods/class_methods/included_spec.rb +8 -3
- data/spec/unit/knight/instance_methods/class_methods/validator_spec.rb +13 -3
- data/spec/unit/knight/instance_methods/run_spec.rb +10 -6
- data/spec/unit/knight/result/on_spec.rb +31 -0
- data/spec/unit/knight/validator/add_spec.rb +3 -13
- data/spec/unit/knight/validator/run_spec.rb +4 -16
- metadata +8 -10
- data/spec/integration/contextual_validator_spec.rb +0 -41
- data/spec/unit/knight/validator/context_spec.rb +0 -13
- data/spec/unit/knight/validator/rules_spec.rb +0 -29
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,39 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
attr_reader :username
|
26
|
+
|
27
|
+
def initialize(username)
|
28
|
+
@username = username
|
29
|
+
end
|
30
|
+
end
|
31
|
+
user = User.new('john')
|
32
|
+
|
33
|
+
validator = Knight::Validator.new(Rule::Presence.new(:username))
|
34
|
+
result = validator.run(user)
|
35
|
+
result.valid? # => true
|
36
|
+
|
37
|
+
|
38
|
+
class UserValidator
|
39
|
+
include Knight::InstanceMethod
|
40
|
+
|
41
|
+
validator.add(Knight::Rule::Presence.new(:username))
|
42
|
+
|
43
|
+
context :login do |validator|
|
44
|
+
validator.add(Knight::Rule::Presence.new(:username))
|
45
|
+
validator.add(Knight::Rule::Presence.new(:password))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
validator = UserValidator.new(user)
|
49
|
+
|
50
|
+
result = validator.run
|
51
|
+
result.valid? # => true
|
52
|
+
|
53
|
+
result = validator.run(:login)
|
54
|
+
result.valid? # => false
|
55
|
+
```
|
24
56
|
|
25
57
|
## Contributing
|
26
58
|
|
data/lib/knight/error.rb
CHANGED
@@ -29,7 +29,7 @@ module Knight
|
|
29
29
|
#
|
30
30
|
# @api private
|
31
31
|
def initialize(rule, resource)
|
32
|
-
@rule
|
32
|
+
@rule = rule
|
33
33
|
@resource = resource
|
34
34
|
end
|
35
35
|
|
@@ -45,16 +45,22 @@ module Knight
|
|
45
45
|
rule.message % rule.to_hash.merge(value: value)
|
46
46
|
end
|
47
47
|
|
48
|
-
|
48
|
+
# Return error attribute name
|
49
|
+
#
|
50
|
+
# @return [String]
|
51
|
+
#
|
52
|
+
# @api private
|
53
|
+
def attribute_name
|
54
|
+
rule.attribute_name
|
55
|
+
end
|
49
56
|
|
50
|
-
# Return
|
57
|
+
# Return error value
|
51
58
|
#
|
52
59
|
# @return [Object]
|
53
60
|
#
|
54
61
|
# @api private
|
55
62
|
def value
|
56
|
-
|
57
|
-
resource.public_send(attribute) if resource.respond_to?(attribute)
|
63
|
+
resource.public_send(attribute_name) if resource.respond_to?(attribute_name)
|
58
64
|
end
|
59
65
|
end
|
60
66
|
end
|
@@ -4,6 +4,7 @@ module Knight
|
|
4
4
|
|
5
5
|
# Base functionality that added when included to a class
|
6
6
|
module InstanceMethods
|
7
|
+
DEFAULT_CONTEXT = :default
|
7
8
|
|
8
9
|
# Return the resource
|
9
10
|
#
|
@@ -42,27 +43,64 @@ module Knight
|
|
42
43
|
#
|
43
44
|
# class RegistrationValidator
|
44
45
|
# include Knight::InstanceMethods
|
45
|
-
# validator.add(Knight::Rule::Presence.new(:username))
|
46
46
|
# end
|
47
47
|
#
|
48
|
-
# result = RegistrationValidator.new(user).
|
48
|
+
# result = RegistrationValidator.new(user).run
|
49
49
|
#
|
50
50
|
# @return [Result]
|
51
51
|
#
|
52
52
|
# @api public
|
53
|
-
def run(context =
|
54
|
-
self.class.validator.run(resource
|
53
|
+
def run(context = DEFAULT_CONTEXT)
|
54
|
+
self.class.validator(context).run(resource)
|
55
55
|
end
|
56
56
|
|
57
57
|
module ClassMethods
|
58
58
|
|
59
59
|
# Return the validator object
|
60
60
|
#
|
61
|
+
# @example
|
62
|
+
# class RegistrationValidator
|
63
|
+
# include Knight::InstanceMethods
|
64
|
+
#
|
65
|
+
# validator.add(Knight::Rule::Presence.new(:username))
|
66
|
+
# end
|
67
|
+
#
|
61
68
|
# @return [Validator]
|
62
69
|
#
|
70
|
+
# @api public
|
71
|
+
def validator(context = DEFAULT_CONTEXT)
|
72
|
+
validators.fetch(context) do |key|
|
73
|
+
validators[key] = Validator.new
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return the validator object
|
78
|
+
#
|
79
|
+
# @example
|
80
|
+
# class RegistrationValidator
|
81
|
+
# include Knight::InstanceMethods
|
82
|
+
#
|
83
|
+
# context :register do |validator|
|
84
|
+
# validator.add(Knight::Rule::Presence.new(:username))
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# @return [undefined]
|
89
|
+
#
|
90
|
+
# @api public
|
91
|
+
def context(name)
|
92
|
+
yield validator(name)
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Return the validators contextual based
|
98
|
+
#
|
99
|
+
# @return [Hash]
|
100
|
+
#
|
63
101
|
# @api private
|
64
|
-
def
|
65
|
-
@
|
102
|
+
def validators
|
103
|
+
@validators = @validators || {}
|
66
104
|
end
|
67
105
|
end
|
68
106
|
end
|
data/lib/knight/result.rb
CHANGED
@@ -28,7 +28,7 @@ module Knight
|
|
28
28
|
# @api private
|
29
29
|
def initialize(resource, rules)
|
30
30
|
@resource = resource
|
31
|
-
@rules
|
31
|
+
@rules = rules
|
32
32
|
end
|
33
33
|
|
34
34
|
# Check the result valid or not
|
@@ -67,6 +67,22 @@ module Knight
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
+
# Return the errors for specific attribute
|
71
|
+
#
|
72
|
+
# @example
|
73
|
+
# validator = Validator.new(Rule::Presence.new(:username))
|
74
|
+
# user = User.new(username: 'john')
|
75
|
+
#
|
76
|
+
# result = validator.run(resource)
|
77
|
+
# result.on(:username)
|
78
|
+
#
|
79
|
+
# @return [Set(Error)]
|
80
|
+
#
|
81
|
+
# @api public
|
82
|
+
def on(attribute)
|
83
|
+
errors.select { |error| error.attribute_name == attribute }
|
84
|
+
end
|
85
|
+
|
70
86
|
private
|
71
87
|
|
72
88
|
# Check an attribute to the rule
|
data/lib/knight/validator.rb
CHANGED
@@ -3,12 +3,20 @@
|
|
3
3
|
module Knight
|
4
4
|
class Validator
|
5
5
|
|
6
|
-
|
6
|
+
# Return validation rules
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# validator = Validator.new(Rule::Presence.new(:username))
|
10
|
+
# validator.rules
|
11
|
+
#
|
12
|
+
# @return [Set(Rule)]
|
13
|
+
#
|
14
|
+
# @api public
|
15
|
+
attr_reader :rules
|
7
16
|
|
8
17
|
# Initialize a validator
|
9
18
|
#
|
10
19
|
# @example
|
11
|
-
# Validator.new
|
12
20
|
# Validator.new(Rule::Presence.new(:username))
|
13
21
|
#
|
14
22
|
# @param [Array(Rule)] rules
|
@@ -17,8 +25,7 @@ module Knight
|
|
17
25
|
#
|
18
26
|
# @api public
|
19
27
|
def initialize(*rules)
|
20
|
-
@rules =
|
21
|
-
rules.each { |rule| add(rule) }
|
28
|
+
@rules = Set.new(rules)
|
22
29
|
end
|
23
30
|
|
24
31
|
# Add a rule
|
@@ -28,64 +35,29 @@ module Knight
|
|
28
35
|
# validator.add(Rule::Presence.new(:username))
|
29
36
|
#
|
30
37
|
# @param [Rule] rule
|
31
|
-
# @param [Symbol] context
|
32
38
|
#
|
33
39
|
# @return [Set(Rule)]
|
34
40
|
#
|
35
41
|
# @api public
|
36
|
-
def add(rule
|
37
|
-
|
42
|
+
def add(rule)
|
43
|
+
rules << rule if rule
|
38
44
|
end
|
39
45
|
|
40
46
|
# Run the validator
|
41
47
|
#
|
42
48
|
# @example
|
43
|
-
# validator = Validator.new(Rule::Presence.new(:username))
|
44
49
|
# user = User.new(username: 'john')
|
50
|
+
#
|
51
|
+
# validator = Validator.new(Rule::Presence.new(:username))
|
45
52
|
# validator.run(user)
|
46
53
|
#
|
47
54
|
# @param [Object] resource
|
48
|
-
# @param [Symbol] context
|
49
55
|
#
|
50
56
|
# @return [Result]
|
51
57
|
#
|
52
58
|
# @api public
|
53
|
-
def run(resource
|
54
|
-
Result.new(resource, rules
|
55
|
-
end
|
56
|
-
|
57
|
-
# Run the validator
|
58
|
-
#
|
59
|
-
# @example
|
60
|
-
# validator = Validator.new(Rule::Presence.new(:username))
|
61
|
-
# validator.rules
|
62
|
-
#
|
63
|
-
# @param [Symbol] context
|
64
|
-
#
|
65
|
-
# @return [Set(Rule)]
|
66
|
-
#
|
67
|
-
# @api public
|
68
|
-
def rules(context_name = DEFAULT_CONTEXT)
|
69
|
-
@rules.fetch(context_name) do |key|
|
70
|
-
@rules[key] = Set.new
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# Accept block
|
75
|
-
#
|
76
|
-
# @example
|
77
|
-
# validator = Validator.new
|
78
|
-
# validator.context :registration do |context|
|
79
|
-
# context << Rule::Presence.new(:username)
|
80
|
-
# end
|
81
|
-
#
|
82
|
-
# @param [Symbol] name
|
83
|
-
#
|
84
|
-
# @return [undefined]
|
85
|
-
#
|
86
|
-
# @api public
|
87
|
-
def context(name)
|
88
|
-
yield rules(name)
|
59
|
+
def run(resource)
|
60
|
+
Result.new(resource, rules)
|
89
61
|
end
|
90
62
|
end
|
91
63
|
end
|
data/lib/knight/version.rb
CHANGED
@@ -5,17 +5,20 @@ require 'spec_helper'
|
|
5
5
|
describe Knight do
|
6
6
|
subject { result.valid? }
|
7
7
|
|
8
|
-
let(:
|
8
|
+
let(:object) { validator.new(resource) }
|
9
|
+
let(:resource) { user_class.new('john') }
|
9
10
|
let(:validator) do
|
10
11
|
Class.new do
|
11
12
|
include InstanceMethods
|
12
13
|
validator.add(Rule::Presence.new(:username))
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
context :registration do |validator|
|
16
|
+
validator.add(Rule::ExactLength.new(:username, 4))
|
17
|
+
validator.add(Rule::Presence.new(:title))
|
18
|
+
validator.add(Rule::ExactLength.new(:description, 40))
|
19
|
+
end
|
16
20
|
end
|
17
21
|
end
|
18
|
-
|
19
22
|
let(:user_class) do
|
20
23
|
Class.new do
|
21
24
|
attr_reader :username
|
@@ -25,6 +28,13 @@ describe Knight do
|
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
|
-
|
29
|
-
|
31
|
+
context 'valid' do
|
32
|
+
let(:result) { object.run }
|
33
|
+
it { should be_true }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'invalid' do
|
37
|
+
let(:result) { object.run(:registration) }
|
38
|
+
it { should be_false }
|
39
|
+
end
|
30
40
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe InstanceMethods, '.context' do
|
6
|
+
|
7
|
+
let(:described_class) { Class.new { include InstanceMethods } }
|
8
|
+
|
9
|
+
it do
|
10
|
+
described_class.should_receive(:validator).with(:registration).and_return(Validator.new)
|
11
|
+
described_class.context(:registration) do |validator|
|
12
|
+
validator.add Rule::Presence.new(:foo)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -3,10 +3,9 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe InstanceMethods, '.included' do
|
6
|
-
subject {
|
6
|
+
subject { described_class }
|
7
7
|
|
8
|
-
let(:
|
9
|
-
let(:klass) { Class.new }
|
8
|
+
let(:klass) { Class.new }
|
10
9
|
|
11
10
|
it 'delegates to the ancestor' do
|
12
11
|
included_ancestor = false
|
@@ -21,4 +20,10 @@ describe InstanceMethods, '.included' do
|
|
21
20
|
klass.send(:include, subject)
|
22
21
|
expect(klass).to respond_to(:validator)
|
23
22
|
end
|
23
|
+
|
24
|
+
it 'extends the klass' do
|
25
|
+
klass.singleton_class.should_not include(described_class::ClassMethods)
|
26
|
+
klass.send(:include, subject)
|
27
|
+
klass.singleton_class.should include(described_class::ClassMethods)
|
28
|
+
end
|
24
29
|
end
|
@@ -3,11 +3,21 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe InstanceMethods, '.validator' do
|
6
|
-
subject { described_class.validator }
|
7
6
|
|
8
7
|
let(:described_class) { Class.new { include InstanceMethods } }
|
9
8
|
let(:object) { described_class.new(double('1')) }
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
context 'default context' do
|
11
|
+
subject { described_class.validator }
|
12
|
+
|
13
|
+
it { should be_instance_of(Validator) }
|
14
|
+
specify { expect(subject).to equal(object.class.validator) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'registration context' do
|
18
|
+
subject { described_class.validator(:registration) }
|
19
|
+
|
20
|
+
it { should be_instance_of(Validator) }
|
21
|
+
specify { expect(subject).not_to equal(described_class.validator) }
|
22
|
+
end
|
13
23
|
end
|
@@ -3,27 +3,31 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe InstanceMethods, '.run' do
|
6
|
-
subject { object.run }
|
7
6
|
|
8
7
|
let(:described_class) do
|
9
8
|
Class.new do
|
10
9
|
include InstanceMethods
|
11
10
|
validator.add(Rule::Presence.new(:username))
|
12
|
-
|
11
|
+
|
12
|
+
context :registration do |validator|
|
13
|
+
validator.add(Rule::Presence.new(:username))
|
14
|
+
validator.add(Rule::Presence.new(:title))
|
15
|
+
end
|
13
16
|
end
|
14
17
|
end
|
15
18
|
let(:object) { described_class.new(resource) }
|
16
19
|
let(:resource) { double('resource') }
|
17
20
|
|
18
|
-
context '
|
21
|
+
context 'run without context' do
|
19
22
|
subject { object.run }
|
23
|
+
it { should be_instance_of(Result) }
|
20
24
|
specify { expect(subject.rules).to eql(described_class.validator.rules) }
|
21
25
|
specify { expect(subject.rules.size).to eql(1) }
|
22
26
|
end
|
23
27
|
|
24
|
-
context '
|
28
|
+
context 'run with context' do
|
25
29
|
subject { object.run(:registration) }
|
26
|
-
specify { expect(subject.rules).to eql(described_class.validator
|
27
|
-
specify { expect(subject.rules.size).to eql(
|
30
|
+
specify { expect(subject.rules).to eql(described_class.validator(:registration).rules) }
|
31
|
+
specify { expect(subject.rules.size).to eql(2) }
|
28
32
|
end
|
29
33
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Result, '#on' do
|
6
|
+
subject { result.on(:foo) }
|
7
|
+
|
8
|
+
let(:klass) do
|
9
|
+
Class.new do
|
10
|
+
attr_reader :foo
|
11
|
+
def initialize(foo)
|
12
|
+
@foo = foo
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:result) { validator.run(resource) }
|
18
|
+
let(:validator) { Validator.new(*rules) }
|
19
|
+
let(:resource) { klass.new('bar') }
|
20
|
+
let(:rules) do
|
21
|
+
[
|
22
|
+
Rule::Presence.new(:foo),
|
23
|
+
Rule::ExactLength.new(:foo, 4),
|
24
|
+
Rule::Presence.new(:username),
|
25
|
+
Rule::ExactLength.new(:username, 3)
|
26
|
+
]
|
27
|
+
end
|
28
|
+
|
29
|
+
specify { expect(subject.size).to eql(1) }
|
30
|
+
specify { expect(result.errors.size).to eql(2) }
|
31
|
+
end
|
@@ -3,27 +3,17 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Validator, '#add' do
|
6
|
-
subject { validator.rules
|
6
|
+
subject { validator.rules }
|
7
7
|
|
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
11
|
|
12
12
|
before do
|
13
|
-
validator.add(rule2, :register)
|
14
13
|
validator.add(rule2)
|
15
14
|
validator.add(nil)
|
16
15
|
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
specify { expect(subject.size).to eql(2) }
|
21
|
-
specify { expect(subject).to eql(Set.new([rule1, rule2])) }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'register' do
|
25
|
-
let(:context) { :register }
|
26
|
-
specify { expect(subject.size).to eql(1) }
|
27
|
-
specify { expect(subject).to eql(Set.new([rule2])) }
|
28
|
-
end
|
17
|
+
specify { expect(subject.size).to eql(2) }
|
18
|
+
specify { expect(subject).to eql(Set.new([rule1, rule2])) }
|
29
19
|
end
|
@@ -3,24 +3,12 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Validator, '#run' do
|
6
|
+
subject { validator.run(resource) }
|
6
7
|
|
7
8
|
let(:validator) { described_class.new(Rule::Presence.new(:username)) }
|
8
9
|
let(:resource) { double('resource') }
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
context 'default context' do
|
15
|
-
subject { validator.run(resource) }
|
16
|
-
it { should be_instance_of(Result) }
|
17
|
-
its(:resource) { should eql(resource) }
|
18
|
-
specify { expect(subject.rules).to eql(validator.rules) }
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'register context' do
|
22
|
-
subject { validator.run(resource, :register) }
|
23
|
-
it { should be_instance_of(Result) }
|
24
|
-
specify { expect(subject.rules).to eql(validator.rules(:register)) }
|
25
|
-
end
|
11
|
+
it { should be_instance_of(Result) }
|
12
|
+
its(:resource) { should eql(resource) }
|
13
|
+
its(:rules) { should eql(validator.rules) }
|
26
14
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: knight
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Handi Wiguna
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
prerelease: false
|
@@ -75,17 +75,18 @@ files:
|
|
75
75
|
- lib/knight/version.rb
|
76
76
|
- metrics/flog.yml
|
77
77
|
- metrics/rubocop.yml
|
78
|
-
- spec/integration/contextual_validator_spec.rb
|
79
78
|
- spec/integration/knight_spec.rb
|
80
79
|
- spec/spec_helper.rb
|
81
80
|
- spec/unit/knight/error/class_methods/new_spec.rb
|
82
81
|
- spec/unit/knight/error/message_spec.rb
|
82
|
+
- spec/unit/knight/instance_methods/class_methods/context_spec.rb
|
83
83
|
- spec/unit/knight/instance_methods/class_methods/included_spec.rb
|
84
84
|
- spec/unit/knight/instance_methods/class_methods/new_spec.rb
|
85
85
|
- spec/unit/knight/instance_methods/class_methods/validator_spec.rb
|
86
86
|
- spec/unit/knight/instance_methods/run_spec.rb
|
87
87
|
- spec/unit/knight/result/class_methods/new_spec.rb
|
88
88
|
- spec/unit/knight/result/errors_spec.rb
|
89
|
+
- spec/unit/knight/result/on_spec.rb
|
89
90
|
- spec/unit/knight/result/valid_predicate_spec.rb
|
90
91
|
- spec/unit/knight/rule/class_methods/new_spec.rb
|
91
92
|
- spec/unit/knight/rule/error_spec.rb
|
@@ -111,8 +112,6 @@ files:
|
|
111
112
|
- spec/unit/knight/rule/to_hash_spec.rb
|
112
113
|
- spec/unit/knight/validator/add_spec.rb
|
113
114
|
- spec/unit/knight/validator/class_methods/new_spec.rb
|
114
|
-
- spec/unit/knight/validator/context_spec.rb
|
115
|
-
- spec/unit/knight/validator/rules_spec.rb
|
116
115
|
- spec/unit/knight/validator/run_spec.rb
|
117
116
|
homepage: ''
|
118
117
|
licenses: []
|
@@ -126,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
125
|
- !ruby/object:Gem::Version
|
127
126
|
segments:
|
128
127
|
- 0
|
129
|
-
hash:
|
128
|
+
hash: -2162189312537362928
|
130
129
|
version: '0'
|
131
130
|
none: false
|
132
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -135,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
134
|
- !ruby/object:Gem::Version
|
136
135
|
segments:
|
137
136
|
- 0
|
138
|
-
hash:
|
137
|
+
hash: -2162189312537362928
|
139
138
|
version: '0'
|
140
139
|
none: false
|
141
140
|
requirements: []
|
@@ -145,17 +144,18 @@ signing_key:
|
|
145
144
|
specification_version: 3
|
146
145
|
summary: ''
|
147
146
|
test_files:
|
148
|
-
- spec/integration/contextual_validator_spec.rb
|
149
147
|
- spec/integration/knight_spec.rb
|
150
148
|
- spec/spec_helper.rb
|
151
149
|
- spec/unit/knight/error/class_methods/new_spec.rb
|
152
150
|
- spec/unit/knight/error/message_spec.rb
|
151
|
+
- spec/unit/knight/instance_methods/class_methods/context_spec.rb
|
153
152
|
- spec/unit/knight/instance_methods/class_methods/included_spec.rb
|
154
153
|
- spec/unit/knight/instance_methods/class_methods/new_spec.rb
|
155
154
|
- spec/unit/knight/instance_methods/class_methods/validator_spec.rb
|
156
155
|
- spec/unit/knight/instance_methods/run_spec.rb
|
157
156
|
- spec/unit/knight/result/class_methods/new_spec.rb
|
158
157
|
- spec/unit/knight/result/errors_spec.rb
|
158
|
+
- spec/unit/knight/result/on_spec.rb
|
159
159
|
- spec/unit/knight/result/valid_predicate_spec.rb
|
160
160
|
- spec/unit/knight/rule/class_methods/new_spec.rb
|
161
161
|
- spec/unit/knight/rule/error_spec.rb
|
@@ -181,6 +181,4 @@ test_files:
|
|
181
181
|
- spec/unit/knight/rule/to_hash_spec.rb
|
182
182
|
- spec/unit/knight/validator/add_spec.rb
|
183
183
|
- spec/unit/knight/validator/class_methods/new_spec.rb
|
184
|
-
- spec/unit/knight/validator/context_spec.rb
|
185
|
-
- spec/unit/knight/validator/rules_spec.rb
|
186
184
|
- spec/unit/knight/validator/run_spec.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Knight do
|
6
|
-
subject { result.valid? }
|
7
|
-
|
8
|
-
let(:validator) do
|
9
|
-
Class.new do
|
10
|
-
include InstanceMethods
|
11
|
-
validator.add(Rule::Presence.new(:username))
|
12
|
-
validator.add(Rule::ExactLength.new(:username, 3..4), :registration)
|
13
|
-
|
14
|
-
validator.context :registration do |context|
|
15
|
-
context << Rule::Presence.new(:title)
|
16
|
-
context << Rule::ExactLength.new(:description, 40)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
let(:user_class) do
|
22
|
-
Class.new do
|
23
|
-
attr_reader :username
|
24
|
-
def initialize(username)
|
25
|
-
@username = username
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'valid' do
|
31
|
-
let(:result) { validator.new(resource).run }
|
32
|
-
let(:resource) { user_class.new('john') }
|
33
|
-
it { should be_true }
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'invalid' do
|
37
|
-
let(:result) { validator.new(resource).run(:registration) }
|
38
|
-
let(:resource) { user_class.new('john') }
|
39
|
-
it { should be_false }
|
40
|
-
end
|
41
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Validator, '#context' do
|
4
|
-
|
5
|
-
before do
|
6
|
-
validator.context :registration do |context|
|
7
|
-
context << Rule::Presence.new(:username)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:validator) { described_class.new }
|
12
|
-
specify { expect(validator.rules(:registration).size).to eql(1) }
|
13
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Validator, '#rules' do
|
6
|
-
|
7
|
-
let(:validator) { described_class.new(Rule::Presence.new(:username)) }
|
8
|
-
let(:resource) { double('resource') }
|
9
|
-
|
10
|
-
before do
|
11
|
-
validator.add(Rule::Presence.new(:title), :register)
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'default context' do
|
15
|
-
subject { validator.rules }
|
16
|
-
specify { expect(subject.size).to eql(1) }
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'register context' do
|
20
|
-
subject { validator.rules(:register) }
|
21
|
-
specify { expect(subject.size).to eql(1) }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'not defined context' do
|
25
|
-
subject { validator.rules(:else) }
|
26
|
-
it { should be_instance_of(Set) }
|
27
|
-
specify { expect(subject.size).to eql(0) }
|
28
|
-
end
|
29
|
-
end
|