pivot_table 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 358c82d5e3a1a349a27b0a35e34c2a2b764fc9f5
4
- data.tar.gz: 725ff8d73693f14eee2c0d868942f8e6c58f6e1a
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjViMDZkOGYzZDNiNzFhMmQ3YTcxNTAyZGFjZGFlNzU2MTBkNWNmNg==
5
+ data.tar.gz: !binary |-
6
+ ODBmNTljNGM5MTQzMmZjMWFlMzBiOTkwMTQ2MDA0ODE1MmE0NDEyNg==
5
7
  SHA512:
6
- metadata.gz: 8c09f55135466324aec7bcd16006dc30a7f6e2b3421bb0d994be06d22ad382b0c91f55f8dda9ff94b37d5c37096cd524562b061ca4603973cec004393c481848
7
- data.tar.gz: 7a976c7c17833be45c8a3a34bfbbc9ff4d8a240d11582b2c57fa4b8406c364b9c13a33f7295251529738fb8e4e89eedb5f4e004fc3a0ab43f31e61512b259e34
8
+ metadata.gz: !binary |-
9
+ YjkwN2VjNzc0MjVlNmU4M2JkMDJkNDgxYTJlMjNhYjc0NWQ2M2I3NjM2Zjhk
10
+ NmExNTAzOTYzMGVlMmY3MDg3MzAzYWEyYjVkYmRhMDkwMDcyNjUxMzE2NzRk
11
+ MmU1Y2EzMDYzMmQ5NDcwYmMxN2Q1MDY0ZDI0YzYxODdhZjAzMDE=
12
+ data.tar.gz: !binary |-
13
+ Yjk3ZjU5YjI4ZTQ1M2FhNGQ4YzQ4MzQ1NTEzZTY1NWJmMDBhOTgyYjEwYjQz
14
+ OGU4Y2FjZGQ0Yjc4MGEzZWNmOWY5NjU4YmZkMWE4M2U1NDUxYjVmZDgxMjJk
15
+ NzM2MDM5YmI3ZTUyNjBhYjM5ZTJhOGVhMzJmMWQ3YjE3Y2VhNGI=
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) [![Code Climate](https://codeclimate.com/github/edjames/pivot_table.png)](https://codeclimate.com/github/edjames/pivot_table) [![Dependency Status](https://gemnasium.com/edjames/pivot_table.png)](https://gemnasium.com/edjames/pivot_table) [![Gem Version](https://badge.fury.io/rb/pivot_table.png)](http://badge.fury.io/rb/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) [![Dependency Status](https://gemnasium.com/edjames/pivot_table.png)](https://gemnasium.com/edjames/pivot_table) [![Gem Version](https://badge.fury.io/rb/pivot_table.png)](http://badge.fury.io/rb/pivot_table) [![pivot_table API Documentation](https://www.omniref.com/ruby/gems/pivot_table.png)](https://www.omniref.com/ruby/gems/pivot_table)
2
2
 
3
3
  A handy tool for transforming a dataset into a spreadsheet-style pivot table.
4
4
 
@@ -43,7 +43,6 @@ Instantiate a new PivotTable::Grid object like this...
43
43
  g.value_name = :sales
44
44
  end
45
45
 
46
-
47
46
  The `value_name` parameter is only required if you want to access totals;
48
47
  the others are required.
49
48
 
@@ -95,8 +94,6 @@ Then you have the following aspects of the pivot table grid available to you...
95
94
  The API should give you a lot of flexibility with regards to rendering this information in your views.
96
95
  E.g. The rows and columns collections make it very easy to produce horizontal, vertical and overall total values.
97
96
 
98
- Ah, that's better.
99
-
100
97
  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
98
 
102
99
  g.column_totals
@@ -108,6 +105,49 @@ If you want to get the totals for rows, columns, or the entire grid, you can pas
108
105
  g.rows[1].total
109
106
  g.grand_total
110
107
 
108
+ ##### Specifying the pivot field
109
+
110
+ You can also specify the field name which should be used as the pivot. Typically you would use this when you want to pivot on a string field which cannot be aggregated.
111
+
112
+ This option will generate a simplified grid which will contain the specified field value instead of the objects.
113
+
114
+ Consider the following data (similar to above):
115
+
116
+ obj_1 = Order.new(city: 'London', quarter: 'Q1', top_sales: 'Ed')
117
+ obj_2 = Order.new(city: 'London', quarter: 'Q2', top_sales: 'Jim')
118
+ obj_3 = Order.new(city: 'London', quarter: 'Q3', top_sales: 'Sam')
119
+ obj_4 = Order.new(city: 'London', quarter: 'Q4', top_sales: 'Ed')
120
+ obj_5 = Order.new(city: 'New York', quarter: 'Q1', top_sales: 'Tom')
121
+ obj_6 = Order.new(city: 'New York', quarter: 'Q2', top_sales: 'Sandy')
122
+ obj_7 = Order.new(city: 'New York', quarter: 'Q3', top_sales: 'Phil')
123
+ obj_8 = Order.new(city: 'New York', quarter: 'Q4', top_sales: 'Jim')
124
+
125
+ Instantiate a new PivotTable::Grid object, this time specifying the `field_name`:
126
+
127
+ g = PivotTable::Grid.new do |g|
128
+ g.source_data = data
129
+ g.column_name = :quarter
130
+ g.row_name = :city
131
+ g.value_name = :sales
132
+ g.field_name = :top_sales
133
+ end
134
+
135
+ Build the grid...
136
+
137
+ g.build
138
+
139
+ This will give you a logical grid (represented by an two-dimensional array) which can be likened to this table:
140
+
141
+ --------------------------------------------
142
+ | | Q1 | Q2 | Q3 | Q4 |
143
+ |----------|--------------------------------
144
+ | London | Ed | Jim | Sam | Ed |
145
+ | New York | Tom | Sandy | Phil | Jim |
146
+ --------------------------------------------
147
+
148
+ Compare this to the first example above. It's simpler, if that's what you need.
149
+
150
+
111
151
  #### Configuration Options
112
152
 
113
153
  You can also provide additional configuration options when instantiating your Grid. Options are provided as a hash e.g.
@@ -116,7 +156,6 @@ You can also provide additional configuration options when instantiating your Gr
116
156
  g.source_data = data
117
157
  g.column_name = :quarter
118
158
  g.row_name = :city
119
- g.value_name = :sales
120
159
  end
121
160
 
122
161
  Here are the available configuration options:
@@ -129,7 +168,6 @@ Here are the available configuration options:
129
168
 
130
169
  This option will automatically sort your data alphabetically based on your column and row headers. If you disable sorting your original data ordering will be preserved.
131
170
 
132
-
133
171
  ### Ruby Support
134
172
 
135
173
  * 1.9.3
@@ -7,5 +7,5 @@ require 'pivot_table/column'
7
7
  require 'pivot_table/row'
8
8
 
9
9
  module PivotTable
10
- VERSION = '0.3.0'
10
+ VERSION = '0.4.0'
11
11
  end
@@ -1,7 +1,7 @@
1
1
  module PivotTable
2
2
  class Grid
3
3
 
4
- attr_accessor :source_data, :row_name, :column_name, :value_name
4
+ attr_accessor :source_data, :row_name, :column_name, :value_name, :field_name
5
5
  attr_reader :columns, :rows, :data_grid, :configuration
6
6
 
7
7
  DEFAULT_OPTIONS = {
@@ -77,7 +77,9 @@ module PivotTable
77
77
  row_headers.each_with_index do |row, row_index|
78
78
  current_row = []
79
79
  column_headers.each_with_index do |col, col_index|
80
- current_row[col_index] = @source_data.find { |item| item.send(row_name) == row && item.send(column_name) == col }
80
+ object = @source_data.find { |item| item.send(row_name) == row && item.send(column_name) == col }
81
+ has_field_name = field_name && object.respond_to?(field_name)
82
+ current_row[col_index] = has_field_name ? object.send(field_name) : object
81
83
  end
82
84
  @data_grid[row_index] = current_row
83
85
  end
@@ -90,6 +92,5 @@ module PivotTable
90
92
  hdrs = @source_data.collect { |c| c.send method }.uniq
91
93
  configuration.sort ? hdrs.sort : hdrs
92
94
  end
93
-
94
95
  end
95
96
  end
@@ -7,6 +7,7 @@ module PivotTable
7
7
  it { is_expected.to respond_to :source_data }
8
8
  it { is_expected.to respond_to :row_name }
9
9
  it { is_expected.to respond_to :column_name }
10
+ it { is_expected.to respond_to :field_name}
10
11
  it { is_expected.to respond_to :columns }
11
12
  it { is_expected.to respond_to :rows }
12
13
  it { is_expected.to respond_to :grand_total }
@@ -90,5 +91,41 @@ module PivotTable
90
91
  it_behaves_like 'a collection of rows'
91
92
  it_behaves_like 'a data grid'
92
93
  end
94
+
95
+ context 'populating the grid' do
96
+ let(:data) { unsorted_data }
97
+
98
+ context 'field_name is correct attribute' do
99
+ let(:instance) do
100
+ Grid.new do |g|
101
+ g.source_data = data
102
+ g.row_name = :row_name
103
+ g.column_name = :column_name
104
+ g.value_name = :id
105
+ g.field_name = :id
106
+ end
107
+ end
108
+
109
+ let(:build_result) { instance.build }
110
+ subject { build_result.data_grid }
111
+ it { should == [[1, 2, 3], [4, 5, 6]] }
112
+ end
113
+
114
+ context 'field_name is wrong attribute' do
115
+ let(:instance) do
116
+ Grid.new do |g|
117
+ g.source_data = data
118
+ g.row_name = :row_name
119
+ g.column_name = :column_name
120
+ g.value_name = :id
121
+ g.field_name = :wrong_attribute
122
+ end
123
+ end
124
+
125
+ let(:build_result) { instance.build }
126
+ subject { build_result.data_grid }
127
+ it { should == [[d1, d2, d3], [d4, d5, d6]] }
128
+ end
129
+ end
93
130
  end
94
131
  end
metadata CHANGED
@@ -1,68 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pivot_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed James
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-21 00:00:00.000000000 Z
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: growl
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: guard-rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '4.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.3'
55
55
  description: Transform an ActiveRecord-ish data set into a pivot table of objects
56
- email: ed.james.email@gmail.com
56
+ email: !binary |-
57
+ ZWQuamFtZXMuZW1haWxAZ21haWwuY29t
57
58
  executables: []
58
59
  extensions: []
59
60
  extra_rdoc_files: []
60
61
  files:
61
- - ".gitignore"
62
- - ".rspec"
63
- - ".ruby-gemset"
64
- - ".ruby-version"
65
- - ".travis.yml"
62
+ - .gitignore
63
+ - .rspec
64
+ - .ruby-gemset
65
+ - .ruby-version
66
+ - .travis.yml
66
67
  - Gemfile
67
68
  - Guardfile
68
69
  - LICENSE
@@ -95,12 +96,12 @@ require_paths:
95
96
  - lib
96
97
  required_ruby_version: !ruby/object:Gem::Requirement
97
98
  requirements:
98
- - - ">="
99
+ - - ! '>='
99
100
  - !ruby/object:Gem::Version
100
101
  version: '1.9'
101
102
  required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  requirements:
103
- - - ">="
104
+ - - ! '>='
104
105
  - !ruby/object:Gem::Version
105
106
  version: '0'
106
107
  requirements: []
@@ -108,7 +109,7 @@ rubyforge_project: pivot_table
108
109
  rubygems_version: 2.2.2
109
110
  signing_key:
110
111
  specification_version: 4
111
- summary: pivot_table-0.3.0
112
+ summary: pivot_table-0.4.0
112
113
  test_files:
113
114
  - spec/pivot_table/column_spec.rb
114
115
  - spec/pivot_table/configuration_spec.rb