Rake task organization

in the “connects” task for the following rakefile, how do I tie-in Item
and Items so that there’s minimal code repetition? For instance,
currently Item is defined in the connect task for the rakefile, wheras I
would rather that Item be “imported” either by requiring a module or
including the Item.rb file, for example.

What’s a good approach?

[email protected]:~/rb$
[email protected]:~/rb$ cat Item.rb
require ‘rubygems’
require ‘active_record’

class Item < ActiveRecord::Base
end
[email protected]:~/rb$
[email protected]:~/rb$ cat Items.rb
class Items < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.column :title, :string
t.column :content, :string
t.column :source, :string
t.column :url, :string
t.column :timestamp, :timestamp
t.column :keyword_id, :integer
t.column :guid, :string
t.column :html, :string
end
end
def self.down
drop_table :items
end
end

[email protected]:~/rb$
[email protected]:~/rb$ cat rakefile.rb
require ‘rubygems’
require ‘active_record’
require ‘feed_tools’
require ‘yaml’

desc “connects to db”
task :connect do
db = YAML.load_file(“database.yml”)
ActiveRecord::Base.establish_connection(
:adapter => db[“adapter”],
:host => db[“host”],
:username => db[“username”],
:password => db[“password”],
:database => db[“database”])
class Item < ActiveRecord::Base ###take out this line?
end
puts “connected to db”
end

desc “drop table items”
task :drop => :connect do
#drop table items
puts “unable to drop table”
end

desc “creates table items unless exists”
task :create => :connect do
#create table items
puts “unable to create table”
end

desc “populate db”
task :populate => [:connect, :create] do
puts “get data”
end

desc “clobber the db”
task :clobber => [:connect, :drop, :create] do
puts “clobber it!”
end
[email protected]:~/rb$
[email protected]:~/rb$

thanks,

Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs