class MonoclePrint::Table

Constants

Blank
Divider
Row
SectionTitle
Segments
TitleRow

Attributes

columns[R]
titles[R]

Public Class Methods

build( *args ) { |table| ... } click to toggle source
# File lib/monocle-print/table.rb, line 13
def self.build( *args )
  table = new( *args ) do | table |
    block_given? and yield( table )
  end
  return( table.render )
end
new( columns, options = {} ) { |self| ... } click to toggle source
# File lib/monocle-print/table.rb, line 21
def initialize( columns, options = {} )
  initialize_view( options )
  
  @titles = nil
  @item = @head = Divider.new( self, :head )
  @foot = Divider.new( self, :foot )
  @columns = []
  @body = []
  
  case columns
  when Fixnum
    expand_columns( columns )
  when Array
    title_row( *columns )
  end
  
  block_given? and yield( self )
end

Public Instance Methods

column( name_or_index ) click to toggle source
# File lib/monocle-print/table.rb, line 106
def column( name_or_index )
  case name_or_index
  when Integer, Range then @columns[ name_or_index ]
  else
    @columns.find do | col |
      name_or_index === col.title
    end
  end
end
divider( type = :row_divider ) click to toggle source
# File lib/monocle-print/table.rb, line 82
def divider( type = :row_divider )
  @item = @item.divider!( type )
end
each() { |item| ... } click to toggle source
# File lib/monocle-print/table.rb, line 48
def each
  block_given? or return( enum_for( :each ) )
  for item in @head
    yield( item )
  end
end
expand_columns(new_size) click to toggle source
# File lib/monocle-print/table.rb, line 141
def expand_columns(new_size)
  new_size.zero? and return
  
  until @columns.length >= new_size
    @columns << Column.new( self, @columns.length )
  end
end
fixed_columns( *column_indicies ) click to toggle source
# File lib/monocle-print/table.rb, line 86
def fixed_columns( *column_indicies )
  for column_index in column_indicies
    if c = column( column_indicies )
      if Array === c then c.each { | i | i.fixed = true }
      else c.fixed = true
      end
    end
  end
end
inner_width() click to toggle source
# File lib/monocle-print/table.rb, line 133
def inner_width
  @inner_width or calculate_inner_width
end
malleable_columns( *column_indicies ) click to toggle source
# File lib/monocle-print/table.rb, line 96
def malleable_columns( *column_indicies )
  for column_index in column_indicies
    if c = column( column_index )
      if Array === c then c.each { | i | i.malleable = true }
      else c.malleable = true
      end
    end
  end
end
render_content( out ) click to toggle source
# File lib/monocle-print/table.rb, line 40
def render_content( out )
  style = @style || out.style
  lock do
    width > out.width and resize( out.width )
    each { | member | member.render( out, style ) }
  end
end
resize( new_size ) click to toggle source
# File lib/monocle-print/table.rb, line 116
def resize( new_size )
  resizable = @columns.select { | c | c.malleable? }
  if resizable.empty?
    warn( "cannot resize #{ self.inspect } as all columns are fixed" )
    return( self )
  end
  
  lock do
    difference = new_size - @width
    resize_columns( difference, resizable )
  end
  
  return( self )
end
row( *members ) click to toggle source
# File lib/monocle-print/table.rb, line 57
def row( *members )
  @item = @item.row!( *members )
end
rows( *list_of_rows ) click to toggle source
# File lib/monocle-print/table.rb, line 73
def rows( *list_of_rows )
  for row in list_of_rows do row( *row ) end
  self
end
section( title, options = {} ) click to toggle source
# File lib/monocle-print/table.rb, line 78
def section( title, options = {} )
  @item = @item.section!( title, options )
end
title_row( *members ) click to toggle source
# File lib/monocle-print/table.rb, line 61
def title_row( *members )
  @titles = @item = @item.title_row!( *members )
  while @titles
    if TitleRow === @titles
      @titles = @titles.cells.map { | c | c.to_s }
      break
    end
    @titles = @titles.before
  end
  self
end
width() click to toggle source
# File lib/monocle-print/table.rb, line 137
def width
  @width or calculate_width
end

Private Instance Methods

calculate_inner_width() click to toggle source
# File lib/monocle-print/table.rb, line 206
def calculate_inner_width
  w = @columns.inject( 0 ) { | w, c | w + c.width }
  w + ( @columns.length - 1 ) * 3
end
calculate_metrics() click to toggle source
# File lib/monocle-print/table.rb, line 194
def calculate_metrics
  @columns.each { | c | c.calculate_metrics }
  @inner_width = calculate_inner_width
  @width = calculate_width
end
calculate_width() click to toggle source
# File lib/monocle-print/table.rb, line 211
def calculate_width
  calculate_inner_width + 4
end
clear_metrics() click to toggle source
# File lib/monocle-print/table.rb, line 200
def clear_metrics
  @columns.each { | c | c.clear_metrics }
  @inner_width = nil
  @width = nil
end
expand( amount, columns ) click to toggle source
# File lib/monocle-print/table.rb, line 167
def expand( amount, columns )
  leftover = amount
  resizable_area = columns.inject( 0 ) do | sz, c |
    sz + c.width
  end
  
  for column in columns
    proportion = ( column.width.to_f / resizable_area )
    delta = ( proportion * amount ).round
    column.width += delta
    leftover -= delta
  end
  
  columns.last.width += leftover
end
lock() { || ... } click to toggle source
# File lib/monocle-print/table.rb, line 185
def lock
  calculate_metrics
  @item.link( @foot )
  yield
ensure
  @foot.unlink
  clear_metrics
end
resize_columns( amount, columns ) click to toggle source
# File lib/monocle-print/table.rb, line 151
def resize_columns( amount, columns )
  leftover = amount
  resizable_area = columns.inject( 0 ) do | sz, c |
    sz + c.width
  end
  
  for column in columns
    proportion = ( column.width.to_f / resizable_area )
    delta = ( proportion * amount ).round
    column.width += delta
    leftover -= delta
  end
  
  columns.last.width += leftover
end