Skip to content

Instantly share code, notes, and snippets.

@chrisortman
Created November 27, 2017 19:32
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 chrisortman/15792f18ae0bf985e85c4ef2a31249c7 to your computer and use it in GitHub Desktop.
Save chrisortman/15792f18ae0bf985e85c4ef2a31249c7 to your computer and use it in GitHub Desktop.
Converts constants.yml values to CSV for import into permissible values
require 'yaml'
require 'csv'
require 'pathname'
SPARC_HOME = Rails.root
CONSTANTS_FILE = SPARC_HOME + "config/constants.yml"
OUTPUT_DIR = SPARC_HOME + "db/seeds/permissible_values/2.0.5"
EXTRACT_KEYS = {
"affiliations" => "affiliation_types",
"document_types" => "document_types",
"federal_grant_codes" => "federal_grant_codes",
"federal_grant_non_phs_sponsors" => "federal_grant_non_phs_sponsors",
"federal_grant_phs_sponsors" => "federal_grant_phs_sponsors",
"funding_sources" => "funding_sources",
"funding_statuses" =>"funding_statuses",
"impact_areas" => "impact_areas",
"potential_funding_sources" => "potential_funding_sources",
"proxy_rights" => "proxy_rights",
"available_statuses" => "statuses",
"study_types" => "study_types",
"submission_types" => "submission_types",
"subspecialties" => "subspecialties",
"user_credentials" => "user_credentials",
"user_roles" =>"user_roles"
}
raise "Could not find SPARC directory #{SPARC_HOME}" unless SPARC_HOME.exist?
raise "Could not find constnts file #{CONSTANTS_FILE}" unless CONSTANTS_FILE.exist?
constants = YAML.load_file CONSTANTS_FILE
EXTRACT_KEYS.each do |yaml_key,csv_name|
output_file = OUTPUT_DIR + "#{csv_name}.csv"
type = csv_name.singularize
data = constants[yaml_key]
puts "Writing #{data.size} records from #{yaml_key} to #{output_file}"
row_num = 0
rows = data.map do |key, value|
row_num += 1
case csv_name
when "document_types","federal_grant_codes", "federal_grant_non_phs_sponsors",
"federal_grant_phs_sponsors", "funding_sources", "funding_statuses",
"potential_funding_sources", "proxy_rights", "submission_types", "subspecialties",
"user_credentials","user_roles"
["",value,key, "", "",row_num,type,"","" ]
when "impact_areas", "statuses","study_types"
["",key, value, "", "",row_num,type,"","" ]
else
["",key, value, "", "","",type,"","" ]
end
end
# Create CSV file
CSV.open(output_file, "wb") do |csv|
csv << %w(id key value concept_code parent_id sort_order category default reserved)
rows.each{ |row| csv << row }
end
# Remove the now defunct data from our file
constants.delete yaml_key
end
File.open(CONSTANTS_FILE, "wb") do |f|
constants.to_yaml(f)
end
@chrisortman
Copy link
Author

I run this using

rails runner <path_to_script> from within my SPARC repository

We had some funkiness with some of our yaml which is why you see key and value transposed on line 48.
I ran this with a clean working directory and then did git diff to check the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment