Class: Bmg::Writer::Text::Table
- Inherits:
-
Object
- Object
- Bmg::Writer::Text::Table
- Includes:
- Utils
- Defined in:
- lib/bmg/writer/text.rb
Overview
class Row
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #each ⇒ Object
- #each_line(pretty = ) ⇒ Object
-
#initialize(renderer, records, attributes) ⇒ Table
constructor
A new instance of Table.
- #sep ⇒ Object
- #sizes ⇒ Object
- #to_s ⇒ Object
Methods included from Utils
Constructor Details
#initialize(renderer, records, attributes) ⇒ Table
Returns a new instance of Table.
113 114 115 116 117 |
# File 'lib/bmg/writer/text.rb', line 113 def initialize(renderer, records, attributes) @renderer = renderer @header = Row.new(renderer, attributes.map(&:to_s)) @rows = records.map{|r| Row.new(renderer, r) } end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
118 119 120 |
# File 'lib/bmg/writer/text.rb', line 118 def header @header end |
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
118 119 120 |
# File 'lib/bmg/writer/text.rb', line 118 def renderer @renderer end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
118 119 120 |
# File 'lib/bmg/writer/text.rb', line 118 def rows @rows end |
Instance Method Details
#each ⇒ Object
148 149 150 151 152 153 |
# File 'lib/bmg/writer/text.rb', line 148 def each return to_enum unless block_given? each_line do |line| yield(line.strip << "\n") end end |
#each_line(pretty = ) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/bmg/writer/text.rb', line 130 def each_line(pretty = renderer.[:pretty]) if pretty && trim = renderer.[:trim_at] each_line(false) do |line| yield(line[0..trim]) end else yield(sep) yield(header.rendering_lines(sizes).first) yield(sep) rows.each do |row| row.rendering_lines(sizes).each do |line| yield(line) end end yield(sep) end end |
#sep ⇒ Object
126 127 128 |
# File 'lib/bmg/writer/text.rb', line 126 def sep @sep ||= '+-' << sizes.map{|s| '-' * s}.join('-+-') << '-+' end |
#sizes ⇒ Object
120 121 122 123 124 |
# File 'lib/bmg/writer/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
155 156 157 |
# File 'lib/bmg/writer/text.rb', line 155 def to_s each.each_with_object(""){|line,buf| buf << line} end |