Class: Alf::Renderer::Text::Table
- Inherits:
-
Object
- Object
- Alf::Renderer::Text::Table
- Includes:
- Utils
- Defined in:
- lib/alf-io/alf/renderer/text.rb
Overview
class Row
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #each {|sep| ... } ⇒ Object
-
#initialize(records, attributes, options = {}) ⇒ Table
constructor
A new instance of Table.
- #sep ⇒ Object
- #sizes ⇒ Object
- #to_s ⇒ Object
Methods included from Utils
Constructor Details
#initialize(records, attributes, options = {}) ⇒ Table
Returns a new instance of Table.
113 114 115 116 117 |
# File 'lib/alf-io/alf/renderer/text.rb', line 113 def initialize(records, attributes, = {}) @header = Row.new(attributes) @rows = records.map{|r| Row.new(r, )} = end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
118 119 120 |
# File 'lib/alf-io/alf/renderer/text.rb', line 118 def header @header end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
118 119 120 |
# File 'lib/alf-io/alf/renderer/text.rb', line 118 def end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
118 119 120 |
# File 'lib/alf-io/alf/renderer/text.rb', line 118 def rows @rows end |
Instance Method Details
#each {|sep| ... } ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/alf-io/alf/renderer/text.rb', line 130 def each return to_enum unless block_given? yield(sep) yield header.rendering_lines(sizes).first << "\n" yield(sep) rows.each do |row| row.rendering_lines(sizes).each do |line| yield(line << "\n") end end yield(sep) end |
#sep ⇒ Object
126 127 128 |
# File 'lib/alf-io/alf/renderer/text.rb', line 126 def sep @sep ||= '+-' << sizes.map{|s| '-' * s}.join('-+-') << '-+' << "\n" end |
#sizes ⇒ Object
120 121 122 123 124 |
# File 'lib/alf-io/alf/renderer/text.rb', line 120 def sizes @sizes ||= rows.inject(header.min_widths) do |memo,row| memo.zip(row.min_widths).map{|x,y| max(x,y)} end end |
#to_s ⇒ Object
143 144 145 |
# File 'lib/alf-io/alf/renderer/text.rb', line 143 def to_s each.each_with_object(""){|line,buf| buf << line} end |