module DinosaurCatalog::CsvModifier
Constants
- ALIAS_TABLE
- NORMALIZED_CSV_FILENAME
Public Instance Methods
build_table_body(header)
click to toggle source
# File lib/dinosaur_catalog/csv_modifier.rb, line 37 def build_table_body(header) header.inject([]) do |new_header, column_name| if ALIAS_TABLE[column_name] new_header << ALIAS_TABLE[column_name] else new_header << column_name end end end
normalize_csv_body(old_csv_file, header_row)
click to toggle source
# File lib/dinosaur_catalog/csv_modifier.rb, line 18 def normalize_csv_body(old_csv_file, header_row) new_headers = replace_header_with_alias(old_csv_file) #CSV.open(NORMALIZED_CSV_FILENAME, 'w') do |csv| new_csv_file = CSV.generate do |csv| csv << header_row CSV.read(old_csv_file, headers: true).each do |row| row['Carnivore'] = 'Carnivore' if row['Carnivore'] == 'Yes' row['Carnivore'] = 'No' if row['Carnivore'] == 'No' csv.puts row end end new_csv_file end
normalize_csv_file(filename)
click to toggle source
# File lib/dinosaur_catalog/csv_modifier.rb, line 13 def normalize_csv_file(filename) new_header_names = replace_header_with_alias(filename) normalize_csv_body(filename, new_header_names) end
replace_header_with_alias(csv_file)
click to toggle source
# File lib/dinosaur_catalog/csv_modifier.rb, line 32 def replace_header_with_alias(csv_file) header_row = CSV.read(csv_file, headers: true).headers build_table_body(header_row) end