Here I have the start of a news posting app I'm working on in ruby. Creating an article works fine, but I'm lost as to how to retrieve one. I'd like my Article.get method to return an article object,.. but it seems the only way to do this is to make all the Article attributes writable, which I don't really want to do. Are there any better ways to go about doing this?
require 'date'
require 'ftools'
require 'find'
module News
class Article
attr_reader :topic, :body, :created, :directory, :file_name, :file, :author
attr_writer :topic, :body, :created, :directory, :file_name, :file, :author
def initialize(author, topic, body)
@topic = topic
@body = body
@author = author
@created = DateTime.now()
@directory = 'archive/' + @created.strftime('%Y/%m') + '/'
@file_name = @created.strftime('%H-%M-%S')
@file = @directory + @file_name
File.makedirs(@directory) unless File.directory?(@directory)
if !File.file?(@file)
@article = File.new(@file, 'w', 0777)
@article.print "#{@topic}\n#{@author}\n#{@body}"
@article.close()
end
end
def Article.get(path)
#???
end
end