Sha256: ca1a293e13f8f378b31cafe8afbe6581263acbd1b72843cbfb0b919eb9f2eb64

Contents?: true

Size: 934 Bytes

Versions: 2

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

require 'tempfile'

module Dsu
  module Services
    class TempFileWriterService
      def initialize(temp_file_content:, options: {})
        raise ArgumentError, 'temp_file_content is nil' if temp_file_content.nil?
        raise ArgumentError, 'temp_file_content is the wrong object type' unless temp_file_content.is_a?(String)
        raise ArgumentError, 'options is nil' if options.nil?
        raise ArgumentError, 'options is the wrong object type' unless options.is_a?(Hash)

        @temp_file_content = temp_file_content
        @options = options || {}
      end

      def call
        raise ArgumentError, 'no block given' unless block_given?

        Tempfile.new('dsu').tap do |file|
          file.write("#{temp_file_content}\n")
          file.close
          yield file.path
        end.unlink
      end

      private

      attr_reader :temp_file_content, :options
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dsu-0.1.0.alpha.4 lib/dsu/services/temp_file_writer_service.rb
dsu-0.1.0.alpha.3 lib/dsu/services/temp_file_writer_service.rb