Class: CalcProfit::LatexTableBuilder
- Inherits:
-
Object
- Object
- CalcProfit::LatexTableBuilder
- Defined in:
- lib/calc_profit/latex_table_builder.rb
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#build(include: table.rows[0].keys, exclude: [], caption: nil, sort_on: , total_on: nil, align: {}, formats: {}, define_as: nil) ⇒ Object
The columns will be arranged in the order given in include.
-
#initialize(table) ⇒ LatexTableBuilder
constructor
A new instance of LatexTableBuilder.
Constructor Details
#initialize(table) ⇒ LatexTableBuilder
Returns a new instance of LatexTableBuilder.
5 6 7 |
# File 'lib/calc_profit/latex_table_builder.rb', line 5 def initialize(table) @table = table end |
Instance Attribute Details
#table ⇒ Object (readonly)
Returns the value of attribute table.
3 4 5 |
# File 'lib/calc_profit/latex_table_builder.rb', line 3 def table @table end |
Instance Method Details
#build(include: table.rows[0].keys, exclude: [], caption: nil, sort_on: , total_on: nil, align: {}, formats: {}, define_as: nil) ⇒ Object
The columns will be arranged in the order given in include. Columns not found in the include array will not be displayed. Also, and columns named in the exclude argument will not be displayed. Thus, if only an exclude parameter is given, all the columns will be displayed other than those named in the exclude parameter.
An optional caption can be given as a string in the caption parameter.
The rows of the table will be sorted according to the column (if a symbol) or columns (if an array of symbols) given in sort_on. By default, they will be sorted on the first column named in the columns parameter.
A footer row containing the totals of columns named in the total_on parameter, which can be a symbol or an array of symbols.
All columns will be right-aligned unless a different spec is given in the align parameter, which must be a hash keyed on the column symbol and having a value that is a valid LaTeX column spec. For example, align: { date: ‘c’, ref: ‘l’ } will cause the items in the date column to be centered and the items in the ref column to be left-aligned.
All table items will be formatted as is unless a format is given in the formats parameter. It should be a hash that contains a formatting directive for any columns that need additional formatting. As of now, the only formatting directive recognized is an integer to specify that the column items should be formatted as a number grouped with commas and rounded to the number of places given in the number. For example, formats: { shares: 0, price: 5 } will cause the numbers in those columns to be rounded to 0 and 5 places respectively and have grouping commas embedded in them.
Finally, the whole table can be returned as a macro definition named by the define_as: parameter if given. Otherwise, return the direct table code.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/calc_profit/latex_table_builder.rb', line 49 def build(include: table.rows[0].keys, exclude: [], caption: nil, sort_on: include[0], total_on: nil, align: {}, formats: {}, define_as: nil) # Allow include and exclude to be a single symbol or an array of symbols # and compute the displayed columns. columns = [include].flatten exclude = [exclude].flatten columns = include - exclude # By default, right-align all columns specs = {} columns.each do |col| specs[col] = align[col] || 'r' end # Allow sort_on and total_on to be a single symbol or an array of # symbols sort_on = [sort_on].flatten total_on = [total_on].flatten if total_on # Macro definition ltable = '' if define_as ltable += "\\newcommand{\\#{define_as}}{\n" end # Table environment spec_string = '' columns.each do |col| spec_string += specs[col] end ltable += <<-EOT.clean \\begin{longtable}{#{spec_string}} \\hline\\hline\\\\ EOT ltable += "\n" # Caption ltable += "\\caption{#{caption.tex_quote}}\\\\\n" if caption # Header ltable += tex_header(columns) ltable += <<-'EOT'.clean \\\hline\\[0.5ex] \endhead EOT ltable += "\n" # Footer if total_on ltable += tex_row(total_row(total_on, columns), columns, formats: formats, bold: true) end ltable += <<-'EOT'.clean \endlastfoot EOT ltable += "\n" # Sorted, formated Body sort_rows_on(sort_on).each do |row| ltable += tex_row(row, columns, formats: formats) end # Close environment ltable += <<-'EOT'.clean \end{longtable} EOT # Close macro if define_as ltable += '}' end ltable end |