class Bayonet::Workbook

Constants

WORKBOOK_PATH

Attributes

file[R]

Public Class Methods

new(file) click to toggle source
# File lib/bayonet/workbook.rb, line 9
def initialize(file)
  @file = file
end

Public Instance Methods

close() click to toggle source
# File lib/bayonet/workbook.rb, line 36
def close
  zip_file.close
end
on_sheet(sheet_name, &block) click to toggle source
# File lib/bayonet/workbook.rb, line 21
def on_sheet(sheet_name, &block)
  sheet = find_sheet_by_name_and_cache(sheet_name)
  block.call(sheet)
end
write(file) click to toggle source
# File lib/bayonet/workbook.rb, line 26
def write(file)
  Zip::File.open(file, Zip::File::CREATE) do |destination|
    zip_file.each do |entry|
      destination.get_output_stream(entry.name) do |file_stream|
        write_modifications(entry, file_stream)
      end
    end
  end
end
write_and_close(file) click to toggle source
# File lib/bayonet/workbook.rb, line 40
def write_and_close(file)
  write(file)
  close
end
xml() click to toggle source
# File lib/bayonet/workbook.rb, line 17
def xml
  @xml ||= read_and_parse_xml
end
zip_file() click to toggle source
# File lib/bayonet/workbook.rb, line 13
def zip_file
  @zip_file ||= Zip::File.open(file)
end

Private Instance Methods

find_sheet_by_name_and_cache(sheet_name) click to toggle source
# File lib/bayonet/workbook.rb, line 49
def find_sheet_by_name_and_cache(sheet_name)
  return modified_sheets[sheet_name] if modified_sheets.key?(sheet_name)

  sheet = Bayonet::Sheet.new(sheet_name, self)
  modified_sheets[sheet_name] = sheet
  sheet
end
find_sheet_by_path(path) click to toggle source
# File lib/bayonet/workbook.rb, line 57
def find_sheet_by_path(path)
  modified_sheets.values.detect { |sheet| sheet.path == path }
end
modified_sheets() click to toggle source
# File lib/bayonet/workbook.rb, line 70
def modified_sheets
  @modified_sheets ||= {}
end
read_and_parse_xml() click to toggle source
# File lib/bayonet/workbook.rb, line 74
def read_and_parse_xml
  entry = zip_file.find_entry(WORKBOOK_PATH)
  Nokogiri::XML.parse(entry.get_input_stream)
end
write_modifications(entry, file_stream) click to toggle source
# File lib/bayonet/workbook.rb, line 61
def write_modifications(entry, file_stream)
  sheet = find_sheet_by_path(entry.name)
  if sheet
    file_stream.write(sheet.xml.to_s)
  else
    file_stream.write(zip_file.read(entry.name))
  end
end