Class: Applocale::ParseCSV
- Inherits:
-
Object
- Object
- Applocale::ParseCSV
- Defined in:
- lib/applocale/Core/ParseCSV/parse_csv.rb
Instance Method Summary collapse
- #find_header(sheet, rows) ⇒ Object
- #handle_duplicate_key_if_any!(row_content) ⇒ Object
-
#initialize(platfrom, csv_directory, langlist, sheetobj_list) ⇒ ParseCSV
constructor
A new instance of ParseCSV.
- #parse ⇒ Object
- #parse_row(sheet_name, index, row, key_header_info, language_header_list) ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(platfrom, csv_directory, langlist, sheetobj_list) ⇒ ParseCSV
Returns a new instance of ParseCSV.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 21 def initialize(platfrom, csv_directory, langlist, sheetobj_list) @platform = platfrom @csv_directory = csv_directory @langlist = langlist @sheetobj_list = sheetobj_list @sheetcontent_list = Array.new @allkey_dict = {} @all_error = Array.new # puts "Start to Parse CSV: \"#{csv_directory}\" ...".green parse end |
Instance Method Details
#find_header(sheet, rows) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 63 def find_header(sheet, rows) sheet_name = sheet.sheetname sheet_info_obj = sheet.obj sheet_language_list = sheet_info_obj.lang_headers sheet_key_header = sheet_info_obj.key_header header_row_index = rows.index do |row| row.include?(sheet_key_header) end header_row_info = rows[header_row_index] unless header_row_index.nil? header_column_index = header_row_info&.index { |cell| cell == sheet_key_header } if header_row_index.nil? || header_column_index.nil? raise "ParseCSVError: Header not found in sheet #{sheet_name}" end key_header_info = ParseModelModule::KeyStrWithColNo.new(sheet_key_header, header_column_index) language_header_list = sheet_language_list.map do |key, value| cell_index = header_row_info.index { |cell| cell == value } cell_index.nil? ? nil : ParseModelModule::LangWithColNo.new(value, key, cell_index) end.compact unless language_header_list.length == sheet_language_list.length raise "ParseCSVError: Wrong language keys in sheet #{sheet_name}" end { header_row_index: header_row_index, key_header_info: key_header_info, language_header_list: language_header_list } end |
#handle_duplicate_key_if_any!(row_content) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 105 def handle_duplicate_key_if_any!(row_content) previous_row_content = @allkey_dict[row_content.key_str.downcase] if previous_row_content.nil? @allkey_dict[row_content.key_str.downcase] = row_content else raise "ParseCSVError:: Duplicate keys:\n sheet #{row_content.sheetname}, row: #{row_content.rowno}, key_str: #{row_content.key_str}\nduplicateWithSheet: #{previous_row_content.sheetname}, row: #{previous_row_content.rowno}, key_str: #{previous_row_content.key_str}" end end |
#parse ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 33 def parse @sheetcontent_list = @sheetobj_list.map do |sheet_obj| sheet_name = sheet_obj.sheetname sheet_content = ParseModelModule::SheetContent.new(sheet_name) csv_path = File.("#{sheet_name}.csv", @csv_directory) unless File.exist? csv_path ErrorUtil.warning("File does not exist: #{csv_path}") next end rows = CSV.read(csv_path) header = find_header(sheet_obj, rows) sheet_content.header_rowno = header[:header_row_index] sheet_content.keyStr_with_colno = header[:key_header_info] sheet_content.lang_with_colno_list = header[:language_header_list] rows.each_with_index do |row, index| next if sheet_content.header_rowno == index row_content = parse_row(sheet_name, index, row, sheet_content.keyStr_with_colno, sheet_content.lang_with_colno_list) handle_duplicate_key_if_any!(row_content) sheet_content.rowinfo_list.push(row_content) end sheet_content end end |
#parse_row(sheet_name, index, row, key_header_info, language_header_list) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 94 def parse_row(sheet_name, index, row, key_header_info, language_header_list) key_str = row[key_header_info.colno] unless ValidKey.is_validkey(@platform, key_str) raise "ParseCSVError: Invaild Key in sheet #{sheet_name}, row: #{index}, key_str: #{key_str}" end rowinfo = ParseModelModule::RowInfo.new(sheet_name, index, key_str) rowinfo.content_dict = Hash[language_header_list.map { |language_header| [language_header.lang, ContentUtil.from_excel(row[language_header.colno] || '')] }] rowinfo end |
#result ⇒ Object
59 60 61 |
# File 'lib/applocale/Core/ParseCSV/parse_csv.rb', line 59 def result @sheetcontent_list end |