Class: DocParser::Output

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename: nil, uniq: false) ⇒ Output

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


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/docparser/output.rb', line 22

def initialize(filename: nil, uniq: false)
  @rowcount = 0
  @filename = filename
  @uniq = uniq
  @uniqarr = []
  fail ArgumentError, 'Please specify a filename' if filename.empty?
  @file = open filename, 'w'
  classname = self.class.name.split('::').last
  @logger = Log4r::Logger.new("docparser::output::#{classname}")
  open_file
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename


5
6
7
# File 'lib/docparser/output.rb', line 5

def filename
  @filename
end

#rowcountObject (readonly)

Returns the value of attribute rowcount


5
6
7
# File 'lib/docparser/output.rb', line 5

def rowcount
  @rowcount
end

Instance Method Details

#add_row(row) ⇒ Object

Adds a row


41
42
43
44
45
46
# File 'lib/docparser/output.rb', line 41

def add_row(row)
  return if @uniq && @uniqarr.include?(row.hash)
  @rowcount += 1
  write_row row
  @uniqarr << row.hash
end

#closeObject

Closes output and IO


49
50
51
52
53
54
55
# File 'lib/docparser/output.rb', line 49

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

Called before closing the file


73
74
# File 'lib/docparser/output.rb', line 73

def footer
end

#headerObject

Called after header is set


63
64
65
# File 'lib/docparser/output.rb', line 63

def header
  # do nothing
end

#header=(row) ⇒ Object

Stores the header


35
36
37
38
# File 'lib/docparser/output.rb', line 35

def header=(row)
  @header = row
  header
end

#open_fileObject

Called after the file is opened


58
59
60
# File 'lib/docparser/output.rb', line 58

def open_file
  # do nothing
end

#write_row(_row) ⇒ Object

Called when a row is added


68
69
70
# File 'lib/docparser/output.rb', line 68

def write_row(_row)
  fail NotImplementedError, 'No row writer defined'
end