Class: OneWriter

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

Overview

Class responsible for writing data into files in specific format

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataHash (readonly)

vm data

Returns:

  • (Hash)

    the current value of data



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

def data
  @data
end

#logObject (readonly)

Returns the value of attribute log.



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

def log
  @log
end

#loggerany logger (readonly)

Returns the current value of logger.

Returns:

  • (any logger)

    the current value of logger



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

def logger
  @logger
end

#outputString (readonly)

path to the output directory

Returns:

  • (String)

    the current value of output



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

Parameters:

  • template_name (String)

    name of the template to look for



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_templateString

Prepare file content according to ERB template

Returns:

  • (String)

    transformed content



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

#writeObject

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