Class: EBPS::Text::Table
- Inherits:
-
Object
- Object
- EBPS::Text::Table
- Defined in:
- lib/ebps/text/table.rb
Instance Attribute Summary collapse
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #clean! ⇒ Object
- #column_widths ⇒ Object
- #each_normalized(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Table
constructor
A new instance of Table.
- #next_cell! ⇒ Object
- #next_multi_cell! ⇒ Object
- #next_row! ⇒ Object
- #to_s ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize ⇒ Table
Returns a new instance of Table.
53 54 55 56 |
# File 'lib/ebps/text/table.rb', line 53 def initialize @rows = [[]] next_cell! end |
Instance Attribute Details
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
52 53 54 |
# File 'lib/ebps/text/table.rb', line 52 def rows @rows end |
Instance Method Details
#<<(str) ⇒ Object
117 118 119 |
# File 'lib/ebps/text/table.rb', line 117 def <<(str) @rows.last.last << str end |
#clean! ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ebps/text/table.rb', line 57 def clean! @rows.each { |row| while((cell = row.last) && cell.empty?) row.pop end } while((row = @rows.last) && row.empty?) @rows.pop end end |
#column_widths ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ebps/text/table.rb', line 67 def column_widths @rows.inject([]) { |memo, row| row.each_with_index { |cell, idx| if cell.is_a? Cell or cell.is_a? MultiCell maxlength = cell.to_s.split("\n").collect{ |part| part.length }.max candidate = maxlength.to_i / cell.col_span else candidate = cell.length end memo[idx] = [memo[idx].to_i, candidate].max } memo } end |
#each_normalized(&block) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/ebps/text/table.rb', line 98 def each_normalized(&block) wd = width @rows.each { |row| block.call(row + Array.new(wd - row.length)) } end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/ebps/text/table.rb', line 81 def empty? @rows.flatten.all? { |cell| cell.strip.empty? } end |
#next_cell! ⇒ Object
84 85 86 87 88 |
# File 'lib/ebps/text/table.rb', line 84 def next_cell! cell = Cell.new @rows.last.push cell cell end |
#next_multi_cell! ⇒ Object
89 90 91 92 93 |
# File 'lib/ebps/text/table.rb', line 89 def next_multi_cell! cell = MultiCell.new @rows.last.push cell cell end |
#next_row! ⇒ Object
94 95 96 97 |
# File 'lib/ebps/text/table.rb', line 94 def next_row! @rows.push [] next_cell! end |
#to_s ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ebps/text/table.rb', line 104 def to_s widths = column_widths @rows.collect { |row| line = '' row.each_with_index { |cell, idx| line << cell.to_s.ljust(widths.at(idx) + 2) } line }.join("\n") end |
#width ⇒ Object
114 115 116 |
# File 'lib/ebps/text/table.rb', line 114 def width @rows.collect { |row| row.length }.max end |