class SimpleTable

Attributes

divider[RW]
padding[RW]
placeholder[RW]

Public Instance Methods

csv(separator=@separator) click to toggle source
# File lib/simpletable.rb, line 39
def csv(separator=@separator)
  @separator = separator if @separator != separator
  # quote strings w/ embedded separator characters
  titles = []
  @titles.each {|t| titles << (t.include?(@separator) ? t.gsub(t,"\"#{t}\"") : t)}

  # print table header
  text = titles.join(@separator) << "\n"

  # print table body
  @objects.each do |o|
    data = @methods.collect{ |m| call_method(o,m) } # collect row data
    text << data.join(@separator) << "\n"
  end
  text
end
from_objects( objects, titles, methods, options = {} ) click to toggle source
# File lib/simpletable.rb, line 9
def from_objects( objects, titles, methods, options = {} )
  raise "Mismatched number of methods and column titles" if titles.length != methods.length
  @objects = objects
  @titles = titles
  @methods = methods
  @divider = options[:divider] || DEFAULT_DIVIDER
  @padding = options[:padding] || DEFAULT_PADDING
  @placeholder = options[:placeholder] || DEFAULT_PLACEHOLDER
  self
end
text() click to toggle source
# File lib/simpletable.rb, line 20
def text
  widths = []
  # calculate column widths
  @titles.zip(@methods).each do |title,method|
    widths << @objects.collect { |o| call_method(o,method) }.push(title).group_by(&:size).max.first + @padding
  end

  # print table header
  text = row(@titles,widths)
  text << (@divider * (widths.inject(:+) - @padding)) << "\n"  # sum of column widths - padding

  # print table body
  @objects.each do |o|
    data = @methods.collect{ |m| call_method(o,m) } # collect row data
    text << row(data,widths)
  end
  text
end

Private Instance Methods

call_method(obj,methods) click to toggle source
# File lib/simpletable.rb, line 63
def call_method(obj,methods)
  begin
    ret = obj
    # cast methods to array, for nested calls
    methods = [methods].flatten
    methods.each do |m|
      ret = ret.send(m)
    end
    ret.to_s
  rescue NoMethodError
    @placeholder == @separator ? "\"#{@placeholder}\"" : @placeholder
  end
end
row(data,widths) click to toggle source
# File lib/simpletable.rb, line 57
def row(data,widths)
  row = ""
  data.zip(widths).each { |d,w| row << d.to_s.ljust(w) }
  row << "\n"
end