Skip to content

Instantly share code, notes, and snippets.

@hogihung
Created May 5, 2014 13:48
Show Gist options
  • Save hogihung/367e1595bc14f1a85a63 to your computer and use it in GitHub Desktop.
Save hogihung/367e1595bc14f1a85a63 to your computer and use it in GitHub Desktop.
Rake task to export records to formatted : delimited CSV files.
require 'csv'
equip_dom = "/tmp/equip.db_new"
equip_mow = "/tmp/equip.db.mow_new"
namespace :oob_export do
task :domestic => :environment do
puts "Building equip.db file."
CSV.open(equip_dom, 'w+', col_sep: ':') do |csv|
Baytech.where(region: "USA").all.each do |baytech|
port = "X"
csv << [baytech.name, baytech.phone_num, baytech.ip_addr, port, baytech.modem_type, port]
baytech.devices.each do |device|
csv << [device.name, device.baytech.phone_num, device.baytech.ip_addr, device.pri_port, device.baytech.modem_type, device.pri_port]
end
end
end
end
task :mow => :environment do
puts "Building equip.db.mow file."
CSV.open(equip_mow, 'w+', col_sep: ':') do |csv|
Baytech.where("region != ?", "USA").all.each do |baytech|
port = "X"
csv << [baytech.name, baytech.phone_num, baytech.ip_addr, port, baytech.modem_type, port]
baytech.devices.each do |device|
csv << [device.name, device.baytech.phone_num, device.baytech.ip_addr, device.pri_port, device.baytech.modem_type, device.pri_port]
end
end
end
end
task :all => [:domestic, :mow] do
puts "Completed building of equip.db and equip.db.mow files."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment