Vidar 09 February 2009
Custom script/console

UPDATE: This is a somewhat stupid way of doing it since all you need to do is


irb -r application_name.rb

from the root of your sinatra application. You will then have full access to database and everything else in your application in the console. Read on if you want a pure database console.

I recently started my first Sinatra project and I’m using datamapper as an ORM. Using the script/console in Ruby on Rails to manipulate the database and do manual test for my models is something I have come to depend on. Using the bare bone web framework Sinatra, you don’t have this privelege, or do you?

Using Sinatra requires you to manually load all gems and libs that your application needs. In my application I have the following code (init_datamapper.rb):

require 'dm-core'
require 'dm-validations'

DataMapper.setup(:default, “sqlite3:///#{Dir.pwd}/dev.db”)
DataMapper::Logger.new(STDOUT, :debug)
%w( user post comment tag ).each do |lib|
require File.join(File.dirname(FILE), ‘lib’, lib)
end


The code will import the datamapper libraries and setup the connection, load your models (from my lib directory). What’s nice is that this is exactly what I need to create my fully operational database console!

irb
load 'init_datamapper.rb'

Fire up irb and load the file. Done.

« Back to posts Write a new comment

Feed-icon 0 Comments

Textilized formatting

How to use the text area
_a phrase_ a phrase
*a phrase* a phrase
_*a phrase*_ a phrase
"Link":http://link.com Example link
Complete reference