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 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)
@@ -80,7 +80,7 @@ module Knight
80
80
  #
81
81
  # @api public
82
82
  def on(attribute)
83
- errors.select { |error| error.attribute_name == attribute }
83
+ errors.select { |error| error.attribute_name == attribute }.to_set
84
84
  end
85
85
 
86
86
  private
@@ -5,7 +5,7 @@ module Knight
5
5
  # An abstract class of a rule
6
6
  class Rule
7
7
  include AbstractType
8
- include Equalizer.new(:attribute_name, :options)
8
+ include Equalizer.new(:attribute_name)
9
9
 
10
10
  DEFAULT_MESSAGE = ''.freeze
11
11
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Knight
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -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('') }
@@ -26,6 +26,7 @@ describe Result, '#on' do
26
26
  ]
27
27
  end
28
28
 
29
+ it { should be_instance_of(Set) }
29
30
  specify { expect(subject.size).to eql(1) }
30
31
  specify { expect(result.errors.size).to eql(2) }
31
32
  end
@@ -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.4
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: 592111569675221754
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: 592111569675221754
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