Class: Excelsieur::Import

Inherits:
Object
  • Object
show all
Includes:
Mapping, Source, Transaction
Defined in:
lib/excelsieur/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transaction

included

Methods included from Mapping

included, #map_row_values

Methods included from Source

included

Constructor Details

#initialize(file = nil) ⇒ Import

Returns a new instance of Import.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/excelsieur/import.rb', line 22

def initialize(file = nil)
  self.source = file || self.class.source_file
  self.fields = self.class.fields

  @doc = ::SimpleXlsxReader.open(source)
  @sheet = @doc.sheets.first

  @columns = @sheet.rows.shift
  @rows = @sheet.rows

  @result = Result.new(@rows.length)
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



19
20
21
# File 'lib/excelsieur/import.rb', line 19

def columns
  @columns
end

#fieldsObject

Returns the value of attribute fields.



19
20
21
# File 'lib/excelsieur/import.rb', line 19

def fields
  @fields
end

#resultObject

Returns the value of attribute result.



19
20
21
# File 'lib/excelsieur/import.rb', line 19

def result
  @result
end

#rowsObject

Returns the value of attribute rows.



19
20
21
# File 'lib/excelsieur/import.rb', line 19

def rows
  @rows
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/excelsieur/import.rb', line 19

def source
  @source
end

Instance Method Details

#run(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/excelsieur/import.rb', line 35

def run(&block)
  check_columns!

  return if result.failed?

  if self.class.use_transaction
    run_with_transaction(&block)
  else
    insert_rows(&block)
  end

  result
end