Parent

Files

Class/Module Index [+]

Quicksearch

Mutter::Table

Attributes

columns[RW]
rows[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/mutter/table.rb, line 14
def initialize options = {}, &blk
  @columns, @rows = [], []
  @options = DefaultTable.merge options

  instance_eval(&blk) if (@fixed = block_given?)
end

Public Instance Methods

<<(row) click to toggle source
# File lib/mutter/table.rb, line 27
def << row
  if row.size > @columns.size && fixed?
    raise ArgumentError,
      "row size is #{row.size} but I only have #{@columns.size} columns"
  else
    @rows << row
  end
end
column(options = {}) click to toggle source
# File lib/mutter/table.rb, line 23
def column options = {}
  @columns << DefaultColumn.merge(options)
end
fixed?() click to toggle source
# File lib/mutter/table.rb, line 21
def fixed?; @fixed end
process(str, length = nil, align = :left, style = []) click to toggle source
# File lib/mutter/table.rb, line 69
def process str, length = nil, align = :left, style = []
  length ||= str.length

  if str.length > length
    str[0...(length - @options[:truncater].length)] + @options[:truncater]
  else
    s = [Mutter.new.clear.process(str, style), ' ' * (length - str.length)]
    s.reverse! if align == :right
    s.join
  end
end
render() click to toggle source
# File lib/mutter/table.rb, line 36
def render
  # Create missing columns as needed
  (@rows.map {|r| r.size }.max - @columns.size).times do
    self.column
  end

  # Compute max column width
  @columns.each_with_index do |col, i|
    col[:_width] = @rows.map do |r|
      r[i].to_s.length
    end.max if @rows[i]
  end

  # print table
  @rows.map do |row|
    @columns.zip(row).map do |col, cell|
      process(cell.to_s || "",
              col[:width] || col[:_width],
              col[:align],
              col[:style])
    end.join @options[:delimiter]
  end
end
Also aliased as: to_a
to_a() click to toggle source
Alias for: render
to_s() click to toggle source
# File lib/mutter/table.rb, line 61
def to_s
  render.join("\n")
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.