Class: TablePuts::Printer
- Inherits:
-
Object
- Object
- TablePuts::Printer
- Defined in:
- lib/table_puts/printer.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#max_width ⇒ Object
readonly
Returns the value of attribute max_width.
-
#min_width ⇒ Object
readonly
Returns the value of attribute min_width.
-
#transposed ⇒ Object
readonly
Returns the value of attribute transposed.
Instance Method Summary collapse
- #abbreviate(string) ⇒ Object
- #break_string ⇒ Object
-
#calculate_column_justification ⇒ Object
Computes how we should justify the values in each column.
- #call ⇒ Object
- #check_widths(min, max) ⇒ Object
-
#column_widths ⇒ Object
An array of how many characters wide each column should be.
- #header ⇒ Object
-
#initialize(data, min_width, max_width) ⇒ Printer
constructor
A new instance of Printer.
- #rows ⇒ Object
-
#transpose_data ⇒ Object
Transforms data into a single hash with each value containing an array of the values from each hash - for example:.
Constructor Details
#initialize(data, min_width, max_width) ⇒ Printer
Returns a new instance of Printer.
4 5 6 7 8 9 10 |
# File 'lib/table_puts/printer.rb', line 4 def initialize(data, min_width, max_width) @data = data @transposed = transpose_data @min_width = min_width @max_width = max_width check_widths(min_width, max_width) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'lib/table_puts/printer.rb', line 2 def data @data end |
#max_width ⇒ Object (readonly)
Returns the value of attribute max_width.
2 3 4 |
# File 'lib/table_puts/printer.rb', line 2 def max_width @max_width end |
#min_width ⇒ Object (readonly)
Returns the value of attribute min_width.
2 3 4 |
# File 'lib/table_puts/printer.rb', line 2 def min_width @min_width end |
#transposed ⇒ Object (readonly)
Returns the value of attribute transposed.
2 3 4 |
# File 'lib/table_puts/printer.rb', line 2 def transposed @transposed end |
Instance Method Details
#abbreviate(string) ⇒ Object
86 87 88 |
# File 'lib/table_puts/printer.rb', line 86 def abbreviate(string) string == string[0...max_width] ? string : string[0...max_width-3] + "..." end |
#break_string ⇒ Object
59 60 61 |
# File 'lib/table_puts/printer.rb', line 59 def break_string @break_string ||= column_widths.map { |size| "+-#{ '-'*size }-" }.join + "+" end |
#calculate_column_justification ⇒ Object
Computes how we should justify the values in each column
If all values in a column contain a number or are nil, then we right justify Otherwise, left justify
Returns an array of ‘r’s and ‘l’s for each column
80 81 82 83 84 |
# File 'lib/table_puts/printer.rb', line 80 def calculate_column_justification transposed.map do |(key, values)| values.all? { |entry| entry.to_s.match(/\d/) || entry.nil? } ? 'r' : 'l' end end |
#call ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/table_puts/printer.rb', line 12 def call puts break_string puts header puts break_string puts rows puts break_string end |
#check_widths(min, max) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/table_puts/printer.rb', line 90 def check_widths(min, max) if min > max puts "WARNING: max_width is greater than min_width" puts " This may cause unexpected output" puts " min_width: #{ min_width }\tmax_width: #{ max_width }" end end |
#column_widths ⇒ Object
An array of how many characters wide each column should be
You can set a minimum and maximum width for all columns by passing in min_width and max_width
67 68 69 70 71 72 |
# File 'lib/table_puts/printer.rb', line 67 def column_widths @column_widths ||= transposed.map do |value_array| largest_width_in_column = value_array.flatten.map { |entry| entry.to_s.length }.max [[largest_width_in_column, max_width].min, min_width].max end end |
#header ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/table_puts/printer.rb', line 39 def header transposed.keys.map.with_index do |key, i| key_string = abbreviate(key.to_s).ljust(column_widths[i]) "| #{ key_string } " end.join + "|" end |
#rows ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/table_puts/printer.rb', line 47 def rows justification = calculate_column_justification data.map do |row| row.map.with_index do |(key, value), i| justify = "#{ justification[i] }just".to_sym value_string = abbreviate(value.to_s).send(justify, (column_widths[i])) "| #{ value_string } " end.join + "|" end end |
#transpose_data ⇒ Object
Transforms data into a single hash with each value containing an array of the values from each hash - for example:
- { x: 1, y: 2 }, { x: 3 }, { y: 4 }
-
becomes { x: [1, 3], y: [2, 4] }
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/table_puts/printer.rb', line 27 def transpose_data transposed_data = {} data.first.keys.map { |key| transposed_data[key] = [] } data.each do |hash| hash.each do |key,value| transposed_data[key] << value end end transposed_data end |