pivot_table 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rvmrc CHANGED
@@ -1,22 +1 @@
1
- #!/usr/bin/env bash
2
-
3
- environment_id="ruby-1.9.2-p290@pivot_table"
4
-
5
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
6
- && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
7
- then
8
- \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
9
-
10
- if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
11
- then
12
- . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
13
- fi
14
- echo "RVM environment: ${environment_id}"
15
- else
16
- # If the environment file has not yet been created, use the RVM CLI to select.
17
- if ! rvm --create "$environment_id"
18
- then
19
- echo "Failed to create RVM environment '${environment_id}'."
20
- exit 1
21
- fi
22
- fi
1
+ rvm use ruby-1.9.3-p125@pivot_table --create
data/.travis.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  rvm:
2
- - 1.9.2
2
+ - 1.9.3
data/CHANGELOG.md CHANGED
@@ -1,17 +1,17 @@
1
1
  Changelog
2
2
  =========
3
+ 0.1.2
4
+ ---
5
+ Removed testing gems and updated Ruby version to 1.9.3
3
6
 
4
7
  0.0.3
5
- -----
6
-
8
+ ---
7
9
  Renamed gem to pivot_table
8
10
 
9
11
  0.0.2
10
- -----
11
-
12
+ ---
12
13
  Major refactoring to allow unbalanced dataset to be pivoted.
13
14
 
14
15
  0.0.1
15
- -----
16
-
16
+ ---
17
17
  Initial release.
data/Gemfile CHANGED
@@ -6,12 +6,12 @@ gemspec
6
6
  gem 'rake'
7
7
 
8
8
  group :development, :test do
9
- gem 'autotest'
9
+ #gem 'autotest'
10
10
  #gem 'autotest-growl'
11
- gem 'autotest-notification'
11
+ #gem 'autotest-notification'
12
12
  gem 'bundler'
13
- gem 'launchy'
14
- gem 'rcov'
13
+ #gem 'launchy'
14
+ #gem 'rcov'
15
15
  gem 'rspec'
16
16
  gem 'shoulda-matchers'
17
17
  end
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Pivot Table [![Build Status](https://secure.travis-ci.org/edjames/pivot_table.png)](http://travis-ci.org/edjames/pivot_table)
1
+ Pivot Table [![Build Status](https://secure.travis-ci.org/edjames/pivot_table.png)](http://travis-ci.org/edjames/pivot_table) [![Code Climate](https://codeclimate.com/github/edjames/pivot_table.png)](https://codeclimate.com/github/edjames/pivot_table)
2
2
  ===========
3
3
 
4
4
  A handy tool for transforming a dataset into a spreadsheet-style pivot table.
@@ -16,12 +16,12 @@ Couldn't be easier...
16
16
 
17
17
  gem install pivot_table
18
18
 
19
- There are no dependencies and pivot will work on any version of Ruby.
19
+ There are no dependencies and pivot *should* work on any version of Ruby.
20
20
 
21
21
  Usage
22
22
  -----
23
23
 
24
- At the very least, you will need to provide four things to create a pivot table...
24
+ At the very least, you will need to provide three things to create a pivot table...
25
25
 
26
26
  * a dataset (this doesn't necessarily have to be an ActiveRecord dataset, but it should at least behave like ActiveRecord e.g. OpenStruct)
27
27
  * the method to be used as column names
@@ -43,12 +43,16 @@ Let's say you have a collection of Order objects that looks like this:
43
43
  Instantiate a new PivotTable::Grid object like this...
44
44
 
45
45
  grid = PivotTable::Grid.new do |g|
46
- g.sourcedata = data
46
+ g.source_data = data
47
47
  g.column_name = :quarter
48
48
  g.row_name = :city
49
+ g.value_name = :sales
49
50
  end
50
51
 
51
52
 
53
+ The `value_name` parameter is only required if you want to access totals;
54
+ the others are required.
55
+
52
56
  All you have to do now is build the grid...
53
57
 
54
58
  g.build
@@ -93,6 +97,17 @@ E.g. The rows and columns collections make it very easy to produce horizontal, v
93
97
 
94
98
  Ah, that's better.
95
99
 
100
+ If you want to get the totals for rows, columns, or the entire grid, you can pass a `value_name` as shown above, and then query the Grid like this:
101
+
102
+ g.column_totals
103
+ g.columns[0].total
104
+ g.columns[1].total
105
+ g.columns[2].total
106
+ g.row_totals
107
+ g.rows[0].total
108
+ g.rows[1].total
109
+ g.grand_total
110
+
96
111
  Still to come
97
112
  -------------
98
113
 
@@ -121,4 +136,4 @@ If you want to contribute:
121
136
  Copyright
122
137
  ---------
123
138
 
124
- Copyright (c) 2011 Ed James. See LICENSE for details.
139
+ Copyright (c) 2011 Ed James. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'bundler/gem_tasks'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
- desc "Fix the xml column types from a SqlServer database in schema.rb"
4
+ desc "Run all specs"
5
5
  task :default do
6
6
  exec 'rspec spec'
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module PivotTable
2
2
  class Column
3
3
 
4
- ACCESSORS = [:header, :data, :total]
4
+ ACCESSORS = [:header, :data, :value_name]
5
5
 
6
6
  ACCESSORS.each do |a|
7
7
  self.send(:attr_accessor, a)
@@ -13,5 +13,9 @@ module PivotTable
13
13
  end
14
14
  end
15
15
 
16
+ def total
17
+ data.inject(0){|t,x| t + (x ? x.send(value_name) : 0)}
18
+ end
19
+
16
20
  end
17
- end
21
+ end
@@ -1,7 +1,7 @@
1
1
  module PivotTable
2
2
  class Grid
3
3
 
4
- attr_accessor :source_data, :row_name, :column_name
4
+ attr_accessor :source_data, :row_name, :column_name, :value_name
5
5
  attr_reader :columns, :rows, :data_grid
6
6
 
7
7
  def initialize(&block)
@@ -18,14 +18,14 @@ module PivotTable
18
18
  def build_rows
19
19
  @rows = []
20
20
  @data_grid.each_with_index do |data, index|
21
- @rows << Row.new(:header => row_headers[index], :data => data)
21
+ @rows << Row.new(:header => row_headers[index], :data => data, :value_name => value_name)
22
22
  end
23
23
  end
24
24
 
25
25
  def build_columns
26
26
  @columns = []
27
27
  @data_grid.transpose.each_with_index do |data, index|
28
- @columns << Column.new(:header => column_headers[index], :data => data)
28
+ @columns << Column.new(:header => column_headers[index], :data => data, :value_name => value_name)
29
29
  end
30
30
  end
31
31
 
@@ -37,6 +37,18 @@ module PivotTable
37
37
  headers @row_name
38
38
  end
39
39
 
40
+ def column_totals
41
+ columns.map{|c| c.total}
42
+ end
43
+
44
+ def row_totals
45
+ rows.map{|r| r.total}
46
+ end
47
+
48
+ def grand_total
49
+ column_totals.inject(0){|t,x| t + x}
50
+ end
51
+
40
52
  def prepare_grid
41
53
  @data_grid = []
42
54
  row_headers.count.times do
@@ -63,4 +75,4 @@ module PivotTable
63
75
  end
64
76
 
65
77
  end
66
- end
78
+ end
@@ -1,7 +1,7 @@
1
1
  module PivotTable
2
2
  class Row
3
3
 
4
- ACCESSORS = [:header, :data, :total]
4
+ ACCESSORS = [:header, :data, :value_name]
5
5
 
6
6
  ACCESSORS.each do |a|
7
7
  self.send(:attr_accessor, a)
@@ -13,5 +13,9 @@ module PivotTable
13
13
  end
14
14
  end
15
15
 
16
+ def total
17
+ data.inject(0){|t,x| t + (x ? x.send(value_name) : 0)}
18
+ end
19
+
16
20
  end
17
- end
21
+ end
data/lib/pivot_table.rb CHANGED
@@ -5,5 +5,5 @@ require "pivot_table/column"
5
5
  require "pivot_table/row"
6
6
 
7
7
  module PivotTable
8
- VERSION = "0.1.1"
8
+ VERSION = "0.1.3"
9
9
  end
data/pivot_table.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.platform = Gem::Platform::RUBY
15
15
  s.required_ruby_version = '>= 1.9'
16
-
16
+
17
17
  s.rubyforge_project = "pivot_table"
18
18
  s.rubygems_version = ">= 1.6.1"
19
19
  s.files = `git ls-files`.split("\n")
@@ -16,8 +16,7 @@ module PivotTable
16
16
 
17
17
  its(:header) { should == 'header' }
18
18
  its(:data) { should == 'data' }
19
- its(:total) { should == 'total' }
20
19
  end
21
20
 
22
21
  end
23
- end
22
+ end
@@ -22,12 +22,16 @@ module PivotTable
22
22
  let(:column_0) { [d1, d4] }
23
23
  let(:column_1) { [d2, d5] }
24
24
  let(:column_2) { [d3, d6] }
25
+ let(:column_totals) { [d1.id + d4.id, d2.id + d5.id, d3.id + d6.id] }
26
+ let(:row_totals) { [d1.id + d2.id + d3.id, d4.id + d5.id + d6.id] }
27
+ let(:grand_total) { d1.id + d2.id + d3.id + d4.id + d5.id + d6.id }
25
28
 
26
29
  let(:instance) do
27
30
  Grid.new do |g|
28
31
  g.source_data = data
29
32
  g.row_name = :row_name
30
33
  g.column_name = :column_name
34
+ g.value_name = :id
31
35
  end
32
36
  end
33
37
 
@@ -38,6 +42,7 @@ module PivotTable
38
42
  it { should respond_to :column_name }
39
43
  it { should respond_to :columns }
40
44
  it { should respond_to :rows }
45
+ it { should respond_to :grand_total }
41
46
  end
42
47
 
43
48
  describe 'build' do
@@ -57,19 +62,22 @@ module PivotTable
57
62
  context '1st column' do
58
63
  subject { build_result.columns[0] }
59
64
  its(:header) { should == column_headers[0] }
60
- its(:data) { should = column_0 }
65
+ its(:data) { should == column_0 }
66
+ its(:total) { should == column_totals[0] }
61
67
  end
62
68
 
63
69
  context '2nd column' do
64
70
  subject { build_result.columns[1] }
65
71
  its(:header) { should == column_headers[1] }
66
- its(:data) { should = column_1 }
72
+ its(:data) { should == column_1 }
73
+ its(:total) { should == column_totals[1] }
67
74
  end
68
75
 
69
76
  context '3rd column' do
70
77
  subject { build_result.columns[2] }
71
78
  its(:header) { should == column_headers[2] }
72
- its(:data) { should = column_2 }
79
+ its(:data) { should == column_2 }
80
+ its(:total) { should == column_totals[2] }
73
81
  end
74
82
  end
75
83
 
@@ -85,13 +93,15 @@ module PivotTable
85
93
  context '1st row' do
86
94
  subject { build_result.rows[0] }
87
95
  its(:header) { should == row_headers[0] }
88
- its(:data) { should = row_0 }
96
+ its(:data) { should == row_0 }
97
+ its(:total) { should == row_totals[0] }
89
98
  end
90
99
 
91
100
  context '2nd row' do
92
101
  subject { build_result.rows[1] }
93
102
  its(:header) { should == row_headers[1] }
94
- its(:data) { should = row_1 }
103
+ its(:data) { should == row_1 }
104
+ its(:total) { should == row_totals[1] }
95
105
  end
96
106
  end
97
107
 
@@ -107,6 +117,13 @@ module PivotTable
107
117
  subject { build_result.data_grid }
108
118
  it { should == [[d1, d2, d3], [d4, d5, d6]] }
109
119
  end
120
+
121
+ context 'totals' do
122
+ subject { build_result }
123
+ its(:column_totals) { should == column_totals }
124
+ its(:row_totals) { should == row_totals }
125
+ its(:grand_total) { should == grand_total }
126
+ end
110
127
  end
111
128
  end
112
- end
129
+ end
@@ -12,12 +12,11 @@ module PivotTable
12
12
  it { should respond_to :total }
13
13
 
14
14
  context 'initialize with hash' do
15
- subject { klass.new(header: 'header', data: 'data', total: 'total')}
15
+ subject { klass.new(header: 'header', data: 'data')}
16
16
 
17
17
  its(:header) { should == 'header' }
18
18
  its(:data) { should == 'data' }
19
- its(:total) { should == 'total' }
20
19
  end
21
20
 
22
21
  end
23
- end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pivot_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-28 00:00:00.000000000Z
12
+ date: 2013-10-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Transform an ActiveRecord-ish data set into a pivot table of objects
15
- email: ed.james.email@gmail.com
15
+ email: !binary |-
16
+ ZWQuamFtZXMuZW1haWxAZ21haWwuY29t
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files: []
@@ -55,8 +56,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  version: '0'
56
57
  requirements: []
57
58
  rubyforge_project: pivot_table
58
- rubygems_version: 1.8.6
59
+ rubygems_version: 1.8.15
59
60
  signing_key:
60
61
  specification_version: 3
61
- summary: pivot_table-0.1.1
62
- test_files: []
62
+ summary: pivot_table-0.1.3
63
+ test_files:
64
+ - spec/pivot_table/column_spec.rb
65
+ - spec/pivot_table/grid_spec.rb
66
+ - spec/pivot_table/row_spec.rb
67
+ - spec/spec_helper.rb