Class: Sawmill::RecordProcessor::CompileReport

Inherits:
All
  • Object
show all
Defined in:
lib/sawmill/record_processor/compile_report.rb

Overview

This processor collects and formats reports from descendant record processors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from All

#extra_entry, #record

Methods inherited from Base

add_dsl_method, #extra_entry, inherited, #record

Constructor Details

#initialize(*children_) ⇒ CompileReport

Create a report collection.

Recognized options include:

:postprocessor

Postprocessor proc for individual reports.

:separator

Separator string for reports. Default is a single newline.

:header

Header string for the final compiled report. Default is the empty string.

:footer

Footer string for the final compiled report. Default is the empty string.



64
65
66
67
68
69
70
71
# File 'lib/sawmill/record_processor/compile_report.rb', line 64

def initialize(*children_)
  opts_ = children_.last.kind_of?(::Hash) ? children_.pop : {}
  @postprocessor = opts_[:postprocessor]
  @separator = opts_[:separator] || "\n"
  @header = opts_[:header] || ''
  @footer = opts_[:footer] || ''
  super(*children_)
end

Instance Attribute Details

Footer string for the final compiled report.



81
82
83
# File 'lib/sawmill/record_processor/compile_report.rb', line 81

def footer
  @footer
end

#headerObject

Header string for the final compiled report.



78
79
80
# File 'lib/sawmill/record_processor/compile_report.rb', line 78

def header
  @header
end

#separatorObject

Separator string to be inserted between individual reports.



75
76
77
# File 'lib/sawmill/record_processor/compile_report.rb', line 75

def separator
  @separator
end

Instance Method Details

#finishObject

On finish, this processor calls finish on its descendants, converts their values into strings and compiles them into a report. It then returns that report as a string.



98
99
100
101
102
103
104
# File 'lib/sawmill/record_processor/compile_report.rb', line 98

def finish
  values_ = super || []
  values_ = [values_] unless values_.kind_of?(::Array)
  values_.map!{ |val_| @postprocessor.call(val_) } if @postprocessor
  values_.compact!
  "#{@header}#{values_.join(@separator)}#{@footer}"
end

#to_postprocess_value(&block_) ⇒ Object

Provide a postprocessor block for individual report values. This block should take a single parameter and return a string that should be included in the compiled report. It may also return nil to indicate that the data should not be included.



89
90
91
# File 'lib/sawmill/record_processor/compile_report.rb', line 89

def to_postprocess_value(&block_)
  @postprocessor = block_
end