Class: Philiprehberger::Table::Grid
- Inherits:
-
Object
- Object
- Philiprehberger::Table::Grid
- Defined in:
- lib/philiprehberger/table.rb
Instance Method Summary collapse
- #add_row(row) ⇒ Object
- #filter ⇒ Object
-
#initialize(headers:, rows: [], align: {}, max_width: {}) ⇒ Grid
constructor
A new instance of Grid.
- #render(style: :unicode, separator: false, padding: 1) ⇒ Object
- #sort_by(column, direction: :asc) ⇒ Object
- #to_csv ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(headers:, rows: [], align: {}, max_width: {}) ⇒ Grid
Returns a new instance of Grid.
37 38 39 40 41 42 |
# File 'lib/philiprehberger/table.rb', line 37 def initialize(headers:, rows: [], align: {}, max_width: {}) @headers = headers.map { |h| h.nil? ? '' : h.to_s } @rows = rows.map { |row| row.map { |cell| cell.nil? ? '' : cell.to_s } } @align = align @max_width = resolve_max_width(headers, max_width) end |
Instance Method Details
#add_row(row) ⇒ Object
44 45 46 47 |
# File 'lib/philiprehberger/table.rb', line 44 def add_row(row) @rows << row.map { |cell| cell.nil? ? '' : cell.to_s } self end |
#filter ⇒ Object
105 106 107 108 |
# File 'lib/philiprehberger/table.rb', line 105 def filter(&) filtered = @rows.select(&) Grid.new(headers: @headers.dup, rows: filtered, align: @align.dup, max_width: @max_width.dup) end |
#render(style: :unicode, separator: false, padding: 1) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/philiprehberger/table.rb', line 49 def render(style: :unicode, separator: false, padding: 1) style_def = Styles.fetch(style) style_name = Styles.style_name_for(style) widths = calculate_widths truncated_headers = apply_truncation(@headers) truncated_rows = @rows.map { |row| apply_truncation(row) } Renderer.new( headers: truncated_headers, rows: truncated_rows, widths: widths, align: @align, style: style_def, style_name: style_name, separator: separator, padding: padding ).render end |
#sort_by(column, direction: :asc) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/philiprehberger/table.rb', line 98 def sort_by(column, direction: :asc) col_index = resolve_column_index(column) sorted = @rows.sort_by { |row| row[col_index].to_s } sorted = sorted.reverse if direction == :desc Grid.new(headers: @headers.dup, rows: sorted, align: @align.dup, max_width: @max_width.dup) end |
#to_csv ⇒ Object
72 73 74 75 76 77 |
# File 'lib/philiprehberger/table.rb', line 72 def to_csv lines = [] lines << CSV.generate_line(@headers).chomp @rows.each { |row| lines << CSV.generate_line(row).chomp } lines.join("\n") end |
#to_html ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/philiprehberger/table.rb', line 79 def to_html lines = [] lines << '<table>' lines << ' <thead>' lines << ' <tr>' @headers.each { |h| lines << " <th>#{escape_html(h)}</th>" } lines << ' </tr>' lines << ' </thead>' lines << ' <tbody>' @rows.each do |row| lines << ' <tr>' row.each { |cell| lines << " <td>#{escape_html(cell)}</td>" } lines << ' </tr>' end lines << ' </tbody>' lines << '</table>' lines.join("\n") end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/philiprehberger/table.rb', line 68 def to_s render end |