# Rakefile invoking ruby script

**URL:** <https://www.ruby-forum.com/t/rakefile-invoking-ruby-script/175856>\
**Category:** Ruby\
**Created:** [September 21, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/rakefile-invoking-ruby-script/175856 "2009-09-21T16:18:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 21, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/rakefile-invoking-ruby-script/175856/1 "2009-09-21T16:18:38Z")

</div>

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(‘[Slashdot](http://www.slashdot.org/index.rss)’)  
feed = FeedTools::Feed.open(‘[www.amazon.com/rss/tag/blu-ray/new](http://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
