class ReportsKits::Reports::Data::FormatTable

Attributes

data[RW]
first_column_label[RW]
format[RW]
report_options[RW]

Public Class Methods

new(data, format:, first_column_label:, report_options:) click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 7
def initialize(data, format:, first_column_label:, report_options:)
  self.data = data
  self.format = format
  self.first_column_label = first_column_label
  self.report_options = report_options || {}
end

Public Instance Methods

perform() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 14
def perform
  table_data
end

Private Instance Methods

column_names() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 28
def column_names
  column_names_column_values[0]
end
column_names_column_values() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 40
def column_names_column_values
  @column_names_column_values ||= begin
    column_names = [format_string(first_column_label)]
    column_values = []
    data[:datasets].each do |dataset|
      column_names << format_string(dataset[:label])
      column_values << dataset[:data]
    end
    [column_names, column_values]
  end
end
column_values() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 32
def column_values
  column_names_column_values[1]
end
data_rows() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 36
def data_rows
  @data_rows ||= column_values.transpose
end
format_string(string) click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 52
def format_string(string)
  return string unless string && strip_html_tags?
  ActionView::Base.full_sanitizer.sanitize(string)
end
strip_html_tags?() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 57
def strip_html_tags?
  format == 'csv'
end
table_data() click to toggle source
# File lib/reports_kits/reports/data/format_table.rb, line 20
def table_data
  data_rows_with_labels = data_rows.map.with_index do |data_row, index|
    label = format_string(data[:labels][index])
    [label] + data_row
  end
  [column_names] + data_rows_with_labels
end