decompose ActiveRecord script into methods

T

Thufir

If I commend out the def and end lines for the "all" method, the script
runs fine. However, the goal is to decompose the script into several
methods, which fails with:

thufir@ARRAKIS:~/projects/aggregator$
thufir@ARRAKIS:~/projects/aggregator$ ruby rss2mysql.rb
rss2mysql.rb:19: class definition in method body
thufir@ARRAKIS:~/projects/aggregator$


The script is adapted from:


Practical Ruby Gems
http://www.apress.com/book/view/9781590598115
Chapter 5




require 'rubygems'
require 'active_record'
require 'feed_tools'
require 'yaml'



def all

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 Items < ActiveRecord::Base
end


# If the table doesn't exist, we'll create it.
unless Items.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




feed_url = 'http://www.slashdot.org/index.rss'

feed=FeedTools::Feed.open(feed_url)
feed.items.each do |feed_item|
unless (Items.find_by_title(feed_item.title) \
or Items.find_by_url(feed_item.link) \
or Items.find_by_guid(feed_item.guid))
puts "processing item '#{feed_item.title}' - new"


Items.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 #end all




thanks,

Thufir
 
P

Paul Smith

If I commend out the def and end lines for the "all" method, the script
runs fine. =A0However, the goal is to decompose the script into several
methods, which fails with:

thufir@ARRAKIS:~/projects/aggregator$
thufir@ARRAKIS:~/projects/aggregator$ ruby rss2mysql.rb
rss2mysql.rb:19: class definition in method body
thufir@ARRAKIS:~/projects/aggregator$

The error message tells you exactly what is wrong:
class Items < =A0ActiveRecord::Base
end

You can't have a class declaration like this inside a method body.

Instead of making a single method around the whole script, make at
least 2 methods, one which covers the code before the class
declaration, then one which covers the code after it. Or move the
class definition to the top of the file, before def all.
thanks,

Thufir



--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
T

Thufir

The error message tells you exactly what is wrong:


You can't have a class declaration like this inside a method body.

Instead of making a single method around the whole script, make at least
2 methods, one which covers the code before the class declaration, then
one which covers the code after it. Or move the class definition to the
top of the file, before def all.

Ah, before the class, that makes sense. I suppose what I was wanting was
to declare the class in a separate file and then "require 'Items.rb'",
how would that work?


thanks,

Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top