Class: Alf::Renderer::Text::Table

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/alf-io/alf/renderer/text.rb

Overview

class Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#max

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, options = {})
  @header  = Row.new(attributes)
  @rows    = records.map{|r| Row.new(r, options)}
  @options = options
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



118
119
120
# File 'lib/alf-io/alf/renderer/text.rb', line 118

def header
  @header
end

#optionsObject (readonly)

Returns the value of attribute options.



118
119
120
# File 'lib/alf-io/alf/renderer/text.rb', line 118

def options
  @options
end

#rowsObject (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

Yields:



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

#sepObject



126
127
128
# File 'lib/alf-io/alf/renderer/text.rb', line 126

def sep
  @sep ||= '+-' << sizes.map{|s| '-' * s}.join('-+-') << '-+' << "\n"
end

#sizesObject



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_sObject



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