lib/optimiser/workbook_pruner.rb in rubyfromexcel-0.0.4 vs lib/optimiser/workbook_pruner.rb in rubyfromexcel-0.0.5
- old
+ new
@@ -3,17 +3,19 @@
class WorkbookPruner
attr_reader :workbook, :cells_to_keep, :output_sheet_names
def initialize(workbook)
@workbook = workbook
+ @depends_on_input_sheet_cache = {}
end
def prune_cells_not_needed_for_output_sheets(*output_sheet_names)
@cells_to_keep = {}
workbook.work_out_dependencies
@output_sheet_names = output_sheet_names
find_dependencies_of output_sheet_names
+ # breakpoint
delete_cells_that_we_dont_want_to_keep
cells_to_keep = nil # So that we can garbage collect
GC.start
end
@@ -52,11 +54,10 @@
workbook.worksheets.each do |name,sheet|
puts "Converting cells into values in #{name}"
sheet.cells.each do |reference,cell|
next if cell.is_a?(ValueCell)
unless depends_on_input_sheets?(cell,input_sheet_names)
- # puts "converting #{cell}"
count = count + 1
sheet.replace_cell(reference,ValueCell.for(cell))
end
end
end
@@ -64,9 +65,14 @@
GC.start
prune_cells_not_needed_for_output_sheets(*output_sheet_names)
end
def depends_on_input_sheets?(cell,input_sheet_names,stack_level = 0)
+ return @depends_on_input_sheet_cache[cell] if @depends_on_input_sheet_cache.has_key?(cell)
+ @depends_on_input_sheet_cache[cell] = work_out_if_depends_on_input_sheets(cell,input_sheet_names,stack_level)
+ end
+
+ def work_out_if_depends_on_input_sheets(cell,input_sheet_names,stack_level)
return true if stack_level > 100
return false unless cell
return true if input_sheet_names.include?(cell.worksheet.variable_name)
return false unless cell.dependencies
return true if cell.dependencies.any? do |reference|
\ No newline at end of file