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

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/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/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/renderer/text.rb', line 118

def header
  @header
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#eachObject



148
149
150
151
152
153
# File 'lib/alf/renderer/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/alf/renderer/text.rb', line 130

def each_line(pretty = options[:pretty])
  if pretty && trim = options[: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

#sepObject



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

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

#sizesObject



120
121
122
123
124
# File 'lib/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



155
156
157
# File 'lib/alf/renderer/text.rb', line 155

def to_s
  each.each_with_object(""){|line,buf| buf << line}
end