I've not started a new topic here for a while, so I thought I'd share this with you.

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 
You can also do a form of specification inheritance:

specification :authenticated_user do 
  should_satisfy :user_specification # the above spec 
You can even uses Ruby procs for quick, arbitrary specs:

specification :foo do 
  should_satisfy { |obj| # do something } 
Finally there is the Satisfies module which makes attaching specs directly to your classes (including ActiveRecord classes), a breeze:

ActiveRecord::Base.send(:include, ActiveSpec::Satisfies) 

class User < ActiveRecord::Base 
  should_satisfy :user_specification 
  should_satisfy :activated_user_specification, :if => :activated? 

user = User.new
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:


You can check out the RDocs here:


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


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!).