Knight

Build Status Code Climate Coverage Status

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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request