Class: Dsu::Services::TempFile::WriterService

Inherits:
Object
  • Object
show all
Defined in:
lib/dsu/services/temp_file/writer_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(tmp_file_content:, options: {}) ⇒ WriterService

Returns a new instance of WriterService.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/dsu/services/temp_file/writer_service.rb', line 9

def initialize(tmp_file_content:, options: {})
  raise ArgumentError, 'tmp_file_content is nil' if tmp_file_content.nil?
  raise ArgumentError, 'tmp_file_content is the wrong object type' unless tmp_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)

  @tmp_file_content = tmp_file_content
  @options = options || {}
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
# File 'lib/dsu/services/temp_file/writer_service.rb', line 19

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

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