Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created June 18, 2016 05:07
Show Gist options
  • Save johana-star/8060600bdc0ad51c820061dd4ee4d2cd to your computer and use it in GitHub Desktop.
Save johana-star/8060600bdc0ad51c820061dd4ee4d2cd to your computer and use it in GitHub Desktop.
require "active_record"
require "./app/models/task"
require "csv"
class OmnifocusImporter
class FileNotFound < StandardError; end
def initialize(file)
raise FileNotFound unless File.exist? file
CSV::Converters[:blank_to_nil] = lambda do |field|
field && field.empty? ? nil : field
end
@csv = CSV.new(
IO.read(file),
headers: true,
header_converters: :symbol,
converters: [:all, :blank_to_nil]
).to_a
end
def import_to_active_record
@csv.map(&:to_hash).
select(&is_action?).
map(&convert_flagged_to_boolean).
map(&rename_type_to_type_of_object).
map(&filter_attributes_for_task_schema).
each(&find_or_create_task)
end
private
def is_action?
lambda { |row| row[:type] == "Action" }
end
def convert_flagged_to_boolean
lambda { |row| row[:flagged] = row[:flagged] == 1 ? true : false; row }
end
def rename_type_to_type_of_object
lambda { |row| row[:type_of_object] = row[:type] ; row }
end
def filter_attributes_for_task_schema
lambda { |row| row.select { |key, _v| Task::ATTRIBUTES.include? key } }
end
def find_or_create_task
lambda { |arg| Task.find_or_create_by arg }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment