class OneWriter
Class responsible for writing data into files in specific format
@attr_reader [Hash] data vm data @attr_reader [String] output path to the output file @attr_reader [any logger] logger
Constants
- APEL_FILENAME_FORMAT
Attributes
data[R]
log[R]
output[R]
Public Class Methods
new(data, file_number, log = Logger.new(STDOUT))
click to toggle source
# 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
template_filename(template_name)
click to toggle source
Load template for data conversion
@param [String] template_name name of the template to look for
# File lib/one_writer.rb, line 69 def self.template_filename(template_name) "#{File.dirname(__FILE__)}/templates/#{template_name}.erb" end
Public Instance Methods
copy_to_output(from, to)
click to toggle source
# 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()
click to toggle source
Prepare file content according to ERB template
@return [String] transformed content
# 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()
click to toggle source
Write data to file in output directory
# 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)
click to toggle source
# File lib/one_writer.rb, line 46 def write_to_tmp(tmp, data) tmp.write(data) tmp.flush end