0.01
No commit activity in last 3 years
No release in over 3 years
The Preconditions library provides a simple set of methods for checking arguments being passed into a method. Instead of writing custom checks and raising exceptions directly in your code you can use Preconditions to verify basic properties of your arguments (not-nil, satisfying a boolean expression, being of a certain type/duck-type) and raise the appropriate exception for you.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.2
>= 0
>= 2.6.0
~> 0.6.0
 Project Readme

Preconditions

A simple package to make the testing of method arguments easier to write and easier to read, inspired by Guava's Preconditions class

Overview

The preconditions package provides a single module, Preconditions, which in turn provides a number of methods to check the validity of method arguments. Two API styles are provided: a standard "command query" interface, where each check is a single method call with an optional message format, and a fluent DSL interface, where checks are built up using a more natural language.

Usage

To use the command-query API you can access the check_XXX methods directly through the Preconditions module, like so:

class MyMath
  def sqrt(num)
    Preconditions.check_not_nil(num)
    Preconditions.check_type(num, Integer, "num argument must be an integer: non integer types are unsupported")
    Preconditions.check_argument(num >= 0, "num argument must be greater than zero")
    num.sqrt
  end
end

You can also include Preconditions to import the command-query calls into your class for use without the Preconditions module prefix. The full list of command-query calls is documented in the Preconditions module itself.

To use the fluent DSL API use the check(arg).is {} form like so:

class MyMath
  def sqrt(num)
    Preconditions.check(num) { is_not_nil and has_type(Integer) and satisfies(">= 0") { num >= 0 } }
    num.sqrt
  end
end

Note that there is less opportunity for custom messaging in the fluent API. However, a second argument to check can be supplied to add the argument name to any raised errors, or one can use the #named method to supply a name:

class MyMath
  def sqrt(num)
    Preconditions.check(num).named('num') { is_not_nil and has_type(Integer) and satisfies(">= 0") { num >= 0 } }
    num.sqrt
  end
end

In this case, if num is the value -10 then an ArgumentError will be raised with a message along the lines of "Argument 'num' must be >= 0, but was -10".

The set of available checks is documented in the ConditionChecker documentation.

The check method on the fluent API will not be imported when the Preconditions module is included: it can only be addressed with the Preconditions prefix. This is to prevent possible name clashes with existing check methods in client code (check being a somewhat common verb). The use of and as a separator in the DSL expression is purely for readability: newlines and semi-colons work just as well (all DSL methods either raise an exception or return true).

Contributing to preconditions

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011 Chris Tucker. See LICENSE.txt for further details.