Module: PBsubs

Extended by:
PBsubs, Rake::DSL
Included in:
PBsubs
Defined in:
lib/persistent_blocks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_persistent_blocks_taskObject

Returns the value of attribute default_persistent_blocks_task.



58
59
60
# File 'lib/persistent_blocks.rb', line 58

def default_persistent_blocks_task
  @default_persistent_blocks_task
end

#marshal_dirObject

Returns the value of attribute marshal_dir.



58
59
60
# File 'lib/persistent_blocks.rb', line 58

def marshal_dir
  @marshal_dir
end

Instance Method Details

#check_for_single_input(raw, n_expected) ⇒ Object



66
67
68
# File 'lib/persistent_blocks.rb', line 66

def check_for_single_input(raw, n_expected)
  (n_expected == 1) ? raw[0] : raw
end

#check_outputs(outputs, output_syms) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/persistent_blocks.rb', line 97

def check_outputs(outputs, output_syms)
  n_expect = output_syms.count
  case n_expect
  when 0 # NP, the block might return an argument, but we don't need to save it
  when 1
    raise "Error: expecting an output (#{outputs}) from the block but got none" if outputs.nil?
    # if we get an array of outputs they will just be mapped to a single marshal file
  else
    raise "Error: expecting #{n_expect} outputs (#{output_sym}) from block but got none" if outputs.nil?
    n_received = [*outputs].count
    raise "Error: expecting #{n_expect} outputs (#{output_syms}) from block but got #{n_received} instead" if n_expect != n_received
  end
end

#check_specified_output_files(specified_files) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/persistent_blocks.rb', line 134

def check_specified_output_files(specified_files)
  specified_files.each do |file|
    unless File.exists? file
      raise "Error: #{file} was not created"
    end
  end
end

#display_output_info(outputs, output_syms) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/persistent_blocks.rb', line 111

def display_output_info(outputs, output_syms)
  output_ar = output_syms.zip(outputs).map do |sym, output|
    how_long = (output.respond_to?:length) ? output.length : nil
    "#{sym} (length = #{how_long})"
  end
  puts "Block Outputs:"
  puts output_ar
end

#ensure_array(raw, n_expected) ⇒ Object



62
63
64
# File 'lib/persistent_blocks.rb', line 62

def ensure_array(raw, n_expected)
  (n_expected == 1) ? [raw] : raw
end

#ensure_marshal_dirObject



120
121
122
123
124
# File 'lib/persistent_blocks.rb', line 120

def ensure_marshal_dir
  CLOBBER.include(@marshal_dir) unless CLOBBER.include?(@marshal_dir)
  directory @marshal_dir
  @marshal_dir
end

#load_inputs(filenames) ⇒ Object



86
87
88
89
# File 'lib/persistent_blocks.rb', line 86

def load_inputs(filenames)
  return nil if filenames.nil? || filenames.empty?
  filenames.map{|f| marshal_load(f)}
end

#marshal_load(filename) ⇒ Object



82
83
84
# File 'lib/persistent_blocks.rb', line 82

def marshal_load(filename)
  File.open(filename, 'r') {|io| Marshal.load(io)}
end

#marshal_save(object, filename) ⇒ Object



78
79
80
# File 'lib/persistent_blocks.rb', line 78

def marshal_save(object, filename)
  File.open(filename, 'w') {|io| Marshal.dump(object, io)}
end

#parse_inputs(input_args) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/persistent_blocks.rb', line 126

def parse_inputs(input_args)
  args = input_args.dup
  options = (args[-1].is_a? Hash) ? args.pop : {}
  syms = args.select{|out| out.is_a? Symbol}
  files = args.select{|out| out.is_a? String}
  return syms, files, options
end

#save_outputs(objects_to_save, filenames) ⇒ Object



91
92
93
94
95
# File 'lib/persistent_blocks.rb', line 91

def save_outputs(objects_to_save, filenames)
  return if objects_to_save.nil?
  return if filenames.nil? || filenames.empty?
  objects_to_save.zip(filenames).each {|o,f| marshal_save(o, f)}
end

#sym_ar_to_file_list(sym_ar) ⇒ Object



74
75
76
# File 'lib/persistent_blocks.rb', line 74

def sym_ar_to_file_list(sym_ar)
  sym_ar.map{|sym| sym_to_filename(sym)}
end

#sym_to_filename(sym) ⇒ Object



70
71
72
# File 'lib/persistent_blocks.rb', line 70

def sym_to_filename(sym)
  File.join(@marshal_dir, sym.to_s)
end