Expects
A DSL for validating input for methods
Installation
Add this line to your application's Gemfile:
gem 'expects'
And then execute:
$ bundle
Or install it yourself as:
$ gem install expects
Usage
Include Expects
in your class and then call it in your methods eg:
class YourClass
include Expects
def initialize(name)
expects name, String
end
end
You can pass an array instead of one class to accept multiple types e.g.
expects number, [Fixnum, Float]
If you try to pass anything other than a string to YourClass.new expects will raise an exception which you can catch like so:
begin
YourClass.new(1234)
rescue UnexpectedInput => e
e. # Expected 1234 to be String
e.subject # 1234
e.expected # [String]
end
All exceptions thrown by expects are instances of UnexpectedInput
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