Module: PersistentBlocks

Extended by:
PersistentBlocks
Included in:
PersistentBlocks
Defined in:
lib/persistent_blocks.rb

Instance Method Summary collapse

Instance Method Details

#persist(*args, &block) ⇒ Object



7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/persistent_blocks.rb', line 7

def persist(*args, &block)
  #puts "#{__FILE__}"
  output_syms, specified_output_files, options = PBsubs.parse_inputs(args)
  marshal_output_files = PBsubs.sym_ar_to_file_list(output_syms)
  marshal_dir = PBsubs.ensure_marshal_dir
  file marshal_output_files[0] => marshal_dir unless marshal_output_files[0].nil?
  all_output_files = marshal_output_files + specified_output_files
  target_file = all_output_files[0] # rake tasks only accept one file
  params = if options[:input_overide]
             [*options[:input_overide]]
           else
             block.parameters.map{|p| p[1]}
           end
  prereqs = PBsubs.sym_ar_to_file_list(params)
  file target_file => prereqs unless prereqs.nil?
  file target_file do |f|
    puts "Persistent_blocks: Persisting #{output_syms} and/or #{specified_output_files} from #{[*params]}"
    raw_inputs = PBsubs.load_inputs(prereqs)
    inputs = PBsubs.check_for_single_input(raw_inputs, prereqs.count)
    raw_outputs = block.(inputs)
    outputs = PBsubs.ensure_array(raw_outputs, output_syms.count)
    unless output_syms.empty?
      PBsubs.check_outputs(outputs, output_syms)
      PBsubs.display_output_info(outputs, output_syms)
      PBsubs.save_outputs(outputs, marshal_output_files) 
    end
    unless specified_output_files.empty?
      PBsubs.check_specified_output_files(specified_output_files)
    end
  end
  options[:task] ||= PBsubs.default_persistent_blocks_task
  task options[:task].to_sym => target_file
  all_output_files.each do |out_file|
    delete_task = "delete_#{out_file}"
    task delete_task do
      rm out_file if File.exists? out_file
    end
    task "delete_#{options[:task].to_sym}" => delete_task
    file out_file => target_file unless out_file == target_file
  end
  specified_output_files.each do |out_file|
    CLOBBER.include(out_file)
  end
  return target_file # allows file target_file => extra_dependency
end