Sha256: 51f4001e0ac7cb0d0a01f77301e511db1d00c4422a5ece743ffb4c5c41115f18

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

Contents

require 'tempfile'

module Milton
  # For lack of a better name, a MiltonTempfile adds some helpful
  # functionality to Ruby's Tempfile
  class Tempfile < ::Tempfile
    class << self
      def create(data_or_path, tempfile_path)
        FileUtils.mkdir_p(tempfile_path) unless File.exists?(tempfile_path)
    
        tempfile = new(basename, tempfile_path)
    
        if data_or_path.is_a?(StringIO)
          tempfile.binmode
          tempfile.write data_or_path.read
          tempfile.close
        else
          tempfile.close
          FileUtils.cp((data_or_path.respond_to?(:path) ? data_or_path.path : data_or_path), tempfile.path)
        end
    
        tempfile
      end
    
      def basename
        "#{rand(Time.now.to_i)}"
      end
    
      def filename(extension)
        "#{basename}.#{extension}"
      end
    
      def path(tempfile_path, extension)
        File.join(tempfile_path, filename(extension))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
citrusbyte-milton-0.3.1 lib/milton/core/tempfile.rb
citrusbyte-milton-0.3.2 lib/milton/core/tempfile.rb