Class: I18nGenerator::ExcelParser
- Inherits:
-
Object
- Object
- I18nGenerator::ExcelParser
- Defined in:
- lib/i18n_generator/excel_parser.rb
Overview
Responsible for parsing Excel spreadsheet to an array of Label objects and inspecting a spreadsheet to return a list of included translations
Constant Summary collapse
- FIRST_DATA_ROW_NUMBER =
2
- FIRST_TRANSLATION_COLUMN =
3
- LABEL_ID_COLUMN_INDEX =
1
- LABEL_DESCRIPTION_COLUMN_INDEX =
2
Instance Method Summary collapse
-
#excel_sheet_from_path(file_path) ⇒ Object
Returns the appropraite Roo Excel object from a given path.
-
#initialize(file_path) ⇒ ExcelParser
constructor
A new instance of ExcelParser.
-
#parse ⇒ Object
Parses the spreadsheet to a list of Labels.
-
#translation_languages ⇒ Object
Returns an array of symbols representing the translation languages contained in the Excel spreadsheet.
Constructor Details
#initialize(file_path) ⇒ ExcelParser
Returns a new instance of ExcelParser.
18 19 20 21 22 23 24 25 26 |
# File 'lib/i18n_generator/excel_parser.rb', line 18 def initialize(file_path) raise 'Invalid path supplied to ExcelParser' unless file_path begin @excel_data = excel_sheet_from_path(file_path) rescue Exception => e raise "Error while creating excel data: #{e}" end end |
Instance Method Details
#excel_sheet_from_path(file_path) ⇒ Object
Returns the appropraite Roo Excel object from a given path
31 32 33 34 35 36 37 38 |
# File 'lib/i18n_generator/excel_parser.rb', line 31 def excel_sheet_from_path(file_path) case File.extname(file_path) when '.xls' Roo::Excel.new(file_path) when '.xlsx' Roo::Excelx.new(file_path) end end |
#parse ⇒ Object
Parses the spreadsheet to a list of Labels
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/i18n_generator/excel_parser.rb', line 42 def parse labels = [].tap do |labels_array| params_hash = {} FIRST_DATA_ROW_NUMBER.upto @excel_data.last_row do |row_num| params_hash[:id] = @excel_data.cell(row_num, LABEL_ID_COLUMN_INDEX) params_hash[:description] = @excel_data.cell(row_num, LABEL_DESCRIPTION_COLUMN_INDEX) FIRST_TRANSLATION_COLUMN.upto @excel_data.last_column do |col_num| params_hash[@excel_data.cell(FIRST_DATA_ROW_NUMBER-1, col_num).to_sym] = @excel_data.cell(row_num, col_num) end labels_array << Label.new(params_hash) end end end |
#translation_languages ⇒ Object
Returns an array of symbols representing the translation languages contained in the Excel spreadsheet
63 64 65 66 |
# File 'lib/i18n_generator/excel_parser.rb', line 63 def translation_languages header_row = @excel_data.row(@excel_data.first_row) header_row[FIRST_TRANSLATION_COLUMN-1,header_row.size].map(&:to_sym) end |