Class: OneWriter
- Inherits:
-
Object
- Object
- OneWriter
- Defined in:
- lib/one_writer.rb
Overview
Class responsible for writing data into files in specific format
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 directory.
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, output, 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, output, log = Logger.new(STDOUT)) ⇒ OneWriter
Returns a new instance of OneWriter.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/one_writer.rb', line 16 def initialize(data, output, log = Logger.new(STDOUT)) fail ArgumentError, 'Data and output cannot be nil' if data.nil? || output.nil? @template = OneWriter.template_filename(Settings.output['output_type']) if Settings['output'] fail ArgumentError, "No such file: #{@template}." unless File.exist?(@template) @data = data @output = output @log = log end |
Instance Attribute Details
#data ⇒ Hash (readonly)
vm data
12 13 14 |
# File 'lib/one_writer.rb', line 12 def data @data end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
14 15 16 |
# File 'lib/one_writer.rb', line 14 def log @log end |
#logger ⇒ any logger (readonly)
Returns the current value of logger.
12 13 14 |
# File 'lib/one_writer.rb', line 12 def logger @logger end |
#output ⇒ String (readonly)
path to the output directory
12 13 14 |
# File 'lib/one_writer.rb', line 12 def output @output end |
Class Method Details
.template_filename(template_name) ⇒ Object
Load template for data conversion
62 63 64 |
# File 'lib/one_writer.rb', line 62 def self.template_filename(template_name) "#{File.dirname(__FILE__)}/templates/#{template_name}.erb" end |
Instance Method Details
#copy_to_output(from, to) ⇒ Object
44 45 46 47 |
# File 'lib/one_writer.rb', line 44 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
52 53 54 55 56 57 |
# File 'lib/one_writer.rb', line 52 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
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/one_writer.rb', line 28 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
39 40 41 42 |
# File 'lib/one_writer.rb', line 39 def write_to_tmp(tmp, data) tmp.write(data) tmp.flush end |