Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@huezoaa
Last active September 22, 2015 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huezoaa/9142660a6d351113b01e to your computer and use it in GitHub Desktop.
Save huezoaa/9142660a6d351113b01e to your computer and use it in GitHub Desktop.
ah_lecture_notes_01-20-2015
require 'csv'
class ConfPanel < ActiveRecord::Base
validates :code, presence: true
validates :name, presence: true
def self.import_from_csv(csv_file)
ConfPanel.transaction do
ConfPanel.destroy_all
# Parse CSV content
file = File.open(csv_file,'r:windows-1251:utf-8').read
content = CSV.parse(file)
# File.read returns a big string of the data in the csv file being read.
# 'r:windows-1251:utf-8' handles the conversion of windows to utf-8 encoding
# CSV.parse restructures the big string into an array data structure one row at a time.
# Check content:
if content.blank?
raise 'Empty conf_panels.csv file'
end
# Extract header
header = content.shift.compact.map do |h|
h.parameterize.underscore.to_sym
end
# shift = removes first element in the array, returns it, and "shifts" all other elements down by one
# compact = removes nil values from the array
# map = invokes given blck once for each element. Creates new array containing the values returned by the block
# parameterize:
# => space becomes dash (-)
# => comma becomes dash (-)
# => letters get lowercased
# => comma followed by space becomes single dash (-)
# => parenthesis are removed
# => periods are replaced with single dash (-)
# Get elements row index. (based on header index)
# index = Returns the index of the first object in array such that the object is == to obj.
code_index = header.index(:code)
name_index = header.index(:name)
standard_profile_index = header.index(:standard_profile)
specimen_source_index = header.index(:specimen_source)
test_type_index = header.index(:test_type)
confirm_negative_index = header.index(:confirm_negative)
content.each do |row|
next if row.compact.empty?
source_attributes = {
code: row[code_index].to_s.strip,
name: row[name_index].to_s.strip,
standard_profile: row[standard_profile_index].to_s.strip,
specimen_source: row[specimen_source_index].to_s.strip,
test_type: row[test_type_index].to_s.strip,
confirm_negative: row[confirm_negative_index].to_s.strip
} # we're building a hash which will save row by row to the table
ConfPanel.create!(source_attributes) # save this row.
print '.'
end
puts 'Import complete'
end
end
def self.export_to_csv(csv_file)
CSV.open(csv_file, 'w') do |csv_row|
csv_row << ["code", "name","standard_profile","specimen_source","test_type","confirm_negative"]
# ConfPanel.where("test_type = 'Other'").each do |db_row|
ConfPanel.all.each do |db_row|
# csv_row << db_row.attributes.values # this dumps ALL fields
csv_row << [db_row.attributes["code"],
"#{db_row.attributes["name"]}",
db_row.attributes["standard_profile"],
db_row.attributes["specimen_source"],
db_row.attributes["test_type"],
db_row.attributes["confirm_negative"]]
print '*'
end
end
puts 'Export Complete'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment