Author: Matthew

  • Agile RTP: A new user group in Raleigh

    Agile RTP is a new user group started in the Raleigh area by Jared Richardson. The first meeting will be on March 22nd where we’ll be video conferencing with the agile group in Charlotte to hear Dr. Laurie Williams speak about test-driven development. I was actually considering driving to Charlotte for this speech, so when Jared told me we’d be able to enjoy it right here in Raleigh I was totally psyched. Get more details on the event and don’t forget to mark your calendar.

  • Overriding existing Rake tasks

    I added some long-running integration tests to a Rails application today and quickly began getting irritated that issuing the rake command runs all tests… unit, functional, AND integration. Since I run rake quite frequently, any sizable delay can quickly get annoying.

    The task that gets executed by rake is the :test task. After spending a few minutes trying to replace it, I discovered that there isn’t an immediately obvious way to override an existing task in Rake. After jumping through a few hoops, though, I did manage to do it.

    First, here’s my replacement for the existing :test task:

    task :test do
      Rake::Task["test:units"].invoke rescue got_error = true
      Rake::Task["test:functionals"].invoke rescue got_error = true
      raise "Test failures" if got_error
    end
    

    All it does is run the unit and functional tests, but no integration tests. I stuck this in my Rakefile right after the require 'tasks/rails' line. Next up, I reopened the Rake::TaskManager module to create my own little helper method to remove a task:

    Rake::TaskManager.class_eval do
      def remove_task(task_name)
        @tasks.delete(task_name.to_s)
      end
    end
    

    Lastly, I called this method from another method defined inside my Rakefile. This way, I could use syntax like remove_task :test which would fit with my other task definitions in the file. This is how everything looks put together (remember that this code should be inserted immediately after the require 'tasks/rails' line or it won’t work):

    Rake::TaskManager.class_eval do
      def remove_task(task_name)
        @tasks.delete(task_name.to_s)
      end
    end
    
    def remove_task(task_name)
      Rake.application.remove_task(task_name)
    end
    
    # Override existing test task to prevent integrations
    # from being run unless specifically asked for
    remove_task :test
    task :test do
      Rake::Task["test:units"].invoke rescue got_error = true
      Rake::Task["test:functionals"].invoke rescue got_error = true
      raise "Test failures" if got_error
    end
    

    This did the trick for me, but it’s kind of long. Anyone know a better way of doing it?

  • Selecting matching key/value pairs from a hash

    I ran into a situation today where I needed to pull out all key/value pairs from a hash that matched the keys in a pre-existing array. This is what I initially came up with:

    hash = { :foo => "foo", :bar => "bar", :bat => "bat" }
    hash.symbolize_keys.reject { |k, v| ![:foo, :bar].include?(k) }
    
    >>> returns { :foo => "foo", :bar => "bar" }
    

    Kind of ugly, ain’t it? I sure don’t want to repeat those lines elsewhere. Let’s see if we can do better:

    class Hash
      def select_all(*attrs)
        symbolize_keys.reject { |k, v| !attrs.include?(k) }
      end
    end
    
    { :foo => "foo", :bar => "bar", :bat => "bat" }.select_all(:foo, :bar)
    
    >>> returns { :foo => "foo", :bar => "bar" }
    

    Now that’s more like it. Extending Hash lets me call a method directly on my hash. I can also use this method anywhere in my Rails application if I place the Hash code in lib/hash.rb and require 'hash' from my environment.rb file. Isn’t Ruby beautiful?

    Anyone else have a better way of doing this? I’m open to suggestions.

  • Tag, I’m it

    I got tagged by my friend Jared Richardson on Wednesday. Now I’m supposed to blog about five things that people don’t know about me. Oh joy. I have to actually do some thinking? This might hurt.

    1. I’ve lived in seven different states over my 25 years of existence. I’ve visited 30 states. No, my father wasn’t in the military. It’s just worked out that way for some reason. I can’t say I’ve particularly enjoyed all the hopping around, but it sure gave me a chance to see a wide variety of scenery and discover how people differ between regions.
    2. My ambition going into college was to become an astronaut. Yes, I’m serious. My plan was to get a masters in Computer Engineering, learn Russian, get my pilot’s license, and move to Houston. Then I discovered that the computer programming I had been doing since I was 9 was actually kind of enjoyable. Funny how circumstances and chance occurrences can have such a dramatic impact on carefully laid plans.
    3. Shortly after I moved to North Carolina, I spent eight months apprenticing at Ken Auer’s software studio in Holly Springs. There I was given the opportunity to learn XP (eXtreme Programming) and pair program with true masters of the craft of software development. The skills I picked up there have been immeasurably valuable to me ever since. Ken has been influencing the direction of XP since it was founded. Aside from getting to learn from him, being at the studio also gave me the chance to meet guys like Andy Hunt, Nathaniel Talbott, Duff O’Melia, and Adam Williams… all during my first year of college.
    4. My first computer was a PC Junior. I was writing text-based adventure games in BASIC on it when I was 9. I still have one of the games I wrote. The code is definitely not DRY, but the game is fun to play.
    5. I was taught completely at home from K through 12, along with my only brother. It’s an experience that I look back on with an incredible amount of appreciation. I’m grateful that my parents (especially my Mom) decided to go this route. They both could have just stuck us in a government school so they could accumulate money and things (like too many people choose to do these days), but they chose to invest themselves in us instead. It was a decision that has paid off in many ways, not least of which is a closer relationship between me, my parents, and my brother.

    There you have it. Still awake? Good. I thought of a few more things I wanted to add to the list (such as “single and looking”) but it just didn’t seem like the right thing to do. Now I’ll tag Nathaniel Talbott, Adam Williams, John W. Long, Alan Hoffler, and Sri Sankaran.

  • AppleScript via Ruby

    Nice to see Ruby getting some attention on a non-Ruby blog.

  • Andy Hunt crashes raleigh.rb tonight

    Andy Hunt will be speaking at the Raleigh-area Ruby Brigade meetup tonight. It’s sure to be a good show, so grab a fellow geek and head on down to Red Hat headquarters at 7 PM. Signs will direct you to the correct room. Also, if you want an even bigger helping of Ruby tonight, join us at 5:30 PM at Baja Burrito for our traditional round of pre-meeting chow and conversation.

  • Integrate GTD with Gmail using Firefox

    Getting Things Done is a book by David Allen. The methodology from the book, commonly referred to as GTD, has become quite popular inside certain tech circles. I’ve been using GTD for roughly a year now. I can’t claim to be an expert at it, but it’s helped me stay organized during a period of my life that would otherwise have been extremely unorganized.

    There are many tools out there that make implementing GTD on your computer fairly painless. For you fellow Mac cultists, Actiontastic does the job nicely with a clean, minimalist interface. There is also Tracks, a Rails-based web application that you can install… well, pretty much wherever. What I’d like to introduce in this post, though, is a unique Firefox extension called GTDGmail.

    GTDGmailI’ve been using GTDGmail for a few months now. Once installed, it integrates with Gmail and modifies your view slightly. Among other things, it divides your tags into four categories: projects, contexts, statuses, and references. As e-mail messages come in, you can categorize them as actions by tagging them with the “Action” status. If an e-mail contains important information you’d like to keep for future reference, tag it with a “Reference” and a “Project.” As you tag your e-mail, it becomes available under GTDGmail’s pre-built search links that appear above your tags.

    You can also send yourself actions and references. This is where the famous GTD practice of “capturing” comes into play. The idea is to get all that stuff floating around in your head out and organized into action items. Sending yourself an action or a reference results in a new e-mail message in your inbox, automatically tagges as an action or a reference and ready to be organized further into projects and contexts.

    GTDGmail also has some handy non-GTD uses. For example, it makes regular tagging much easier by placing links at the top of each e-mail, one link for each tag. Simply click on a link to add that tag to the e-mail you’re currently viewing. Click the red X next to the tag to remove it from the e-mail. This is much faster than scrolling through a drop-down to find the tag you want to add or remove.

    The only downside to GTDGmail is that it does make Gmail less responsive. Since GTDGmail has to overlay Gmail’s existing layout with additional information, it can get bogged down at times. However, the upside of being able to immediately categorize your incoming e-mail into action items more than makes up for this. GTDGmail is not for everyone, but I encourage you to try it out to see if it fits your own organizational style.

  • Show database migration versions with Rake

    Something I frequently need to do when deploying a Rails application is determine the current migration version of a database. In the past, I’ve done this by bringing up mySQL and typing:

    SELECT version FROM schema_info

    Well, I’m finally tired of doing that so I cooked up this Rake task:

    namespace :db do
      desc "Print current database migration version to the console"
      task :version => :environment do
        puts "VERSION=#{ActiveRecord::Migrator.current_version}"
      end
    end
    

    To install, copy and paste into lib/tasks/common.rake (or wherever you store your custom Rake tasks). Then fire up a terminal window and feel the power:

    $ rake db:version
    (in /Users/matt/project)
    VERSION=125
    

    I’m surprised this isn’t already in Rails. Sure, there’s script/about, but I always forget to use it. It also spits out other information I don’t typically need to know.

    Why care about the migration version in the first place? When you’re on a project with multiple Rails deployments (dev/test/prod) and multiple developers creating migrations in multiple branches, it comes in quite handy.

  • Teascript goes live

    TeascriptI’m proud to announce that Teascript went live early last week. It’s been a long several months since originally conceiving the idea, building the app, beta testing, and deploying but now it’s finally ready for prime time.

    Teascript is a Rails application that makes it easy to design and build professional high school transcripts. It’s mainly targeted towards home school parents and students, however, I’ve received interest from several public and private schools who are looking for a transcript generation package they can install locally so the potential for this product seems huge.

    For those interested in how Teascript was deployed, the application runs on Apache 2.2 with mod_proxy_balancer and a cluster of Mongrels. I’m using a VPS provided by Slicehost. This setup has been rock solid so far.

    My talk at RailsConf this year will focus on the development of Teascript: how the target niche was chosen, how the application was marketed, and so on. My goal is to demonstrate how the principles outlined in 37 Signals’ book Getting Real and the homesteading concepts put forth by Nathaniel Talbott at last year’s RailsConf were combined to produce a web product that is self-maintaining, sustainable, and capable of generating passive income.

    So please, check out Teascript and let me know what you think.

  • Mind blowing Ruby

    I’ve been writing Ruby code for over four years now. I’ve been getting paid to do so for about two years. Prior to that, the bacon I brought home came mainly through the careful crafting of lines of Java, PHP, and C# code. Matz spoiled the party for me in the mid-90s, however, by releasing such a wonderfully beautiful language that it made Java tedious and uninteresting in comparison.

    Ruby is a fantastic tool. But it’s not just a tool, it’s also an art medium. I had never truly seen beautiful code until I met Ruby. All that being said, when I was first introduced to the language it was a lot like trying to breathe while standing under Niagara Falls. The “wow-that’s-cool-but-I-don’t-quite-grok-it” moments came fast and furious. Ruby was blowing my mind on a daily basis.

    It doesn’t anymore. At least, not every day. But at least once a month, I see a code snippet or read a blog post or hear about a new way to do something and… well, there goes my mind again. For example…

    (more…)