class Painless::Table
Attributes
cell_sizes[R]
heading[R]
objects[R]
separate_rows[R]
Public Class Methods
new(parameters={})
click to toggle source
# File lib/painless-table/table.rb, line 7 def initialize parameters={} @heading = parameters.fetch :heading, [] @objects = parameters.fetch :objects, [] end
Public Instance Methods
to_s()
click to toggle source
# File lib/painless-table/table.rb, line 12 def to_s draw_heading + draw_body end
Private Instance Methods
draw_body()
click to toggle source
# File lib/painless-table/table.rb, line 75 def draw_body body = '' get_table_values.each do |row_with_values| row = '|' row_with_values.each_with_index do |value, column| cell = ' ' + value.ljust(get_cell_widths[column]-1) + '|' row.concat cell end row.concat "\n" body.concat row end body + draw_borderline end
draw_borderline()
click to toggle source
# File lib/painless-table/table.rb, line 57 def draw_borderline border_length = get_cell_widths.reduce( :+ ) + get_cell_widths.count + 1 borderline = Borderline.new border_length borderline.to_s end
draw_heading()
click to toggle source
# File lib/painless-table/table.rb, line 63 def draw_heading heading = '|' get_heading_titles.each_with_index do |title, column| column_title = title.center(get_cell_widths[column]).concat '|' heading.concat column_title end heading.concat "\n" draw_borderline + heading + draw_borderline end
get_cell_widths()
click to toggle source
# File lib/painless-table/table.rb, line 18 def get_cell_widths widths = @heading.map { |cell| cell.size } get_table_values.each do |row| row.each_with_index do |cell, column| cell_size = cell.to_s.size widths[column] = cell_size if widths[column] < cell_size end end widths.map { |width| width + 2 } end
get_heading_titles()
click to toggle source
# File lib/painless-table/table.rb, line 31 def get_heading_titles @heading.map { |cell| cell.to_s.humanize } end
get_table_values()
click to toggle source
# File lib/painless-table/table.rb, line 35 def get_table_values rows = [] @objects.each do |object| object_values = [] @heading.each do |attribute| if object.methods.include? attribute.to_sym object_values.push object.send(attribute.to_sym).to_s elsif object.kind_of? Hash object_values.push object[attribute.to_sym].to_s else object_values.push '-' end end rows.push object_values end rows end