knight 0.0.3 → 0.0.4
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/README.md
CHANGED
@@ -22,36 +22,44 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
class User
|
25
|
-
attr_reader :username
|
25
|
+
attr_reader :username, :password
|
26
26
|
|
27
|
-
def initialize(username)
|
27
|
+
def initialize(username, password)
|
28
28
|
@username = username
|
29
|
+
@password = password
|
29
30
|
end
|
30
31
|
end
|
31
|
-
user = User.new('john')
|
32
|
+
user = User.new('john', '')
|
32
33
|
|
33
|
-
validator = Knight::Validator.new(Rule::Presence.new(:username))
|
34
|
+
validator = Knight::Validator.new(Knight::Rule::Presence.new(:username))
|
34
35
|
result = validator.run(user)
|
35
36
|
result.valid? # => true
|
36
37
|
|
37
|
-
|
38
38
|
class UserValidator
|
39
|
-
include Knight::
|
39
|
+
include Knight::InstanceMethods
|
40
40
|
|
41
41
|
validator.add(Knight::Rule::Presence.new(:username))
|
42
42
|
|
43
43
|
context :login do |validator|
|
44
|
-
validator.add(Knight::Rule::Presence.new(:username))
|
45
44
|
validator.add(Knight::Rule::Presence.new(:password))
|
46
45
|
end
|
47
46
|
end
|
48
47
|
validator = UserValidator.new(user)
|
49
|
-
|
50
48
|
result = validator.run
|
51
49
|
result.valid? # => true
|
52
50
|
|
53
51
|
result = validator.run(:login)
|
54
52
|
result.valid? # => false
|
53
|
+
|
54
|
+
user = User.new('', 'password')
|
55
|
+
validator = UserValidator.new(user)
|
56
|
+
result = validator.run(:login)
|
57
|
+
result.valid? # => false
|
58
|
+
|
59
|
+
user = User.new('john', 'password')
|
60
|
+
validator = UserValidator.new(user)
|
61
|
+
result = validator.run(:login)
|
62
|
+
result.valid? # => true
|
55
63
|
```
|
56
64
|
|
57
65
|
## Contributing
|
@@ -38,6 +38,8 @@ module Knight
|
|
38
38
|
|
39
39
|
# Return the validation result
|
40
40
|
#
|
41
|
+
# @param [Symbol] context
|
42
|
+
#
|
41
43
|
# @example
|
42
44
|
# user = User.new
|
43
45
|
#
|
@@ -58,6 +60,8 @@ module Knight
|
|
58
60
|
|
59
61
|
# Return the validator object
|
60
62
|
#
|
63
|
+
# @param [Symbol] context
|
64
|
+
#
|
61
65
|
# @example
|
62
66
|
# class RegistrationValidator
|
63
67
|
# include Knight::InstanceMethods
|
@@ -76,6 +80,8 @@ module Knight
|
|
76
80
|
|
77
81
|
# Return the validator object
|
78
82
|
#
|
83
|
+
# @param [Symbol] name
|
84
|
+
#
|
79
85
|
# @example
|
80
86
|
# class RegistrationValidator
|
81
87
|
# include Knight::InstanceMethods
|
@@ -89,12 +95,14 @@ module Knight
|
|
89
95
|
#
|
90
96
|
# @api public
|
91
97
|
def context(name)
|
92
|
-
|
98
|
+
validator = validator(name)
|
99
|
+
validator.rules.merge(shared_rules) unless name == DEFAULT_CONTEXT
|
100
|
+
yield validator
|
93
101
|
end
|
94
102
|
|
95
103
|
private
|
96
104
|
|
97
|
-
# Return
|
105
|
+
# Return all validators
|
98
106
|
#
|
99
107
|
# @return [Hash]
|
100
108
|
#
|
@@ -102,6 +110,15 @@ module Knight
|
|
102
110
|
def validators
|
103
111
|
@validators = @validators || {}
|
104
112
|
end
|
113
|
+
|
114
|
+
# Return rules for default context
|
115
|
+
#
|
116
|
+
# @return [Set(Rule)]
|
117
|
+
#
|
118
|
+
# @api private
|
119
|
+
def shared_rules
|
120
|
+
validator.rules
|
121
|
+
end
|
105
122
|
end
|
106
123
|
end
|
107
124
|
end
|
data/lib/knight/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Knight do
|
6
|
+
|
7
|
+
let(:klass) do
|
8
|
+
Class.new do
|
9
|
+
include InstanceMethods
|
10
|
+
|
11
|
+
validator.add(Rule::Presence.new(:foo))
|
12
|
+
|
13
|
+
context :context1 do |validator|
|
14
|
+
validator.add(Rule::Presence.new(:bar))
|
15
|
+
end
|
16
|
+
|
17
|
+
validator.add(Rule::Presence.new(:baz))
|
18
|
+
|
19
|
+
context :default do |validator|
|
20
|
+
validator.add(Rule::Presence.new(:username))
|
21
|
+
end
|
22
|
+
|
23
|
+
context :context1 do |validator|
|
24
|
+
validator.add(Rule::Presence.new(:password))
|
25
|
+
end
|
26
|
+
|
27
|
+
context :context2 do |validator|
|
28
|
+
validator.add(Rule::Presence.new(:title))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'default context' do
|
34
|
+
subject { klass.validator }
|
35
|
+
specify { expect(subject.rules.size).to eql(3) }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'context1' do
|
39
|
+
subject { klass.validator(:context1) }
|
40
|
+
specify { expect(subject.rules.size).to eql(5) }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'context2' do
|
44
|
+
subject { klass.validator(:context2) }
|
45
|
+
specify { expect(subject.rules.size).to eql(4) }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'not exists context' do
|
49
|
+
subject { klass.validator(:registration) }
|
50
|
+
specify { expect(subject.rules.size).to eql(0) }
|
51
|
+
end
|
52
|
+
end
|
@@ -6,10 +6,19 @@ describe InstanceMethods, '.context' do
|
|
6
6
|
|
7
7
|
let(:described_class) { Class.new { include InstanceMethods } }
|
8
8
|
|
9
|
+
it 'only call validator once' do
|
10
|
+
described_class.should_receive(:validator).once.and_return(Validator.new)
|
11
|
+
described_class.context(:default) do |validator|
|
12
|
+
validator.add Rule::Presence.new(:foo)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
9
16
|
it do
|
10
|
-
described_class.
|
17
|
+
described_class.validator.add(Rule::Presence.new(:foo))
|
11
18
|
described_class.context(:registration) do |validator|
|
12
|
-
validator.add Rule::Presence.new(:
|
19
|
+
validator.add Rule::Presence.new(:bar)
|
13
20
|
end
|
21
|
+
expect(described_class.validator(:default).rules.size).to eql(1)
|
22
|
+
expect(described_class.validator(:registration).rules.size).to eql(2)
|
14
23
|
end
|
15
24
|
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.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Handi Wiguna
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/knight/version.rb
|
76
76
|
- metrics/flog.yml
|
77
77
|
- metrics/rubocop.yml
|
78
|
+
- spec/integration/context_spec.rb
|
78
79
|
- spec/integration/knight_spec.rb
|
79
80
|
- spec/spec_helper.rb
|
80
81
|
- spec/unit/knight/error/class_methods/new_spec.rb
|
@@ -125,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
126
|
- !ruby/object:Gem::Version
|
126
127
|
segments:
|
127
128
|
- 0
|
128
|
-
hash:
|
129
|
+
hash: 592111569675221754
|
129
130
|
version: '0'
|
130
131
|
none: false
|
131
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -134,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
135
|
- !ruby/object:Gem::Version
|
135
136
|
segments:
|
136
137
|
- 0
|
137
|
-
hash:
|
138
|
+
hash: 592111569675221754
|
138
139
|
version: '0'
|
139
140
|
none: false
|
140
141
|
requirements: []
|
@@ -144,6 +145,7 @@ signing_key:
|
|
144
145
|
specification_version: 3
|
145
146
|
summary: ''
|
146
147
|
test_files:
|
148
|
+
- spec/integration/context_spec.rb
|
147
149
|
- spec/integration/knight_spec.rb
|
148
150
|
- spec/spec_helper.rb
|
149
151
|
- spec/unit/knight/error/class_methods/new_spec.rb
|