Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Created March 1, 2009 06:01
Show Gist options
  • Save bgreenlee/72234 to your computer and use it in GitHub Desktop.
Save bgreenlee/72234 to your computer and use it in GitHub Desktop.

Revisions

  1. bgreenlee revised this gist Apr 3, 2009. 1 changed file with 44 additions and 44 deletions.
    88 changes: 44 additions & 44 deletions report.rb
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,45 @@
    # 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 report(items, *fields)
    # find max length for each field; start with the field names themselves
    fields = items.first.attribute_names unless fields.any?
    max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
    items.each do |item|
    fields.each do |field|
    len = item.read_attribute(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.read_attribute(f)) }.join(' | ') + ' |'
    puts row
    end

    puts border
    puts "#{items.length} rows in set\n"
    # 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 report(items, *fields)
    # find max length for each field; start with the field names themselves
    fields = items.first.class.column_names unless fields.any?
    max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
    items.each do |item|
    fields.each do |field|
    len = item.read_attribute(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.read_attribute(f)) }.join(' | ') + ' |'
    puts row
    end

    puts border
    puts "#{items.length} rows in set\n"
    end
  2. bgreenlee revised this gist Mar 1, 2009. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions report.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # mysql-style output for an array of Ruby objects
    # mysql-style output for an array of ActiveRecord objects
    #
    # Usage:
    # report(records) # displays report with all fields
    @@ -23,7 +23,7 @@ def report(items, *fields)
    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
    len = item.read_attribute(field).to_s.length
    max_len[field] = len if len > max_len[field]
    end
    end
    @@ -36,7 +36,7 @@ def report(items, *fields)
    puts border

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

  3. bgreenlee renamed this gist Mar 1, 2009. 1 changed file with 44 additions and 44 deletions.
    88 changes: 44 additions & 44 deletions snippet.rb → report.rb
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,45 @@
    # mysql-style output for an array of Ruby 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 report(items, *fields)
    # find max length for each field; start with the field names themselves
    fields = items.first.attribute_names unless fields.any?
    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"
    # mysql-style output for an array of Ruby 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 report(items, *fields)
    # find max length for each field; start with the field names themselves
    fields = items.first.attribute_names unless fields.any?
    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
  4. bgreenlee created this gist Mar 1, 2009.
    45 changes: 45 additions & 0 deletions snippet.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # mysql-style output for an array of Ruby 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 report(items, *fields)
    # find max length for each field; start with the field names themselves
    fields = items.first.attribute_names unless fields.any?
    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