I have a situation where I need to download data from CSV files, which rarely change and daily data is updated from the Internet. I will give a somewhat complete example of how to make the first.
First I have a rake file in lib/tasks/update.rake :
require 'update/from_csv_files.rb' namespace :update do task :csvfiles => :environment do Dir.glob('db/static_data/*.csv') do |file| Update::FromCsvFiles.load(file) end end end
=> :environment means that we will have access to the database using regular models.
Then I have the code in the lib/update/from_csv_files.rb to do the actual work:
require 'csv' module Update module FromCsvFiles def FromCsvFiles.load(file) csv = CSV.open(file, 'r') csv.each do |row| id = row[0] s = Statistic.find_by_id(id) if (s.nil?) s = Statistic.new s.id= id end s.survey_area = row[1] s.nr_of_space_men = row[2] s.save end end end end
Then I can just run rake update:csvfiles whenever the CSV files change to load new data. I also have another task that is configured similarly to updating my daily data.
In your case, you should be able to write code to load YML files or do your calculations directly. To handle your smaller fixes, you can create a general method for loading YML files and invoke it using specific files from the rake task. Therefore, you need to include the YML file and update the rake file with the new task. To process the execution order, you can make a rake command that calls other rake tasks in the appropriate order. Now I'm just throwing some ideas, you know better than me.
source share