Mark Needham

Thoughts on Software Development

Archive for the ‘rails’ tag

Ruby: Parameterising with ActiveResource

without comments

We've been using Ruby/Rails on my current project to create a RESTful web service. One of the problems we wanted to solve was making the data queried by this web service configurable from our build.

We started off with the following bit of code (which makes use of the recently added ActiveResource class):

1
2
3
class MyClass < ActiveResource::Base
  self.site = "http://localhost:3000/"
end

And then called this class as follows:

1
MyClass.create(:param => "param-value")

This worked fine for us until we wanted to parameterise the 'site' value so that we could set it to different values depending which build we were running (dev/ci/qa). We tried all the obvious ways – overriding the constructor and passing in the site, trying to set the site by calling MyClass.site but none of them did what we wanted. We eventually ended up creating a new method to create an instance of the class with our configurable site:

1
2
3
4
5
6
7
8
class MyClass < ActiveResource::Base

def instance(site, args)
  self.site = site
  new(args) unless args.nil?
end

end

We then call the code like this:

1
2
my_class = MyClass.instance("http://localhost:3000", :param => "param-value")
my_class.save

It seems like a bit of a hack but it got it working!

Out of interest it has taken me ages to try and find a way to put the Ruby code on here in a readable format. I tried to use the TextMate exporter but that wasn't giving me any love. I eventually ended up using Spotlight, a neat little tool written by Tyler Jennings. I found it from Jake Scruggs blog post.

Written by Mark Needham

August 8th, 2008 at 10:16 pm

Posted in Ruby

Tagged with , , ,

Watching a master at work

without comments

I've always found it fascinating watching people who really excel in their field going about business, be it footballers, tennis players, actors, whoever.

This week at TWU I've been playing around with some Ruby on Rails as I mentioned in the previous post, and yesterday I had the opportunity to watch one of the leading figures in the Ruby on Rails field at work. Take a bow Obie Fernandez, who gave several of the TWU attendees a demonstration of how to develop applications using Ruby on Rails. It was actually bordering on the severely impressive watching the speed at which he thought through concepts and then transformed them into code.

I also realised that the way I'd been using the language previously had been dubious at best. I hadn't realised that you can take care of database creation without using MySQL Administrator, nor had I realised quite how 'clever' Ruby on Rails is at mapping database tables to models created in the application.

It was almost painful watching how simple it is to do arduous tasks, such as creating textboxes and drop down boxes, in Ruby on Rails and it almost made me want to weep as I recalled the many hours I've spend using PHP and C# tweaking interaction between code and stored procedures/queries.

So +1 for Ruby on Rails and I certainly hope to improve my ability with it over the next few weeks.

Written by Mark

September 2nd, 2006 at 1:01 am

Posted in Ruby

Tagged with , , ,