Sha256: 1f7aeae8f3d0ee93a18811aa32a359c4a22c54f81264a1976e4d8acb2040947d

Contents?: true

Size: 544 Bytes

Versions: 13

Compression:

Stored size: 544 Bytes

Contents

require 'tempfile'

module WithTempFile
  def self.included(target)
    target.instance_eval do
      def self.with_temp_file(name)
        random_name = "#{name}-#{rand(100_000..200_000)}"
        full_path   = File.join(Dir.tmpdir, random_name)
        define_method "#{name}_path" do
          full_path
        end
        define_method "unlink_#{name}" do
          return unless File.exist?(full_path)
          File.unlink(full_path)
        end

        after do
          send("unlink_#{name}")
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
candy_check-0.6.0 spec/support/with_temp_file.rb
candy_check-0.5.0 spec/support/with_temp_file.rb
candy_check-0.4.0 spec/support/with_temp_file.rb
candy_check-0.3.0 spec/support/with_temp_file.rb
candy_check-0.2.1 spec/support/with_temp_file.rb
candy_check-0.2.0 spec/support/with_temp_file.rb
candy_check-0.1.2 spec/support/with_temp_file.rb
candy_check-0.1.1 spec/support/with_temp_file.rb
candy_check-0.1.0.pre spec/support/with_temp_file.rb
candy_check-0.0.5 spec/support/with_temp_file.rb
candy_check-0.0.3 spec/support/with_temp_file.rb
candy_check-0.0.2 spec/support/with_temp_file.rb
candy_check-0.0.1 spec/support/with_temp_file.rb