Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move pivot grouping operations to Operation module

  • Loading branch information...
commit e3bfad91fa8a755ff9b5c9a5a9848007709d2b13 1 parent 4e65164
@wpiekutowski wpiekutowski authored
Showing with 48 additions and 95 deletions.
  1. +30 −31 lib/ruport/data/table.rb
  2. +18 −64 test/table_pivot_test.rb
View
61 lib/ruport/data/table.rb
@@ -33,7 +33,7 @@ def initialize(table, group_col, pivot_col, summary_col, options = {})
@summary_column = summary_col
@pivot_order = options[:pivot_order]
@operation = options[:operation] || :first
- unless respond_to?("perform_#{@operation}", true)
+ unless Operation.respond_to?(@operation, true)
raise ArgumentError, "Unknown pivot operation '#{@operation}'"
end
end
@@ -74,38 +74,9 @@ def group_column_entries
end
def perform_operation(rows)
- send "perform_#{@operation}", rows
+ Operation.send @operation, rows, @summary_column
end
- private
-
- def perform_first(rows)
- rows.first && rows.first[@summary_column]
- end
-
- def perform_sum(rows)
- rows && rows.inject(0) { |sum,row| sum+row[@summary_column] }
- end
-
- def perform_count(rows)
- rows && rows.length
- end
-
- def perform_mean(rows)
- sum = rows && rows.inject(0) { |sum,row| sum+row[@summary_column] }
- sum / rows.length
- end
-
- def perform_min(rows)
- rows && (rows.collect { |r| r[@summary_column] }).min
- end
-
- def perform_max(rows)
- rows && (rows.collect { |r| r[@summary_column] }).max
- end
-
- public
-
def to_table
result = Table.new
result.add_column(@group_column)
@@ -126,6 +97,34 @@ def to_table
result
end
+ module Operation
+ extend self
+
+ def first(rows, summary_column)
+ rows.first && rows.first[summary_column]
+ end
+
+ def sum(rows, summary_column)
+ rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
+ end
+
+ def count(rows, summary_column)
+ rows && rows.length
+ end
+
+ def mean(rows, summary_column)
+ sum = rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
+ sum / rows.length
+ end
+
+ def min(rows, summary_column)
+ rows && (rows.collect { |r| r[summary_column] }).min
+ end
+
+ def max(rows, summary_column)
+ rows && (rows.collect { |r| r[summary_column] }).max
+ end
+ end
end
# Creates a new table with values from the specified pivot column
View
82 test/table_pivot_test.rb
@@ -135,92 +135,46 @@ def test_preserves_ordering_on_calculated_column_with_proc_pivot_order
class TablePivotOperationTest < Test::Unit::TestCase
def setup
- @table = Table(File.join(TEST_SAMPLES, 'sales.csv'),
- :csv_options => { :converters => :integer })
+ @rows = [
+ Ruport::Data::Record.new('Values' => 3),
+ Ruport::Data::Record.new('Values' => 9),
+ Ruport::Data::Record.new('Values' => 4)
+ ]
end
def test_performs_operation_sum
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 10, 12]
- expected << ['South', 5, 12, 6]
- expected << ['East', 10, 8, 3]
- expected << ['West', 8, 7, 10]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :sum)
- assert_equal(expected, pivoted)
+ sum = Ruport::Data::Table::Pivot::Operation.sum(@rows, 'Values')
+ assert_equal 16, sum
end
def test_performs_operation_first
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 2, 8]
- expected << ['South', 2, 5, 6]
- expected << ['East', 5, 8, 3]
- expected << ['West', 8, 1, 6]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :first)
- assert_equal(expected, pivoted)
+ first = Ruport::Data::Table::Pivot::Operation.first(@rows, 'Values')
+ assert_equal 3, first
end
def test_performs_operation_count
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 2, 2]
- expected << ['South', 2, 2, 1]
- expected << ['East', 3, 1, 1]
- expected << ['West', 1, 2, 2]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :count)
- assert_equal(expected, pivoted)
+ count = Ruport::Data::Table::Pivot::Operation.count(@rows, 'Values')
+ assert_equal 3, count
end
def test_performs_operation_mean
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 5, 6]
- expected << ['South', 2, 6, 6]
- expected << ['East', 3, 8, 3]
- expected << ['West', 8, 3, 5]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :mean)
- assert_equal(expected, pivoted)
+ mean = Ruport::Data::Table::Pivot::Operation.mean(@rows, 'Values')
+ assert_equal 5, mean
end
def test_performs_operation_min
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 2, 4]
- expected << ['South', 2, 5, 6]
- expected << ['East', 1, 8, 3]
- expected << ['West', 8, 1, 4]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :min)
- assert_equal(expected, pivoted)
+ min = Ruport::Data::Table::Pivot::Operation.min(@rows, 'Values')
+ assert_equal 3, min
end
def test_performs_operation_max
- expected = Table('Region', 'Gadget', 'Gizmo', 'Widget')
- expected << ['North', 1, 8, 8]
- expected << ['South', 3, 7, 6]
- expected << ['East', 5, 8, 3]
- expected << ['West', 8, 6, 6]
-
- pivoted = @table.pivot('Product', :group_by => 'Region',
- :values => 'Units Sold',
- :operation => :max)
- assert_equal(expected, pivoted)
+ max = Ruport::Data::Table::Pivot::Operation.max(@rows, 'Values')
+ assert_equal 9, max
end
def test_invalid_operation_causes_exception
assert_raise ArgumentError do
- @table.pivot('Product', :group_by => 'Region', :values => 'Units Sold',
- :operation => :foo)
+ Ruport::Data::Table::Pivot.new(nil, nil, nil, nil, :operation => :foo)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.