Sha256: b339ba805ad9c38f1e18d3196812307ff07b899ba06840acb237f64fea201d8f

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

# -*- coding: utf-8 -*-
module DirCat

  # From http://gist.github.com/72234
  #
  # mysql-style output for an array of ActiveRecord objects
  #
  # Usage:
  #   report(records)  # displays report with all fields
  #   report(records, :field1, :field2, ...) # displays report with given fields
  #
  # Example:
  # >> report(records, :id, :amount, :created_at)
  # +------+-----------+--------------------------------+
  # | id   | amount    | created_at                     |
  # +------+-----------+--------------------------------+
  # | 8301 | $12.40    | Sat Feb 28 09:20:47 -0800 2009 |
  # | 6060 | $39.62    | Sun Feb 15 14:45:38 -0800 2009 |
  # | 6061 | $167.52   | Sun Feb 15 14:45:38 -0800 2009 |
  # | 6067 | $12.00    | Sun Feb 15 14:45:40 -0800 2009 |
  # | 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
  # +------+-----------+--------------------------------+
  # 5 rows in set
  #
  def self.report(items, *fields)
    # find max length for each field; start with the field names themselves
    max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]

    items.each do |item|
      fields.each do |field|
        len = item.send(field).to_s.length
        max_len[field] = len if len > max_len[field]
      end
    end

    border = '+-' + fields.map {|f| '-' * max_len[f] }.join('-+-') + '-+'
    title_row = '| ' + fields.map {|f| sprintf("%-#{max_len[f]}s", f.to_s) }.join(' | ') + ' |'

    puts border
    puts title_row
    puts border

    items.each do |item|
      row = '| ' + fields.map {|f| sprintf("%-#{max_len[f]}s", item.send(f)) }.join(' | ') + ' |'
      puts row
    end

    puts border
    puts "#{items.length} rows in set\n"
  end
end

if __FILE__ == $0
  require 'ostruct'
  o1 = OpenStruct.new
  o1.a = 1
  o1.b = 2

  o2 = OpenStruct.new :a => 2, :b => 4
  
  DirCat::report( [o1, o2], :a, :b )
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dircat-0.1.5 lib/dircat/report.rb