Rakefile invoking ruby script

I’m just doing ruby and rake, and am consciously staying away from RoR
for the time being – I may build a GUI with Fox down the road.

Possibly I have completely the wrong approach, but I want the “drop”
task
to invoke/call the migration file, with something along the lines of:

system “rake db:migrate RAILS_ENV=production”

the migration file:

module Agg

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

the rake file:

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
end
puts “connected to db”
end

desc “drop table items”
task :drop => :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
end
#drop table if exists Item.table #fails
puts “dropped items”
#system “rake db:migrate RAILS_ENV=production”
#apparently this may invoke a migration?
end

desc “creates table items unless exists”
task :create => :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
end

unless Item.table_exists?
ActiveRecord::Schema.define do
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
end

end

desc “clobber the db”
task :clobber => [:connect, :drop, :create, :populate, :iterate] do
puts “clobber”
end

desc “gets rss data”
task :populate => :connect do
#feed = FeedTools::Feed.open(‘http://www.slashdot.org/index.rss’)
feed = FeedTools::Feed.open(‘www.amazon.com/rss/tag/blu-ray/new’)

feed.items.each do |feed_item|
    unless (Item.find_by_title(feed_item.title)\
      or Item.find_by_url(feed_item.link)\
      or Item.find_by_guid(feed_item.guid))
        puts "processing item '#{feed_item.title}' - new"

  Item.new do |newitem|
    newitem.title=feed_item.title.gsub(/<[^>]*>/, '')
    newitem.guid=feed_item.guid
    if feed_item.publisher.name
      newitem.source=feed_item.publisher.name
    end
    newitem.url=feed_item.link
    newitem.content=feed_item.description
    newitem.timestamp=feed_item.published
    newitem.save
  end

else
    puts "processing item '#{feed_item.title}' - old"
end

end
end

desc “query”
task :iterate => [:connect] do
end

How can I make the rakefile more concise by drawing more upon the
migration and model?

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