Class: MiddleDrive::Data
- Inherits:
-
Object
- Object
- MiddleDrive::Data
- Defined in:
- lib/middle_drive/data.rb
Instance Method Summary collapse
- #build(path) ⇒ Object
- #build_list(sheet) ⇒ Object
-
#initialize(site) ⇒ Data
constructor
A new instance of Data.
Constructor Details
#initialize(site) ⇒ Data
Returns a new instance of Data.
4 5 6 |
# File 'lib/middle_drive/data.rb', line 4 def initialize(site) @data_document = site.spreadsheets.select { |s| s.title == 'data' }.first end |
Instance Method Details
#build(path) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/middle_drive/data.rb', line 8 def build(path) data_path = "#{path}/data" Dir.mkdir(data_path) unless File.exists?(data_path) @data_document.worksheets.each do |sheet| save_to = "#{data_path}/#{sheet.title}.yml" data = {} puts "Building list named #{sheet.title}" data_type = sheet[1, 1] if data_type == 'array' or data_type == 'hash' data[sheet.title] = [] if data_type == 'array' data[sheet.title] = {} if data_type == 'hash' (2..sheet.num_rows).each do |row| data[sheet.title] << sheet[row, 1] if data_type == 'array' # only one column data[sheet.title][sheet[row, 1]] = sheet[row, 2] if data_type == 'hash' end elsif data_type == 'list' data = build_list(sheet) end File.write(save_to, data.to_yaml) end end |
#build_list(sheet) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/middle_drive/data.rb', line 35 def build_list(sheet) data = { sheet.title => [] } (2..sheet.num_rows).each do |row| list_object = {} (2..sheet.num_cols).each do |col| list_object[sheet[1, col]] = sheet[row, col] end data[sheet.title] << list_object end data end |