Constraint -- Ensure that objects always satisfy a set of constraints
This library provides a way to ensure that certain objects always
satisfy a specified set of constraints. An object that can be constrained
must be a child of
Constraint::Shell, which acts as a
wrapper for the actual value. Constraints can added to subclasses and
its instances (per-object constraints). It is possible to define methods
that handle constraint violations and retrofit the object according
to its definition.
With some help from the developer, this library can also be used (to some extent at least) to generate correct values and to find solutions fitting the set of constraints.
In my experience, a certain type of error in dynamically typed languages is caused by pushing a wrong object to a collection or similar. What makes this type of error so awkward is that they often result in an exception in a different part of your program, which is why it can sometimes be unnecessarily difficult to track down what is actually causing the problem. The goal of this library is to raise an exception right where such a thing happens. While ruby is sometimes a little bit hesitant in raising exceptions, this library tries to gently counteract this attitude a little.
Be aware that this library introduces yet another level of indirection and yet additional runtime checks which slows things down a little. If you define CONSTRAINT_DISABLE before requiring the library, constraint checks will be deactivated.