class Procrastinator::Loader::CSVLoader
Simple Task
I/O object that writes task information (ie. TaskMetaData
attributes) to a CSV file.
@author Robin Miller
Constants
- DEFAULT_FILE
- HEADERS
ordered
Public Class Methods
new(file_path = DEFAULT_FILE)
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 18 def initialize(file_path = DEFAULT_FILE) @path = Pathname.new(file_path) if @path.directory? || @path.to_s.end_with?('/') @path += DEFAULT_FILE elsif @path.extname.empty? @path = Pathname.new("#{ file_path }.csv") end end
Public Instance Methods
create(queue:, run_at:, initial_run_at:, expire_at:, data: '')
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 42 def create(queue:, run_at:, initial_run_at:, expire_at:, data: '') existing_data = begin read rescue Errno::ENOENT [] end max_id = existing_data.collect { |task| task[:id] }.max || 0 new_data = { id: max_id + 1, queue: queue, run_at: run_at, initial_run_at: initial_run_at, expire_at: expire_at, attempts: 0, data: data } write(existing_data + [new_data]) end
delete(id)
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 80 def delete(id) existing_data = begin read rescue Errno::ENOENT [] end existing_data.delete_if do |task| task[:id] == id end write(existing_data) end
read()
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 28 def read data = CSV.table(@path.to_s, force_quotes: false).to_a headers = data.shift data.collect do |d| hash = Hash[headers.zip(d)] hash[:data] = hash[:data].gsub('""', '"') hash end end
update(id, data)
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 64 def update(id, data) existing_data = begin read rescue Errno::ENOENT [] end task_data = existing_data.find do |task| task[:id] == id end task_data.merge!(data) write(existing_data) end
write(data)
click to toggle source
# File lib/procrastinator/loaders/csv_loader.rb, line 94 def write(data) lines = data.collect do |d| CSV.generate_line(d, headers: HEADERS, force_quotes: true) end @path.dirname.mkpath @path.open('w') do |f| f.puts HEADERS.join(',') f.puts lines.join end end