Knight
Installation
Add this line to your application's Gemfile:
gem 'knight'
And then execute:
$ bundle
Or install it yourself as:
$ gem install knight
Usage
class User
attr_reader :username, :password
def initialize(username, password)
@username = username
@password = password
end
end
user = User.new('john', '')
validator = Knight::Validator.new(Knight::Rule::Presence.new(:username))
result = validator.run(user)
result.valid? # => true
class UserValidator
include Knight::InstanceMethods
validator.add(Knight::Rule::Presence.new(:username))
context :login do |validator|
validator.add(Knight::Rule::Presence.new(:password))
end
end
validator = UserValidator.new(user)
result = validator.run
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)
result = validator.run(:login)
result.valid? # => false
user = User.new('john', 'password')
validator = UserValidator.new(user)
result = validator.run(:login)
result.valid? # => true
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request