Skip to content

Commit

Permalink
* filtering result and validator based on attribute
Browse files Browse the repository at this point in the history
* bump to 0.0.5
  • Loading branch information
Handi Wiguna committed Aug 8, 2013
1 parent f374fdd commit 7046490
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ result.valid? # => true

result = validator.run(:login)
result.valid? # => false
rresult.on(:password) # => #<Set: {#<Knight::Error rule=#<Knight::Rule::Presence attribute_name=:password>>}>esult.valid? # => true

user = User.new('', 'password')
validator = UserValidator.new(user)
Expand Down
2 changes: 1 addition & 1 deletion lib/knight/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def errors
#
# @api public
def on(attribute)
errors.select { |error| error.attribute_name == attribute }
errors.select { |error| error.attribute_name == attribute }.to_set
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/knight/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Knight
# An abstract class of a rule
class Rule
include AbstractType
include Equalizer.new(:attribute_name, :options)
include Equalizer.new(:attribute_name)

DEFAULT_MESSAGE = ''.freeze

Expand Down
35 changes: 35 additions & 0 deletions lib/knight/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,40 @@ def add(rule)
def run(resource)
Result.new(resource, rules)
end

# Run the validator on specific attribute
#
# @example
# user = User.new(username: 'john')
#
# validator = Validator.new(Rule::Presence.new(:username))
# validator.run_on(user, :username)
#
# @param [Object] resource
# @param [Symbol] attribute
#
# @return [Result]
#
# @api public
def run_on(resource, attribute)
Result.new(resource, on(attribute))
end

# Run the rules on specific attribute
#
# @example
# user = User.new(username: 'john')
#
# validator = Validator.new(Rule::Presence.new(:username))
# validator.on(:username)
#
# @param [Symbol] attribute
#
# @return [Set(Rules)]
#
# @api public
def on(attribute)
rules.select { |rule| rule.attribute_name == attribute }.to_set
end
end
end
2 changes: 1 addition & 1 deletion lib/knight/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8

module Knight
VERSION = '0.0.4'
VERSION = '0.0.5'
end
2 changes: 2 additions & 0 deletions spec/unit/knight/result/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def initialize(foo)
let(:rules) do
[Rule::Presence.new(:foo), Rule::ExactLength.new(:bar, 3)]
end
let(:resource) { double('resource') }
it { should be_instance_of(Set) }

context 'empty string' do
let(:resource) { klass.new('') }
Expand Down
1 change: 1 addition & 0 deletions spec/unit/knight/result/on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def initialize(foo)
]
end

it { should be_instance_of(Set) }
specify { expect(subject.size).to eql(1) }
specify { expect(result.errors.size).to eql(2) }
end
2 changes: 2 additions & 0 deletions spec/unit/knight/validator/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
let(:validator) { described_class.new(rule1) }
let(:rule1) { Rule::ExactLength.new(:username, 10) }
let(:rule2) { Rule::Presence.new(:username) }
let(:rule3) { Rule::Presence.new(:username, message: 'message') }

before do
validator.add(rule2)
validator.add(rule3)
validator.add(nil)
end

Expand Down
13 changes: 13 additions & 0 deletions spec/unit/knight/validator/on_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# encoding: utf-8

require 'spec_helper'

describe Validator, '#on' do
subject { validator.on(:username) }

let(:validator) { described_class.new(*rules) }
let(:rules) { [Rule::Presence.new(:username), Rule::Presence.new(:password)] }

it { should be_instance_of(Set) }
specify { expect(subject.size).to eql(1) }
end
17 changes: 17 additions & 0 deletions spec/unit/knight/validator/run_on_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: utf-8

require 'spec_helper'

describe Validator, '#run_on' do
subject { validator.run_on(resource, attribute) }

let(:validator) { described_class.new }
let(:resource) { double('resource') }
let(:attribute) { :username }

it do
validator.should_receive(:on).with(attribute)
should be_instance_of(Result)
end
its(:resource) { should eql(resource) }
end

0 comments on commit 7046490

Please sign in to comment.