Class: RubyFromExcel::Process

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Process

Returns a new instance of Process.



40
41
42
43
# File 'lib/rubyfromexcel.rb', line 40

def initialize(&block)
  instance_eval(&block) if block
  load_from_checkpoint
end

Instance Attribute Details

#checkpoint_directoryObject

Returns the value of attribute checkpoint_directory.



36
37
38
# File 'lib/rubyfromexcel.rb', line 36

def checkpoint_directory
  @checkpoint_directory
end

#convert_independent_of_input_sheetsObject

Returns the value of attribute convert_independent_of_input_sheets.



35
36
37
# File 'lib/rubyfromexcel.rb', line 35

def convert_independent_of_input_sheets
  @convert_independent_of_input_sheets
end

#debug_dont_write_checkpoint_after_stageObject

Returns the value of attribute debug_dont_write_checkpoint_after_stage.



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

def debug_dont_write_checkpoint_after_stage
  @debug_dont_write_checkpoint_after_stage
end

#prune_except_output_sheetsObject

Returns the value of attribute prune_except_output_sheets.



34
35
36
# File 'lib/rubyfromexcel.rb', line 34

def prune_except_output_sheets
  @prune_except_output_sheets
end

#skip_testsObject

Returns the value of attribute skip_tests.



33
34
35
# File 'lib/rubyfromexcel.rb', line 33

def skip_tests
  @skip_tests
end

#source_excel_directoryObject

Returns the value of attribute source_excel_directory.



30
31
32
# File 'lib/rubyfromexcel.rb', line 30

def source_excel_directory
  @source_excel_directory
end

#stageObject

Returns the value of attribute stage.



37
38
39
# File 'lib/rubyfromexcel.rb', line 37

def stage
  @stage
end

#target_ruby_directoryObject

Returns the value of attribute target_ruby_directory.



31
32
33
# File 'lib/rubyfromexcel.rb', line 31

def target_ruby_directory
  @target_ruby_directory
end

#workbookObject

Returns the value of attribute workbook.



32
33
34
# File 'lib/rubyfromexcel.rb', line 32

def workbook
  @workbook
end

Instance Method Details

#checkpoint(checkpoint_number) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rubyfromexcel.rb', line 147

def checkpoint(checkpoint_number)
  puts
  if self.stage > checkpoint_number
    puts "Stage #{checkpoint_number} already completed, skipping..."
    return
  else
    puts "Stage #{checkpoint_number}"
  end
  yield
  save_checkpoint if checkpoint_directory  # Then create a check point
  self.stage = checkpoint_number + 1
end

#checkpoint_filenameObject



192
193
194
# File 'lib/rubyfromexcel.rb', line 192

def checkpoint_filename
  File.join(checkpoint_directory,"checkpoint#{self.stage}.marshal")
end

#load_from_checkpointObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rubyfromexcel.rb', line 172

def load_from_checkpoint
  unless checkpoint_directory
    puts "No checkpoint directory given"
    return false
  end
  checkpoint_filenames = Dir.entries(checkpoint_directory)
  highest_stage_checkpoint = checkpoint_filenames.map { |filename| filename =~ /checkpoint(\d+)/ ? $1.to_i : 0 }.max
  checkpoint_to_open = File.join(checkpoint_directory,"checkpoint#{highest_stage_checkpoint}.marshal")
  unless File.exists?(checkpoint_to_open)
    puts "No checkpoint file found"
    return false
  end
  dumped_objects = Marshal.load(File.open(checkpoint_to_open))
  self.stage = dumped_objects.shift + 1
  self.workbook = dumped_objects.shift
  SheetNames.instance.replace(dumped_objects.shift)
  SharedStrings.instance.replace(dumped_objects.shift)
  puts "Stage #{self.stage-1} checkpoint found and loaded."
end

#prepare_destination_folderObject



131
132
133
134
# File 'lib/rubyfromexcel.rb', line 131

def prepare_destination_folder
  FileUtils.mkpath(File.join(target_ruby_directory,'specs'))
  FileUtils.mkpath(File.join(target_ruby_directory,'sheets'))
end

#reset_global_classesObject

FIXME: Urgh. Global variables. Need to eliminate these!



126
127
128
129
# File 'lib/rubyfromexcel.rb', line 126

def reset_global_classes
  SheetNames.instance.clear
  SharedStrings.instance.clear
end

#save_checkpointObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rubyfromexcel.rb', line 160

def save_checkpoint
  if debug_dont_write_checkpoint_after_stage && (self.stage > debug_dont_write_checkpoint_after_stage)
    puts "Debug mode: Not writing checkpoint for stage #{checkpoint_number}"
    return
  else
    puts "Writing checkpoint for stage #{self.stage}"
    FileUtils.mkpath(checkpoint_directory)
    objects_to_dump = [self.stage,self.workbook,SheetNames.instance.to_hash,SharedStrings.instance.to_a]
    File.open(checkpoint_filename,'w') { |f| f.puts Marshal.dump(objects_to_dump) }
  end
end

#start!(starting_stage = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubyfromexcel.rb', line 49

def start!(starting_stage = nil)
  self.stage = starting_stage || self.stage || 0
  
  checkpoint 0 do
    reset_global_classes
  
    time "Preparing destination folder..." do
      prepare_destination_folder
    end

    time "Loading..." do
      self.workbook = Workbook.new(workbook_filename)
    end
  end

  time "Pruning..." do    
    
    if prune_except_output_sheets || convert_independent_of_input_sheets
      workbook.work_out_dependencies
    end
    
    checkpoint 1 do
      if convert_independent_of_input_sheets
        workbook.convert_cells_to_values_when_independent_of_input_sheets(*convert_independent_of_input_sheets)
      end
    end
    
    checkpoint 2 do
      if prune_except_output_sheets
        workbook.prune_cells_not_needed_for_output_sheets(*prune_except_output_sheets)
      end
    end
    
  end
    
  time "Workbook contains #{workbook.worksheets.size} sheets:\n" do
    
    checkpoint 3 do
      puts "0) Generating ruby for the workbook"
      write "spreadsheet.rb" do
        workbook.to_ruby
      end
    end

    checkpoint 4 do
      i = 0
      workbook.worksheets.each do |variable_name, worksheet|
        time "#{i+=1}) Generating ruby for #{variable_name}..." do
          write 'sheets', "#{variable_name}.rb" do
            worksheet.to_ruby
          end
        end
      end
    end

    checkpoint 5 do
      unless skip_tests
        i = 0
        workbook.worksheets.each do |variable_name, worksheet|
          time "#{i+=1}) Generating spec for #{variable_name}..." do
            write 'specs',"#{variable_name}_rspec.rb" do
              worksheet.to_test
            end
          end
        end
      end # skip tests
    end # 5 
    
  end
  
  unless skip_tests
    puts "Running tests of generated files"
    puts `rspec -fp #{File.join(target_ruby_directory,'specs',"*")}`
  end
end

#time(message) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/rubyfromexcel.rb', line 196

def time(message)
  print message
  STDOUT.flush
  start_time = Time.now
  yield
  puts "took #{(Time.now-start_time).to_i} seconds."
end

#workbook_filenameObject



45
46
47
# File 'lib/rubyfromexcel.rb', line 45

def workbook_filename
  File.join(source_excel_directory,'xl','workbook.xml')
end

#write(*filenames) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/rubyfromexcel.rb', line 136

def write(*filenames)
  target_filename = File.join(target_ruby_directory,*filenames)
  if checkpoint_directory && !debug_dont_write_checkpoint_after_stage && File.exists?(target_filename)
    print "skipping #{File.basename(target_filename)}, already exists and checkpoints are on..."
    return nil
  end
  File.open(target_filename,'w') do |f|
    f.puts yield.to_s
  end
end