making a release to celebrate miniunit 1.1.0
[augment.git] / lib / backends / backend.rb
blob33d900378b8b17668c5fa774cd9fac45ba811771
1 require 'fileutils'
3 ##
4 # Base class from which to subclass other backends. Subclasses must
5 # implement the +run+ method that gets passed a file name and must (0)
6 # set the +@layers+ instance var to a hash of layers containing the
7 # metadata gathered and call the +write_layers+ method when finished
8 # to save that data.
10 class Backend
11   class << self
12     def run(file)
13       raise "Base Backend class shouldn't be used for real augmentation."
14     end
16     # Output the +@layers+ hash as JSON where augment expects it.
17     def write_layers
18       @layers.each do |file, layers|
19         FileUtils.mkpath(File.dirname(file) + '/.augment')
20         File.open(Augment.augment_path(file), 'w') do |f|
21           f.puts "[#{layers.map{ |l| l.to_json }.join(", \n")}]"
22         end
23       end
24     end
26     # Suppress STDOUT while a block runs.
27     def with_no_output
28       old_stdout = $stdout.clone
29       $stdout.reopen(File.new('/dev/null','w'))
30       yield
31       $stdout.reopen(old_stdout)
32     end
33   end
34 end