class DocParser::Output

The Output base class. All Output classes inherit from this one.

Attributes

filename[R]
rowcount[R]

Public Class Methods

new(filename: nil, uniq: false) click to toggle source

Creates a new output

You can assign the output to the Parser so it automatically writes all data to the file you want.

Do not use this class as an output, instead use one of the classes that inherit from it

@param filename [String] Output filename @see Parser @see CSVOutput @see HTMLOutput @see YAMLOutput @see XLSXOutput @see MultiOutput

# File lib/docparser/output.rb, line 24
def initialize(filename: nil, uniq: false)
  @rowcount = 0
  @filename = filename
  @uniq = uniq
  @uniqarr = []
  raise ArgumentError, 'Please specify a filename' if filename.empty?

  @file = open filename, 'w'
  @logger = Logger.new(STDERR)
  @logger.level = Logger::INFO
  open_file
end

Public Instance Methods

add_row(row) click to toggle source

Adds a row

# File lib/docparser/output.rb, line 44
def add_row(row)
  return if @uniq && @uniqarr.include?(row.hash)

  @rowcount += 1
  write_row row
  @uniqarr << row.hash
end
close() click to toggle source

Closes output and IO

# File lib/docparser/output.rb, line 53
def close
  footer
  @file.close unless @file.closed?
  @logger.info 'Finished writing'
  size = File.size(@filename) / 1024.0
  @logger.info format('%s: %d rows, %.2f KiB', @filename, rowcount, size)
end
header() click to toggle source

Called after header is set

# File lib/docparser/output.rb, line 67
def header
  # do nothing
end
header=(row) click to toggle source

Stores the header

# File lib/docparser/output.rb, line 38
def header=(row)
  @header = row
  header
end
open_file() click to toggle source

Called after the file is opened

# File lib/docparser/output.rb, line 62
def open_file
  # do nothing
end
write_row(_row) click to toggle source

Called when a row is added

# File lib/docparser/output.rb, line 72
def write_row(_row)
  raise NotImplementedError, 'No row writer defined'
end