Sha256: ea84d3f3fc9a555431c4f2f6911ff21fa067fd297bdd27b8cae22ebf5ee90df4

Contents?: true

Size: 1013 Bytes

Versions: 7

Compression:

Stored size: 1013 Bytes

Contents

require 'fileutils'

module Temporaries
  module Directory
    include Core

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def use_temporary_directory(directory)
        temporaries_adapter.before do
          push_temporary_directory(directory)
        end

        temporaries_adapter.after do
          pop_temporary_directory
        end
      end
    end

    def push_temporary_directory(directory)
      exists = File.exist?(directory)
      push_temporary(:directory, [directory, exists])
      FileUtils.mkdir_p directory unless exists
    end

    def pop_temporary_directory
      directory, existed = pop_temporary(:directory)
      FileUtils.rm_rf directory unless existed
    end

    def with_temporary_directory(directory)
      push_temporary_directory(directory)
      begin
        yield
      ensure
        pop_temporary_directory
      end
    end

    def tmp
      top = top_temporary(:directory) and
        top.first
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
temporaries-0.4.0 lib/temporaries/directory.rb
temporaries-0.3.0 lib/temporaries/directory.rb
temporaries-0.2.0 lib/temporaries/directory.rb
temporaries-0.1.0 lib/temporaries/directory.rb
temporaries-0.0.3 lib/temporaries/directory.rb
temporaries-0.0.2 lib/temporaries/directory.rb
temporaries-0.0.1 lib/temporaries/directory.rb