Camping for IT
Posted by Jon Gretar on December 06, 2006 at 08:50 PM
Camping is a great micro-framework from the legendary _why the lucky stiff. It's small size and simplicity give me even more enjoyment than when I work with Rails. But it hasn't gotten the recognition as a tool for companies as it deserves. In many ways Camping should have easier access into the hearts and minds of the system administrators and internal company coders then Rails has. A lot of what is written internally for a company is little tools for small changes here and there. Passwords need to be changed and the business software needs to export new usernames or change account informations automatically. Historically this has usually been done with perl cgi-bin scripts or even small PHP scripts. We could start using Ruby cgi-bin scrips from ground up but that wouldn't be very ruby'ish of us would it.
But you do run into a few problems when you start using Camping like that. Camping's conventions assume that you are writing something from the ground up. It assumes that you want to use sqlite but if you use mysql then it assumes that everything is in a single database. But things tend to be different when we write our little helper tools. We might want to build a logviewer to look at some logs in a mysql table and then we have to compare that to a user table in a totally different server. Luckily, this being ruby, that is easily fixable in an elegant way.
Let's write a YAML config file. We could do this all inside the code but I prefer not having anything that needs to be configure inside my code. So make a file called database.yml and write inside it:
:radcheck:
:adapter: mysql
:host: auth.example.com
:database: radauth
:username: USER
:password: PASS
:radpostauth:
:adapter: mysql
:host: syslog.example.com
:database: radlog
:username: USER
:password: PASS
Easy enough. Now we need to load the confuration into the camping file. Easily enough done. Just make the start of your file look something like this
#!/usr/bin/env ruby
require `camping`
require `yaml`
Markaby::Builder.set(:indent, 2)
DBCONF = YAML.load(File.open(File.join(File.dirname(__FILE__), `database.yml`)))
Now the next step is to configure your models. First we need to load the database config to each model and we also need to correct what Camping expects the table's name to be. Because camping expects it's name to be appname__table. So just make the model definition look something like this.
module Appname::Models
class RadCheck < Base
self.establish_connection(DBCONF[:radcheck])
self.table_name = `radcheck`
end
class RadPostAuth < Base
self.establish_connection(DBCONF[:radpostauth])
self.table_name = `radpostauth`
end
end
And now you should be able to use the models normally. Hope this helps anyone.
Comments
There are 0 comments on this post. Post yours →
Post a comment
Required fields in bold.