Class: GrafanaReporter::AbstractTableFormatStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana_reporter/abstract_table_format_strategy.rb

Overview

The abstract base class, which is to be implemented for different table output formats. By implementing this class, you e.g. can decide if a table will be formatted as CSV, JSON or any other format.

Constant Summary collapse

@@subclasses =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.abbreviationString

This method is abstract.

Returns short name of the current stategy, under which it shall be accessible.

Returns:

  • (String)

    short name of the current stategy, under which it shall be accessible

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/grafana_reporter/abstract_table_format_strategy.rb', line 22

def self.abbreviation
  raise NotImplementedError
end

.get(abbreviation) ⇒ AbstractTableFormatStrategy

Returns fitting strategy instance for the given name.

Parameters:

  • abbreviation (String)

    name of the requested table format strategy

Returns:



16
17
18
# File 'lib/grafana_reporter/abstract_table_format_strategy.rb', line 16

def self.get(abbreviation)
  @@subclasses.select { |item| item.abbreviation == abbreviation }.first.new
end

.inherited(obj) ⇒ Object



10
11
12
# File 'lib/grafana_reporter/abstract_table_format_strategy.rb', line 10

def self.inherited(obj)
  @@subclasses << obj
end

Instance Method Details

#format(content, include_headline, transposed) ⇒ String

Used to format a given content array to the desired output format. The default implementation applies the #format_rules to create a custom string export. If this is not sufficient for a desired table format, you may simply overwrite this function to have full freedom about the desired output.

Parameters:

  • content (Hash)

    datasource table result

  • include_headline (Boolean)

    true, if headline should be included in result

  • transposed (Boolean)

    true, if result array is in transposed format

Returns:

  • (String)

    formatted in table format



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grafana_reporter/abstract_table_format_strategy.rb', line 34

def format(content, include_headline, transposed)
  result = content[:content]

  # add the headline at the correct position to the content array
  if include_headline
    if transposed
      result.each_index do |i|
        result[i] = [content[:header][i]] + result[i]
      end
    else
      result = result.unshift(content[:header])
    end
  end

  # translate the content to a table
  result.map do |row|
    format_rules[:row_start] + row.map do |item|
      value = item.to_s
      if format_rules[:replace_string_or_regex]
        value = value.gsub(format_rules[:replace_string_or_regex], format_rules[:replacement])
      end

      format_rules[:cell_start] + value + format_rules[:cell_end]
    end.join(format_rules[:between_cells])
  end.join(format_rules[:row_end])
end

#format_rulesObject

Formatting rules, which are applied to build the table output format.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grafana_reporter/abstract_table_format_strategy.rb', line 62

def format_rules
  {
    row_start: '',
    row_end: '',
    cell_start: '',
    between_cells: '',
    cell_end: '',
    replace_string_or_regex: nil,
    replacement: ''
  }
end