# frozen_string_literal: true require ::File.expand_path('file_asset', File.dirname(__FILE__)) # require ::File.expand_path('report_builder', File.dirname(__FILE__)) module Cornucopia module Util class ReportTable class ReportTableException < Exception attr_accessor :error def initialize(error) @error = error end def to_s @error.to_s end def backtrace @error.backtrace end end attr_reader :full_table # Usage Example: # # ReportTable.new do |report_table| # report_table.write_stats("label", "value") # # ReportTable.new(table_prefix: more_info_prefix_text, # table_postfix: more_info_postfix_text, # nested_table: report_table) do |more_info_block| # ReportTable.new(nested_table: more_info_block) do |more_info_table| # more_info_table.write_stats("label", "value") # # ReportTable.new(report_table: is_sub_table? nil : more_info_table, # nested_table: more_info_table) do |sub_table| # sub_table.write_stats("label", "value") # end # end # end # end # options # table_prefix - The value to open the table with. # Default:
tags will not be added to the output
# default: false
# do_not_pretty_print - If set, then the value will only be escaped.
# If not, then it will be pretty formatted.
# default: false
def write_stats label, value, options = {}
raise Exception.new("The table is closed, you may not add more rows to it") if @table_closed
if options[:format]
print_value = options[:format].call(value)
elsif options[:format_function] && options[:format_object]
print_value = options[:format_object].send(options[:format_function], value)
elsif options[:do_not_pretty_print]
print_value = Cornucopia::Util::ReportBuilder.escape_string(value)
else
print_value = Cornucopia::Util::ReportBuilder.pretty_format(value)
end
label = Cornucopia::Util::ReportBuilder.escape_string(label)
unless @options[:not_a_table]
@full_table << " \n"
@full_table << " \n#{label}\n\n"
@full_table << " \n"
@full_table << " \n"
unless options[:prevent_shrink]
@full_table << " \n"
end
@full_table << "" unless options[:exclude_code_block]
end
@full_table << print_value
unless @options[:not_a_table]
@full_table << "
\n" unless options[:exclude_code_block]
unless options[:prevent_shrink]
@full_table << " \n"
@full_table << " \n"
end
@full_table << " \n"
@full_table << " \n"
end
end
end
end
end