Automagical RSpec: Sake Spec for your Scripts 3

Posted by Justin Reagor Thu, 17 Apr 2008 01:18:00 GMT

I’ve been trying to attempt at spec’ing every single tiny lil script and bit of code I write a long the way. This means I needed a quick, cross directory/app/project script that would run specs from the current directory.

I love Rake, and I love Sake even more.. and Sake was born to do this sort of system-wide task… so lets get to the code!

Throw this in your ~/.sake and smoke it:

desc 'runs specs in the current project, with its own SpecHelper setup'
task 'spec' do
  require 'rake'
  require 'spec/rake/spectask'

  module SpecBase
    def self.included(klass)
      Object.class_eval do
        require 'rubygems'
        require 'spec'
        Spec::Runner.configure { |config| config.mock_with(:mocha) }
      end
    end
  end

  Spec::Rake::SpecTask.new("spec") do |t|
    t.spec_opts  = ["--format", "specdoc", "--colour"]
    t.spec_files = Dir["spec/**/*_spec.rb", "./*_spec.rb"].sort
    include SpecBase
  end
end

As the description string states, this task encapsulates spec_helper.rb and runs any _spec.rb files in the current directory. This makes it very quick for writing specs for small scripts.

Of course, I try to maintain a convention of naming my spec’s just like in Spec::Rails. So if I’m writing a eat_bacon.rb script, I use eat_bacon_spec.rb as the spec file.

Also note: I’m loading Mocha up there, so make sure to take that out if you don’t need it.

Enjoy, with Sake!

Our server names 5

Posted by Colin A. Bartlett Wed, 16 Apr 2008 12:59:00 GMT

Every good tech company needs a sensible naming scheme for their servers. Ours has always been “film directors Colin likes”. I figured it would be fun to publish our list here. So, in no particular order, these are the server names we’re currently using:

  • Richard Linklater
  • Alfred Hitchcock
  • Joel and Ethan Coen
  • Quentin Tarantino
  • M. Night Shyamalan
  • Robert Altman
  • David Cronenberg
  • Martin Scorsese
  • Dario Argento
  • Guy Ritchie
  • Wes Anderson
  • Darren Aronofksy
  • George Lucas
  • Rob Reiner
  • Robert Zemeckis
  • Stanley Kubrick
  • Steven Spielberg
  • David Fincher
  • Hayao Miyazaki
  • Terry Gilliam
  • Danny Boyle
  • John Hughes

Some others have been retired over the years but we try to recycle names when we can. Any suggestions? Baz Luhrmann, Joel Schumacher, Alan J. Pakula are all on the short list we keep.

Automagical RSpec: Shared Example Loading from Separate Files

Posted by Justin Reagor Wed, 16 Apr 2008 01:05:00 GMT

I especially love Ruby because I can quickly customize it to my tastes and likes. With out regard for anyone else’s feeling but my own. With that said, I do try and use this power for good. For the better of my office mates.

Earlier today I was doing some of this…

describe Admin::ModelsController do
  shared_examples_for 'all admin pages' do
    code code code here...
  end

  describe 'when logged in' do
    more specific controller specs here...
  end
end

...and I thought to myself that I would really just love to do this…

describe Admin::ModelsController do
  it_should_behave_like 'all admin pages'

  describe 'when logged in' do
    more specific controller specs here...
  end
end

Loading it from some separated out module underneath the specific MVC spec/ sub directories.

I’ll explain more in a second… but using some code I was working on before, thanks to storing it in Yojimbo, I quickly wrote this into my spec_helper.rb…

Dir[File.dirname(__FILE__)+'/**/shared/*'].each { |group| 
  require group
  include Object.const_get(group.match(/.*[\/]{1}([\w]*)[.rb]./).captures.first.camelize)
}

As you can tell from the code, it will go through all sub-directories of the current one (RAILS_ROOT/spec in this case) and rummage for “shared/” directories. It will then try and load modules, within these files, named after the file’s file name.

Without having to require and include each single file/module throughout your spec files (or the parent spec_helper).

Examples:

spec/controllers/shared/all_admin_pages.rb
module AllAdminPages
  shared_examples_for 'all admin pages' do
    code code code here...
  end
end

Of course this is a smallish hack that I think really cleans out my specs. I generally got the idea from app/views/shared or app/views/layouts/shared directories in Rails. Keeping small shared view partials in separate, nicely organized sub-directories.

Let the flaming commence! j/k ;)

Introduction to the Internet and eCommerce

Posted by Colin A. Bartlett Mon, 14 Apr 2008 20:13:00 GMT

Each year I volunteer a few hours of my time to speak to a group of individuals interested in starting their own business as part of the Reading SCORE program. It’s always great to see new entrepreneurs and to relay whatever little nuggets of experience I might have gained during my trials and tribulations starting a company (or two).

I speak for a short time about the basics of building a website for your start-up business and then I answer questions. I’ve been using the same presentation now for ages. (I don’t even recall if I created it or if it was given to me.) But it was in need a refresh.

So I’ve drafted a whole new version which is more my style – less boring bullets and more basic points and pictures to help trigger me to recall things and prompt questions.

For those that attended, you can download the slides here.