ActiveSpec - Ruby Specifications library (preview)

Hi Guys

Over the past week (well, since RailsConf finished) I've been working
on a Ruby Specifications library called ActiveSpec, with the aim to
provide an alternative to Rails validations module (though ActiveSpec
can be used outside of Rails apps too).

What is ActiveSpec?

It is an implementation of the Specification domain pattern. ActiveSpec
provides built-in low-level specification classes (that mostly mirror
Rails' validates_* macros) and a few extras like composite specs and a
spec negator decorator. It provides a more declarative way of creating
composite specs, with the ActiveSpec::Base class, and finally it
provides a high-level DSL for creating specifications. What does that
DSL look like?

specification :user do
  should_require_presence_of :username, :password
  should_require_confirmation_of :password
end

You can also do a form of specification inheritance:

specification :authenticated_user do
  should_satisfy :user_specification # the above spec
end

You can even uses Ruby procs for quick, arbitrary specs:

specification :foo do
  should_satisfy { |obj| # do something }
end

Why ActiveSpec?

Rails' validates_* macros are awesome but sometimes you want a bit more
power, and using the lower level validate methods is a bit icky. More
importantly, as we found out on a recent project with a particularly
complex domain model, you want to validate your model in different ways
depending on its state or what context it is being used in. Its hard to
do this elegantly with Rails' current validations API.

Is it ready to use?

Not quite, but theres enough there to check out and start playing with.
The major thing that it is lacking that Rails validations have, is
error messages. This *will* be added but I haven't tackled it yet. But
I'd love people to check out the trunk and have a play and provide me
with some feedback at this early stage.

Where can I find it?

You can grab the source code from the Agile Evolved open source
repository:
http://opensource.agileevolved.com/svn/root/activespec/trunk/

You can check out the RDocs here:
http://docs.britsonrails.org.uk/activespec/

I've tried to fully document the API as best as I can. There's also a
README file there with more information.

License?

MIT, natch!

Have fun, let me know how you get on!

(by the way, if you want to know more about the Specification pattern
beyond what google tells you, I cannot recommend Domain Driven Design
by Eric Evans enough!).

Sorry guys; whilst I'm sure this is of interest to some of you on here,
I did actually mean to post this to the general Rails list (if my posts
ever show up on there!).

After looking at the examples provided I’m already seeing me adding a
method_missing to the object that prepends “should_” to what I call :slight_smile:
Just kidding… keep up the good work. Until now I used basic
subclassing for inheriting validations while declaring the top class as
abstract to deactivate STI. But when things get more complicated, I
think ActiveSpec will turn to be a gem. Thank you!