Module: ActiveExcel
- Defined in:
- lib/active_excel.rb,
lib/active_excel/version.rb,
lib/active_excel/validated_records.rb
Defined Under Namespace
Classes: ValidatedRecords
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
Instance Method Details
#build_from_excel(file_path) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_excel.rb', line 7 def build_from_excel(file_path) workbook = RubyXL::Parser.parse(file_path) table_name = self.name.underscore.pluralize sheet = workbook[table_name] column_names = sheet[0].cells.map(&:value) captured_attributes = self.attribute_names.map do |attribute| if column_names.include?(attribute) [attribute.to_sym, column_names.index(attribute)] end end.compact.to_h records = [] sheet[1..-1].each do |row| if row attributes = captured_attributes.map do |attribute, index| [attribute, row[index].value] end.to_h records << self.new(attributes) end end ValidatedRecords.new(table_name, records) end |