Skip to content

Commit

Permalink
Allow Hash syntax for Table's :row_colors
Browse files Browse the repository at this point in the history
  • Loading branch information
bradediger committed Sep 22, 2009
1 parent e2ebf0a commit 9d6b968
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ class Table
# <tt>:position</tt>:: One of <tt>:left</tt>, <tt>:center</tt> or <tt>n</tt>, where <tt>n</tt> is an x-offset from the left edge of the current bounding box
# <tt>:width</tt>:: A set width for the table, defaults to the sum of all column widths
# <tt>:column_widths</tt>:: A hash of indices and widths in PDF points. E.g. <tt>{ 0 => 50, 1 => 100 }</tt>
# <tt>:row_colors</tt>:: An array of row background colors which are used cyclicly.
# <tt>:row_colors</tt>:: Used to specify background colors for rows. See below for usage.
# <tt>:align</tt>:: Alignment of text in columns, for entire table (<tt>:center</tt>) or by column (<tt>{ 0 => :left, 1 => :center}</tt>)
#
# Row colors are specified as html encoded values, e.g.
# ["ffffff","aaaaaa","ccaaff"]. You can also specify
# <tt>:row_colors => :pdf_writer</tt> if you wish to use the default color
# scheme from the PDF::Writer library.
# Row colors (<tt>:row_colors</tt>) are specified as HTML hex color values,
# e.g., "ccaaff". They can take several forms:
#
# * An array of colors, used cyclically to "zebra stripe" the table: <tt>['ffffff', 'cccccc', '336699']</tt>.
# * A hash taking 0-based row numbers to colors: <tt>{ 0 => 'ffffff', 2 => 'cccccc'}</tt>.
# * The symbol <tt>:pdf_writer</tt>, for PDF::Writer's default color scheme.
#
# See Document#table for typical usage, as directly using this class is
# not recommended unless you know why you want to do it.
Expand Down Expand Up @@ -252,6 +254,13 @@ def generate_table
renderable_data.each_with_index do |row,index|
c = Prawn::Table::CellBlock.new(@document)

if C(:row_colors).is_a?(Hash)
real_index = index
real_index -= 1 if C(:headers)
color = C(:row_colors)[real_index]
c.background_color = color if color
end

col_index = 0
row.each do |e|
case C(:align)
Expand Down Expand Up @@ -379,6 +388,8 @@ def draw_page(contents)


def next_row_color
return if C(:row_colors).is_a?(Hash)

color = C(:row_colors).shift
C(:row_colors).push(color)
color
Expand Down
16 changes: 16 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@
pdf.table(data, :row_colors => ['cccccc', 'ffffff'])
end

it "should allow hash syntax for :row_colors" do
data = [["foo"], ["bar"], ['baz']]
pdf = Prawn::Document.new

# fill_color() is used to retrieve fill color; ignore it
pdf.stubs(:fill_color)

# Verify that fill_color is called in proper sequence for row colors.
seq = sequence('row_colors')
%w[cccccc dddddd eeeeee].each do |color|
pdf.expects(:fill_color).with(color).in_sequence(seq)
end

pdf.table(data, :row_colors => {0 => 'cccccc', 1 => 'dddddd',
2 => 'eeeeee'})
end
end

describe "An invalid table" do
Expand Down

0 comments on commit 9d6b968

Please sign in to comment.