Class: OneWriter
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
-
#data ⇒ Hash
readonly
vm data.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#logger ⇒ any logger
readonly
The current value of logger.
-
#output ⇒ String
readonly
path to the output file.
Class Method Summary collapse
-
.template_filename(template_name) ⇒ Object
Load template for data conversion.
Instance Method Summary collapse
- #copy_to_output(from, to) ⇒ Object
-
#fill_template ⇒ String
Prepare file content according to ERB template.
-
#initialize(data, file_number, log = Logger.new(STDOUT)) ⇒ OneWriter
constructor
A new instance of OneWriter.
-
#write ⇒ Object
Write data to file in output directory.
- #write_to_tmp(tmp, data) ⇒ Object
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
#data ⇒ Hash (readonly)
vm data
13 14 15 |
# File 'lib/one_writer.rb', line 13 def data @data end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
17 18 19 |
# File 'lib/one_writer.rb', line 17 def log @log end |
#logger ⇒ any logger (readonly)
Returns the current value of logger.
13 14 15 |
# File 'lib/one_writer.rb', line 13 def logger @logger end |
#output ⇒ String (readonly)
path to the output file
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
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_template ⇒ String
Prepare file content according to ERB template
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 |
#write ⇒ Object
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 |