File: filtering.rb

package info (click to toggle)
ruby-prawn-table 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 440 kB
  • ctags: 223
  • sloc: ruby: 3,343; makefile: 5
file content (36 lines) | stat: -rw-r--r-- 1,048 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# encoding: utf-8
#
# Another way to reduce the number of cells is to <code>filter</code> the table.
#
# <code>filter</code> is just like <code>Enumerable#select</code>. Pass it a
# block and it will iterate through the cells returning a new
# <code>Prawn::Table::Cells</code> instance containing only those cells for
# which the block was not false.
#
require File.expand_path(File.join(File.dirname(__FILE__),
                                   %w[.. example_helper]))

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
  data = [ ["Item", "Jan Sales", "Feb Sales"],
           ["Oven", 17, 89],
           ["Fridge", 62, 30],
           ["Microwave", 71, 47]
         ]

  table(data) do
    values = cells.columns(1..-1).rows(1..-1)

    bad_sales = values.filter do |cell|
      cell.content.to_i < 40
    end

    bad_sales.background_color = "FFAAAA"

    good_sales = values.filter do |cell|
      cell.content.to_i > 70
    end

    good_sales.background_color = "AAFFAA"
  end
end