Class: OneWriter

Inherits:
Object
  • Object
show all
Includes:
OutputTypes
Defined in:
lib/one_writer.rb

Overview

Class responsible for writing data into files in specific format

Constant Summary collapse

APEL_FILENAME_FORMAT =
'%014d'

Constants included from OutputTypes

OutputTypes::APEL_OT, OutputTypes::LOGSTASH_OT, OutputTypes::PBS_OT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, file_number, log = Logger.new(STDOUT)) ⇒ OneWriter

Returns a new instance of OneWriter.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/one_writer.rb', line 19

def initialize(data, file_number, log = Logger.new(STDOUT))
  fail ArgumentError, 'Data and file number cannot be nil' if data.nil? || file_number.nil?

  output_type = Settings.output['output_type']
  @template = OneWriter.template_filename(output_type) if Settings['output']
  fail ArgumentError, "No such file: #{@template}." unless File.exist?(@template)

  @data = data
  @log = log

  filename = file_number.to_s
  filename = APEL_FILENAME_FORMAT % file_number if APEL_OT.include?(output_type)
  @output = "#{Settings.output['output_dir']}/#{filename}"
end

Instance Attribute Details

#dataHash (readonly)

vm data

Returns:

  • (Hash)

    the current value of data



13
14
15
# File 'lib/one_writer.rb', line 13

def data
  @data
end

#logObject (readonly)

Returns the value of attribute log.



17
18
19
# File 'lib/one_writer.rb', line 17

def log
  @log
end

#loggerany logger (readonly)

Returns the current value of logger.

Returns:

  • (any logger)

    the current value of logger



13
14
15
# File 'lib/one_writer.rb', line 13

def logger
  @logger
end

#outputString (readonly)

path to the output file

Returns:

  • (String)

    the current value of output



13
14
15
# File 'lib/one_writer.rb', line 13

def output
  @output
end

Class Method Details

.template_filename(template_name) ⇒ Object

Load template for data conversion

Parameters:

  • template_name (String)

    name of the template to look for



69
70
71
# File 'lib/one_writer.rb', line 69

def self.template_filename(template_name)
  "#{File.dirname(__FILE__)}/templates/#{template_name}.erb"
end

Instance Method Details

#copy_to_output(from, to) ⇒ Object



51
52
53
54
# File 'lib/one_writer.rb', line 51

def copy_to_output(from, to)
  @log.debug("Copying temporary file into '#{@output}'")
  FileUtils.cp(from, to)
end

#fill_templateString

Prepare file content according to ERB template

Returns:

  • (String)

    transformed content



59
60
61
62
63
64
# File 'lib/one_writer.rb', line 59

def fill_template
  @log.debug("Reading erb template from file: '#{@template}'.")
  erb = ERB.new(File.read(@template), nil, '-')
  erb.filename = @template
  erb.result(binding)
end

#writeObject

Write data to file in output directory



35
36
37
38
39
40
41
42
43
44
# File 'lib/one_writer.rb', line 35

def write
  @log.debug('Creating temporary file...')
  tmp = Tempfile.new('oneacct_export')
  @log.debug("Temporary file: '#{tmp.path}' created.")
  @log.debug('Writing to temporary file...')
  write_to_tmp(tmp, fill_template)
  copy_to_output(tmp.path, @output)
ensure
  tmp.close(true)
end

#write_to_tmp(tmp, data) ⇒ Object



46
47
48
49
# File 'lib/one_writer.rb', line 46

def write_to_tmp(tmp, data)
  tmp.write(data)
  tmp.flush
end