lib/prawn/table/cells.rb in prawn-0.11.1.pre vs lib/prawn/table/cells.rb in prawn-0.11.1
- old
+ new
@@ -52,10 +52,11 @@
# table.row(0) # selects first row
# table.rows(3..4) # selects rows four and five
#
def rows(row_spec)
index_cells unless @indexed
+ row_spec = transform_spec(row_spec, @row_count)
Cells.new(@rows[row_spec] ||= select{ |c| row_spec === c.row })
end
alias_method :row, :rows
# Limits selection to the given column or columns. +col_spec+ can be
@@ -63,13 +64,14 @@
# column numbers; most commonly a number or a range.
#
# table.column(0) # selects first column
# table.columns(3..4) # selects columns four and five
#
- def columns(row_spec)
+ def columns(col_spec)
index_cells unless @indexed
- Cells.new(@columns[row_spec] ||= select{ |c| row_spec === c.column })
+ col_spec = transform_spec(col_spec, @column_count)
+ Cells.new(@columns[col_spec] ||= select{ |c| col_spec === c.column })
end
alias_method :column, :columns
# Allows you to filter the given cells by arbitrary properties.
#
@@ -102,14 +104,11 @@
# This allows you to set more complicated properties:
#
# table.cells.style { |cell| cell.border_width += 12 }
#
def style(options={}, &block)
- each do |cell|
- options.each { |k, v| cell.send("#{k}=", v) }
- block.call(cell) if block
- end
+ each { |cell| cell.style(options, &block) }
end
# Returns the total width of all columns in the selected set.
#
def width
@@ -180,10 +179,28 @@
@columns[cell.column] ||= []
@columns[cell.column] << cell
end
+ @row_count = @rows.size
+ @column_count = @columns.size
+
@indexed = true
+ end
+
+ # Transforms +spec+, a column / row specification, into an object that
+ # can be compared against a row or column number using ===. Normalizes
+ # negative indices to be positive, given a total size of +total+.
+ #
+ def transform_spec(spec, total)
+ case spec
+ when Range
+ transform_spec(spec.begin, total)..transform_spec(spec.end, total)
+ when Integer
+ spec < 0 ? (total + spec) : spec
+ else # pass through
+ spec
+ end
end
end
end
end