Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Some README stuff

  • Loading branch information...
commit 35e3e98424a53c7afa587089497f1641aa54704d 1 parent df15f9f
Martin Gamsjaeger snusnu authored

Showing 1 changed file with 102 additions and 1 deletion. Show diff stats Hide diff stats

  1. +102 1 README.rdoc
103 README.rdoc
Source Rendered
... ... @@ -1,6 +1,107 @@
1 1 = data_table
2 2
3   -Description goes here.
  3 +In memory representation and manipulation of tabular data. Useful for powering all kinds of charts and exports.
  4 +
  5 +
  6 + class OrderPosition
  7 +
  8 + attr_accessor :articlenum
  9 + attr_accessor :name
  10 + attr_accessor :ordered
  11 + attr_accessor :group
  12 + attr_accessor :quantity
  13 + attr_accessor :price
  14 +
  15 + def initialize(articlenum, name, group, ordered, quantity, price)
  16 + @articlenum = articlenum
  17 + @name = name
  18 + @group = group
  19 + @ordered = ordered
  20 + @quantity = quantity
  21 + @price = price
  22 + end
  23 +
  24 + end
  25 +
  26 + order_positions = [
  27 + OrderPosition.new "23423424", "R2D1", "AA", Date.new(2009, 10, 4), 1, 1000.0,
  28 + OrderPosition.new "56456345", "R2D2", "AA", Date.new(2009, 9, 4), 1, 100.0,
  29 + OrderPosition.new "42342344", "R2D3", "AA", Date.new(2009, 9, 4), 1, 10.0,
  30 + OrderPosition.new "56757657", "R2D4", "BB", Date.new(2009, 10, 4), 1, 200.0,
  31 + OrderPosition.new "56678799", "R2D5", "BB", Date.new(2009, 10, 4), 1, 20.0
  32 + ]
  33 +
  34 +
  35 + # ----------------------------------------
  36 + # DataTable block API
  37 + # ----------------------------------------
  38 +
  39 + # order_positions will be available as source inside the block
  40 +
  41 + t = DataTable.new(order_positions) do
  42 +
  43 + # Variant 1: implicit method lookup
  44 +
  45 + column :articlenum, "Articlenum"
  46 + column :name, "Product Name"
  47 + column :ordered, "Datum"
  48 + column :group, "Product Group"
  49 +
  50 + # Variant 2: explicit transformer function that yields an item of your collection
  51 +
  52 + column :amount, "Amount", lambda { |p| p.quantity*p.price }
  53 +
  54 + # Variant 3: explicit row modification (see build_rows)
  55 +
  56 + column(:doubled_price, "Doubled Price")
  57 + build_rows(source) do |row, p| # block can be omitted
  58 + row[:doubled_price] = p.price * 2
  59 + end
  60 +
  61 + end
  62 +
  63 +
  64 + # ----------------------------------------
  65 + # DataTable public method API
  66 + # ----------------------------------------
  67 +
  68 + t = DataTable.new
  69 +
  70 + # Variant 1: implicit method lookup
  71 +
  72 + t.column :articlenum, "Articlenum"
  73 + t.column :name, "Product Name"
  74 + t.column :ordered, "Datum"
  75 + t.column :group, "Product Group"
  76 +
  77 + # Variant 2: explicit transformer function that yields an item of your collection
  78 +
  79 + t.column(:amount, "Amount", lambda { |p| p.quantity*p.price })
  80 +
  81 + # Variant 3: explicit row modification (see build_rows)
  82 +
  83 + t.column(:doubled_price, "Doubled Price")
  84 + t.build_rows(order_positions) do |row, p| # block can be omitted
  85 + row[:doubled_price] = p.price * 2
  86 + end
  87 +
  88 +
  89 + # ----------------------------------------
  90 + # Grouping
  91 + # ----------------------------------------
  92 +
  93 + grouped_table = t.group_by(:ordered, {
  94 + :amount => DataTable::Aggregators::SUM,
  95 + :doubled_price => DataTable::Aggregators::SUM
  96 + }, lambda { |x| "#{x.year}-#{x.month}" })
  97 +
  98 +
  99 + # ----------------------------------------
  100 + # Exporting
  101 + # ----------------------------------------
  102 +
  103 + grouped_table.to_csv # defaults to (:col_sep => ",")
  104 +
4 105
5 106 == Note on Patches/Pull Requests
6 107

0 comments on commit 35e3e98

Please sign in to comment.
Something went wrong with that request. Please try again.