Class: Cornucopia::Util::ReportTable

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/util/report_table.rb

Defined Under Namespace

Classes: ReportTableException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ReportTable

options

table_prefix          - The value to open the table with.
                        Default: <div class="cornucopia-table">
table_postfix         - The value to output when closing the table.
                        Default: </div>
report_table          - The table that all table cells are to be output
                        to.
                        If set, this table will effectively not do anything.
                        Default: nil
nested_table          - If this table is nested inside of another table, this will ensure
                        that the nested table is written, even in case of an exception.
nested_table_label    - The label that the nested table is to be output with.
nested_table_options  - The options that the nested table is to be output with.
                        Default: { prevent_shrink: true, exclude_code_block: true, do_not_pretty_print: true }
not_a_table           - If set, then write_stats simply appends the value to the "table"
                        Default: false
suppress_blank_table  - If set, then when a nested table is to be written to the parent table, the
                        nested table will not be output if it is empty.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cornucopia/util/report_table.rb', line 64

def initialize(options = {}, &block)
  @full_table   = "".dup
  @table_closed = false
  @options      = options

  @options.delete_if { |key, value| value.blank? }
  @options.reverse_merge!({
                              table_prefix:         "<div class=\"cornucopia-table\">\n",
                              table_postfix:        "</div>\n",
                              report_table:         self,
                              nested_table:         nil,
                              nested_table_label:   nil,
                              nested_table_options: { prevent_shrink:      true,
                                                      exclude_code_block:  true,
                                                      do_not_pretty_print: true }
                          })

  @options[:report_table] ||= self

  begin
    open_table

    block.yield(@options[:report_table]) if block
  rescue ReportTableException => table_error
    if @options[:nested_table]
      raise table_error
    else
      raise table_error.error
    end
  rescue Exception => error
    error_report = "".html_safe
    error_report << error.to_s + "\n"
    error_report << error.class.name + "\n"
    error_report << error.class.name + "\n"
    error_report << Cornucopia::Util::ReportBuilder.pretty_format(error.backtrace)

    @options[:report_table].write_stats "<strong>Exception while building table</strong>".html_safe, error_report

    raise(ReportTableException.new(error))
  ensure
    close_table
  end
end

Instance Attribute Details

#full_tableObject (readonly)

Returns the value of attribute full_table.



25
26
27
# File 'lib/cornucopia/util/report_table.rb', line 25

def full_table
  @full_table
end

Instance Method Details

#close_tableObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cornucopia/util/report_table.rb', line 113

def close_table
  empty_table = @table_start == @full_table

  @full_table << @options[:table_postfix] if @options[:table_postfix] && @options[:report_table] == self
  @full_table = @full_table.html_safe

  if @options[:nested_table] && @options[:report_table] != @options[:nested_table]
    if !@options[:suppress_blank_table] || !empty_table
      @options[:nested_table].write_stats(@options[:nested_table_label],
                                          @options[:report_table].full_table,
                                          @options[:nested_table_options])
    end
  else
    unless !@options[:suppress_blank_table] || !empty_table
      @full_table = "".html_safe
    end
  end

  @table_closed = true
end

#open_tableObject



108
109
110
111
# File 'lib/cornucopia/util/report_table.rb', line 108

def open_table
  @full_table << @options[:table_prefix] if @options[:table_prefix] && @options[:report_table] == self
  @table_start = @full_table.clone
end

#write_stats(label, value, options = {}) ⇒ Object

Writes information to the table.

Parameters:

label                 - The label for the information.
                        Should be short.  Will be made bold and the cell will be shrunk.
value                 - The value for the information.
                        If the value is very wide, the cell will expand to show it.
                        If the value is very tall, an expansion option will be provided, and the
                        cell will truncate the value otherwise.

options:
  prevent_shrink     -  If set, the cell will not be truncated if it is too tall, instead the cell will show
                        the full contents.
                        default - false
  exclude_code_block  - If set, then the <pre><code> 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

Raises:

  • (Exception)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cornucopia/util/report_table.rb', line 153

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 << "  <div class=\"cornucopia-row\">\n"
    @full_table << "    <div class=\"cornucopia-cell-label\">\n#{label}\n</div>\n"
    @full_table << "    <div class=\"cornucopia-cell-expand\">\n"
    unless options[:prevent_shrink]
      @full_table << "    <div class=\"hidden\"><a class=\"cornucopia-cell-more-data\" href=\"#\"><img src=\"expand.gif\"></a></div>\n"
    end
    @full_table << "    </div>\n"
    @full_table << "    <div class=\"cornucopia-cell-data\">\n"
    unless options[:prevent_shrink]
      @full_table << "      <div class=\"hide-contents\">\n"
    end

    @full_table << "<pre><code>" unless options[:exclude_code_block]
  end

  @full_table << print_value

  unless @options[:not_a_table]
    @full_table << "</code></pre>\n" unless options[:exclude_code_block]
    unless options[:prevent_shrink]
      @full_table << "      </div>\n"
      @full_table << "      <div class=\"cornucopia-cell-more hidden\"><a class=\"cornucopia-cell-more-data\" href=\"#\">more...</a></div>\n"
    end
    @full_table << "    </div>\n"
    @full_table << "  </div>\n"
  end
end