How to Build Simple Console Apps with Ruby and ActiveRecordby Gregory Brown
If you're coming into Ruby via Rails, you've probably noticed that there is a whole lot to learn within the framework itself. In fact, the integration between Rails (the framework) and Ruby (the programming language) is so tight, that it might be tough to easily see where Rails ends and Ruby begins.
In terms of productivity and learning curve, this is a great feature. It means that you can learn both at once without encountering the chicken and egg problem you might find elsewhere. Still, if you're either a control freak, a tinkerer, or someone with needs that don't comfortably fit within a web browser, you've probably thought about exploring more of Ruby outside of Rails.
The really attractive thing about branching out into writing Ruby code outside of Rails is you don't need to throw away all of your Rails experience. Most of the features found in Rails are broken out into separate packages, which means you can cherry pick from your favorites and use them in your Ruby applications.
Introducing EarGTD, the Easy ActiveRecord GTD System for Ruby
Database driven web applications are great, but nothing brings back the power and glory of 1986 like a database driven console application. To illustrate this, we'll be walking through a simple Ruby application with some Rails goodness sprinkled on top.
Rather than taking the approach of building up an application as we work through the article tutorial style, this time, we'll be treating the source code like a cheap magazine, flipping back and forth to the interesting parts, and skipping some of the mundane details. This means your experience will be a whole lot more enjoyable if you grab the EarGTD source package so you can follow along and tinker with it as we explore its different parts.
A Quick GTD Primer
Many programmers I know are big fans of David Allen's Getting Things Done (GTD) productivity system. Though the system is quite comprehensive in all its practices and habits, I've always looked at it as mostly a "Mega To Do List."
The power of using this system is that all of your outstanding tasks end up in a single sink, and then you can cleverly filter them to be able to work on what makes the most sense productivity-wise at a given point in time.
I tend to prefer a minimalist approach for all of this, and EarGTD reflects that. I find that you can get pretty far with having just two ways to organize your tasks: by project and by context.
A project consists of several inter-related tasks. This can range anywhere from something code related to something like "Clean the garage."
A context is a conditional filter that lets you quickly decide whether a task is actionable at a given time. Certain things can be done anywhere, other things most definitely cannot. For example, "Mow the lawn" might have the context of "Home."
This topic can go a whole lot deeper, but since this is a Ruby article and not a productivity article, we'll leave it at that for now.
EarGTD in Action
EarGTD implements a very simple command-line interface that lets you record and manage tasks by project and context. It doesn't do a whole lot beyond that, but it is a perfect example of what you might build using plain old Ruby.
The following sample session shows how you might use this tool:
$ earGTD @ Looks like you have nothing to do. $ earGTD + "Check in accounting module <Foo> [work]" $ earGTD + "Add tests for credit card validation <Foo> [work]" $ earGTD + "Call Joe Frasier [work]" $ earGTD + "Plant a tree <Beautification> [home]" $ earGTD + "Weed the garden <Beautification> [home]" $ earGTD + "Paint the shed [home]" $ earGTD + "Play tennis for 60 hours" $ earGTD @ 1. Check in accounting module <Foo> [work] 2. Add tests for credit card validation <Foo> [work] 3. Call Joe Frasier [work] 4. Plant a tree <Beautification> [home] 5. Weed the garden <Beautification> [home] 6. Paint the shed [home] 7. Play tennis for 60 hours $ earGTD @c work 1. Check in accounting module <Foo> [work] 2. Add tests for credit card validation <Foo> [work] 3. Call Joe Frasier [work] $ earGTD @p Beautification 4. Plant a tree <Beautification> [home] 5. Weed the garden <Beautification> [home] $ earGTD - 5 $ earGTD @p Beautification 4. Plant a tree <Beautification> [home] $ earGTD @ 1. Check in accounting module <Foo> [work] 2. Add tests for credit card validation <Foo> [work] 3. Call Joe Frasier [work] 4. Plant a tree <Beautification> [home] 6. Paint the shed [home] 7. Play tennis for 60 hours
Though it's not particularly fancy, even this simple feature set shows that it's somewhat useful for recording and filtering tasks.
Learning by Example
We're going to take a walk through EarGTD's implementation. We'll start by taking a look at how to get ActiveRecord hooked up without the aid of code generators. We'll then dive into some of the application's more interesting parts.
Along the way, we'll cover several different concepts, including console and file I/O, simple modular structure, and some other useful idioms for building Ruby console applications.
Of course, leaving Rails behind isn't without its sacrifices…