Class: Garden::Excel

Inherits:
Object
  • Object
show all
Defined in:
lib/garden/spreadsheets.rb

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Excel

Returns a new instance of Excel.



4
5
6
# File 'lib/garden/spreadsheets.rb', line 4

def initialize filepath
  open filepath
end

Instance Method Details

#open(filepath) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/garden/spreadsheets.rb', line 8

def open filepath
  puts "Planting spreadsheet: #{filepath}"
  
  @ss = Spreadsheet.open filepath
  @ss.worksheets.each do |table|
    puts "Parsing table #{table.name}"
    parse_table table
  end
  
end

#parse_table(table) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/garden/spreadsheets.rb', line 19

def parse_table table
  # The table object passed in is a Spreadsheet::Worksheet instance.

  table_mediator = Mediators::Table.new table.name
  if !table_mediator.valid?
    return
  end
  
  # Get the headers. These values will be the attribute names
  headers = table_mediator.parse_headers table.first.to_a
  
  # Now loop the table rows, inserting records.
  table.each do |row|
    next if row.idx == 0
    # puts '...............'
    
    table_mediator.create_instance parse_worksheet_row(headers, row)
  end
end

#parse_worksheet_row(keys, values) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/garden/spreadsheets.rb', line 39

def parse_worksheet_row keys, values
  h = {}
  keys.each_index do |index|
    key = keys[index].to_sym
    h[key] = values[index]
  end
  h
end