From the monthly archives:

January 2009

I’ve been working on NORM and after a few proof of concept things, I wrote a test to test the create method for the base class.

Here’s the create method:

def create(attributes)
  columns = attributes.keys.join(", ")
  values = attributes.collect {|k, v| "'#{v}'"}.join(", ")
  @@connection.execute("INSERT INTO base (#{columns}) VALUES (#{values});")
  new(attributes)
end

I’ve written dozens of unit tests in Ruby on Rails, but what I didn’t realize was that the same library you use to test in Rails is the standard test/unit library came with the Ruby installation on my Windows machine. So, I wrote my own test which I later revised. [click to continue…]

{ 0 comments }

I’m really excited to try out the new Windows 7 that’s coming out. I’ve downloaded the beta and will be trying it out when I get a few minutes.

{ 0 comments }

I’m pretty sure that the Ruby DBI gem is deprecated, however, I’m writing NORM so that I can experience the pain of refactoring as well as proving out that Test Driven Design has a place in modern development. It also helps because previous to the project I’m currently working on for my job at SolutionStream I have not used TDD and I’m anxious to see its power in a project like this one.

I found I needed the MySQL adapter for my application—I’m only building for MySQL right now. That required the dbd-mysql gem in addition to the dbi gem. After installing the gems, I found I could require them and then use them to connect to the database. Here are some code snippets from NORM that show how I’m using the DBI library.

First require the dbi and mysql gems.

require 'dbd/mysql'
require 'dbi'

Then, in my object, I’m storing the connection in a class variable.

@@connection = DBI.connect("DBI:Mysql:norm:localhost", "root", "")

Finally, I’m using the connection to perform operations on the database.

def create(attributes)
  columns = attributes.keys.join(", ")
  values = attributes.collect {|k, v| "'#{v}'"}.join(", ")
  @@connection.execute("INSERT INTO base (#{columns}) VALUES (#{values});")
  new(attributes)
end

{ 0 comments }

While working on NORM, I found a nice trick where you can enclose your class variables and methods inside a class << self … end block. I was aware of the ability to create class methods by using the def ClassName.method_name definition, but where I’m creating a series of class methods in NORM, I found this notation to be more useful.

I’m sure there will be more ruby stuff yet to be learned.

{ 0 comments }