Class: EPC::TabularOutputter
Constant Summary collapse
- MAX_WIDTH =
80
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#column_widths ⇒ Object
Returns the value of attribute column_widths.
-
#fields ⇒ Object
Returns the value of attribute fields.
Instance Method Summary collapse
- #break_content_for(element, field) ⇒ Object
- #compute_col_max_widths ⇒ Object
-
#initialize(collection, fields) ⇒ TabularOutputter
constructor
A new instance of TabularOutputter.
- #line_for(element) ⇒ Object
- #normalize_col_widths ⇒ Object
- #parse ⇒ Object
- #parse_fields(fields) ⇒ Object
- #print(options = {}) ⇒ Object
Constructor Details
#initialize(collection, fields) ⇒ TabularOutputter
Returns a new instance of TabularOutputter.
7 8 9 10 11 12 |
# File 'lib/epc/tabular_outputter.rb', line 7 def initialize(collection, fields) @padding = 2 @collection = collection || [] parse_fields(fields) end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
6 7 8 |
# File 'lib/epc/tabular_outputter.rb', line 6 def collection @collection end |
#column_widths ⇒ Object
Returns the value of attribute column_widths.
6 7 8 |
# File 'lib/epc/tabular_outputter.rb', line 6 def column_widths @column_widths end |
#fields ⇒ Object
Returns the value of attribute fields.
6 7 8 |
# File 'lib/epc/tabular_outputter.rb', line 6 def fields @fields end |
Instance Method Details
#break_content_for(element, field) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/epc/tabular_outputter.rb', line 59 def break_content_for(element, field) content = get_content(element, field) broken_line = [] return {field => [content]} if content.length < @column_widths[field] while !content.empty? if content.length <= @column_widths[field] broken_line << content.slice!(0..content.length) else line = content[0..@column_widths[field]] if line.include?("\n") position = [line.rindex("\n"), 0].max else position = [line.rindex(/[ ,\.]/) - 1, 0].max end broken_line << content.slice!(0..(position || @column_widths[field])).gsub("\n", "") end end broken_line.delete_if{|a| a == " "} {field => broken_line} end |
#compute_col_max_widths ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/epc/tabular_outputter.rb', line 30 def compute_col_max_widths @column_widths = {} @fields.each do |field| @column_widths[field] = -1 @collection.each do |item| content = get_content(item, field) @column_widths[field] = [content.length, @column_widths[field]].max end end end |
#line_for(element) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/epc/tabular_outputter.rb', line 81 def line_for(element) broken_lines = [] line = "" @fields.each do |field| broken_lines << break_content_for(element,field) end longest_line = broken_lines.collect{|line| line.values.first.size}.max longest_line.times do |index| broken_lines.each do |broken_line| b_line = broken_line.values.first field = broken_line.keys.first content = (b_line[index] || (" " * @column_widths[field])) if content =~ /\D/ line << content.ljust(@column_widths[field]).ljust(@column_widths[field] + @padding) else line << content.rjust(@column_widths[field]).ljust(@column_widths[field] + @padding) end end line << "\n" end line end |
#normalize_col_widths ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/epc/tabular_outputter.rb', line 41 def normalize_col_widths extra = 0 while table_length > padded_width + extra max_field, max_length = @column_widths.max{|a, b| a.last <=> b.last} longest = get_longest_word(max_field) available = padded_width - @column_widths.reject{|k, v| k == max_field}.values.reduce(:+) @column_widths[max_field] = [longest, available].max extra = extra + 1 end end |
#parse ⇒ Object
53 54 55 56 |
# File 'lib/epc/tabular_outputter.rb', line 53 def parse compute_col_max_widths normalize_col_widths end |
#parse_fields(fields) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/epc/tabular_outputter.rb', line 14 def parse_fields(fields) if fields.is_a? Array @fields = fields title_row = {} @fields.each do |field| title_row[field] = field.to_s.upcase.gsub("_", " ") end @collection = [title_row] + @collection elsif fields.is_a? Hash fields.each {|k, v| fields[k] = v.to_s.gsub("_", " ")} @fields = fields.keys @col_names = fields @collection = [fields] + @collection end end |
#print(options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/epc/tabular_outputter.rb', line 106 def print( = {}) parse content = "\n" content << line_for(@collection.delete_at(0)) content << ("-" * MAX_WIDTH) content << "\n" @collection.each do |element| content << line_for(element) if ![:line_separator].nil? && [:line_separator] content << ("-" * MAX_WIDTH) content << "\n" end end content end |