Class: Excel2Yaml::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/excel2yaml/convert.rb

Instance Method Summary collapse

Constructor Details

#initializeConvert

Returns a new instance of Convert.



3
4
5
# File 'lib/excel2yaml/convert.rb', line 3

def initialize()
  @sheets = {}
end

Instance Method Details

#main_sheet=(value) ⇒ Object



38
39
40
# File 'lib/excel2yaml/convert.rb', line 38

def main_sheet=(value)
  @main_sheet = value
end

#process(source, main_sheet, ref_column) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/excel2yaml/convert.rb', line 7

def process(source, main_sheet, ref_column)
  self.source = source
  self.main_sheet = main_sheet
  @ref_column = ref_column
  self.read
  self.process_main
end

#process_mainObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/excel2yaml/convert.rb', line 15

def process_main
  @sheets[@main_sheet].rows.each(headers: true) do |row|
    file_path = "outputs/#{@target}"
    row["details"] = []
    @sheets[row[@ref_column]].rows.each(headers: true) do |sub_row|
      row["details"] << sub_row
    end
    save(file_path, row[@ref_column], row)
  end
end

#readObject



31
32
33
34
35
36
# File 'lib/excel2yaml/convert.rb', line 31

def read
  doc = SimpleXlsxReader.open(@source)
  doc.sheets.each do |sheet|
    @sheets[sheet.name] = sheet
  end
end

#save(file_path, file_name, content) ⇒ Object



26
27
28
29
# File 'lib/excel2yaml/convert.rb', line 26

def save(file_path, file_name, content)
  FileUtils.mkdir_p(file_path) unless File.directory?(file_path)
  File.open("#{file_path}/#{file_name}.yml", 'w') {|f| f.write content.to_yaml }
end

#source=(value) ⇒ Object



42
43
44
45
# File 'lib/excel2yaml/convert.rb', line 42

def source=(value)
  @target = value.split('/').last.split('.').first
  @source = value
end