group_by_summary 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/group_by_summary.gemspec +32 -0
- data/lib/array_named_access.rb +11 -0
- data/lib/group_by_summary.rb +70 -0
- data/lib/group_by_summary/version.rb +3 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d88a626a72ccd137f934c59f4b2fd1c84d4474fe
|
4
|
+
data.tar.gz: 22f244aebe0881404f48f0e3e215f8ed09d248f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f3464d2d35df6b79c3c27ae2abb7ca9dc1cf9aeee90ac4827ecd168672262874825cdeeda54e4679d0d3314c93e232930d0c8f52000ab6268b5a7c8a3da46ff
|
7
|
+
data.tar.gz: 7ec45c60d9504cdc208930e8303f2af199f72bcac1244152269ba0881b00fadb3374271445b42a1fe9065e9524f1b16c251cc4e8a0bd36584264bf4d3b2b79fa
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 jimjames99
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# GroupBySummary
|
2
|
+
![Travis badge](https://travis-ci.org/jimjames99/group_by_summary.svg?branch=master)
|
3
|
+
|
4
|
+
You run a chain of stores that carry different kinds of fruit. Which stores carry which fruit?
|
5
|
+
The query for store name, fruit, and quantity in stock returns many rows for each store:
|
6
|
+
|
7
|
+
```StoreInventory.group(:name, :fruit).pluck(:name, :fruit, 'sum(quantity)')```
|
8
|
+
|
9
|
+
```
|
10
|
+
[
|
11
|
+
['Eastside', 'banana', 44],
|
12
|
+
['Eastside', 'apple', 22],
|
13
|
+
['Westside', 'banana', 33],
|
14
|
+
['Westside', 'orange', 44],
|
15
|
+
['Northside', 'tomato', 55],
|
16
|
+
with many rows for each store
|
17
|
+
]
|
18
|
+
```
|
19
|
+
|
20
|
+
You want to consolidate those rows into pretty summary table with only one row per store like this (output by [TerminalTable](https://github.com/tj/terminal-table)):
|
21
|
+
|
22
|
+
```
|
23
|
+
| | apple | banana | orange | tomato |
|
24
|
+
|-----------|------:|-------:|-------:|-------:|
|
25
|
+
| Eastside | 22 | 44 | | |
|
26
|
+
| Westside | | 33 | 44 | |
|
27
|
+
| Northside | | | | 55 |
|
28
|
+
```
|
29
|
+
|
30
|
+
Or you want to tab-separate the row values so you can paste it into an Excel spreadsheet.
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
Add this line to your application's Gemfile:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'group_by_summary'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install group_by_summary
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Create a summary object by passing an array of arrays. The output of this arel is perfect:
|
51
|
+
```StoreInventory.group(:name, :fruit).pluck(:name, :fruit, 'sum(quantity)')```
|
52
|
+
Each array containing 2 or 3 elements:
|
53
|
+
* entity name
|
54
|
+
* attribute for column heading
|
55
|
+
* value to be displayed in the cell (optional)
|
56
|
+
|
57
|
+
```
|
58
|
+
require 'group_by_summary'
|
59
|
+
raw_rows =
|
60
|
+
[
|
61
|
+
['Eastside', 'banana', 44],
|
62
|
+
['Eastside', 'apple', 22],
|
63
|
+
['Westside', 'banana', 33],
|
64
|
+
['Westside', 'orange', 44],
|
65
|
+
['Northside', 'tomato', 55]
|
66
|
+
]
|
67
|
+
summary = GroupBySummary.new(raw_rows)
|
68
|
+
```
|
69
|
+
|
70
|
+
Column headings for the table are extracted from the second element in the array
|
71
|
+
```
|
72
|
+
summary.heading
|
73
|
+
=> [nil, 'apple', 'banana', 'orange', 'tomato']
|
74
|
+
```
|
75
|
+
If you want to give a column heading for the entity column (first col), give it a name:
|
76
|
+
```
|
77
|
+
summary.heading('Store')
|
78
|
+
=> ['Store', 'apple', 'banana', 'orange', 'tomato']
|
79
|
+
```
|
80
|
+
Install the [TerminalTable gem](https://github.com/tj/terminal-table) to display a nice table:
|
81
|
+
```
|
82
|
+
require 'terminal-table'
|
83
|
+
table = Terminal::Table.new(rows: summary.rows)
|
84
|
+
table.headings = summary.heading
|
85
|
+
puts table
|
86
|
+
=>
|
87
|
+
+-----------+-------+--------+--------+--------+
|
88
|
+
| | apple | banana | orange | tomato |
|
89
|
+
+-----------+-------+--------+--------+--------+
|
90
|
+
| Eastside | 22 | 44 | | |
|
91
|
+
| Westside | | 33 | 44 | |
|
92
|
+
| Northside | | | | 55 |
|
93
|
+
+-----------+-------+--------+--------+--------+
|
94
|
+
```
|
95
|
+
Or output to console with tab separators so you can paste into an Excel spreadsheet. If values are not present an x is printed.
|
96
|
+
```
|
97
|
+
puts summary.to_s(:tab)
|
98
|
+
=>
|
99
|
+
apple banana orange tomato
|
100
|
+
Eastside x x
|
101
|
+
Westside x x
|
102
|
+
Northside x
|
103
|
+
```
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'group_by_summary'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'group_by_summary/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'group_by_summary'
|
8
|
+
spec.version = GroupBySummary::VERSION
|
9
|
+
spec.authors = ['jimjames99']
|
10
|
+
spec.email = ['jim@jimjames.org']
|
11
|
+
|
12
|
+
spec.summary = %q{Takes array produced from StoreInventory.group(:name, :fruit).pluck(:name, :fruit, 'sum(quantity)') and produces an array of arrays suitable for display in a table.}
|
13
|
+
spec.homepage = 'https://github.com/jimjames99/group_by_summary'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
else
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '>= 1.7.6', '~> 1.7'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'minitest', '~> 5'
|
32
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'group_by_summary/version'
|
2
|
+
require 'array_named_access'
|
3
|
+
|
4
|
+
class GroupBySummary
|
5
|
+
|
6
|
+
attr_reader :list_of_items
|
7
|
+
|
8
|
+
# Imagine you run a chain of stores that sell different kinds of fruit.
|
9
|
+
# Query for store name and kind of fruit in stock:
|
10
|
+
# StoreInventory.group(:name, :fruit).pluck(:name, :fruit, 'sum(quantity)')
|
11
|
+
# [
|
12
|
+
# ['Eastside', 'banana', 44],
|
13
|
+
# ['Eastside', 'apple', 22],
|
14
|
+
# ['Westside', 'banana', 33],
|
15
|
+
# ['Westside', 'orange', 44],
|
16
|
+
# ['Northside', 'tomato', 55],
|
17
|
+
# ]
|
18
|
+
#
|
19
|
+
# Order matters here: entity, attributes, value (optional)
|
20
|
+
# Also must be ordered by entity.
|
21
|
+
#
|
22
|
+
def initialize(arr=[])
|
23
|
+
@list_of_items = Array.new(arr).extend(ArrayNamedAccess)
|
24
|
+
@list_of_items.each { |a| a.extend(ArrayNamedAccess) }
|
25
|
+
end
|
26
|
+
|
27
|
+
# ['apple', 'banana', 'orange', 'tomato']
|
28
|
+
def column_names
|
29
|
+
@column_names ||= @list_of_items.collect(&:second).uniq.sort
|
30
|
+
end
|
31
|
+
|
32
|
+
# ['Store', 'apple', 'banana', 'orange', 'tomato']
|
33
|
+
def heading(entity_name=nil)
|
34
|
+
[entity_name] + column_names
|
35
|
+
end
|
36
|
+
|
37
|
+
def heading_with_tabs(entity_name=nil)
|
38
|
+
heading(entity_name).join("\t")
|
39
|
+
end
|
40
|
+
|
41
|
+
def rows
|
42
|
+
rows = []
|
43
|
+
@list_of_items.group_by(&:first).each do |store|
|
44
|
+
store.extend(ArrayNamedAccess)
|
45
|
+
store_row = [nil] * heading.size
|
46
|
+
# Add the entity name to the start of the row.
|
47
|
+
store_row[0] = store.first
|
48
|
+
# Fill in values for each column (remember entity name is already first position).
|
49
|
+
store.second.each do |tuple|
|
50
|
+
store_row[@column_names.index(tuple.second) + 1] = (tuple.third || 'x')
|
51
|
+
end
|
52
|
+
rows << store_row
|
53
|
+
end
|
54
|
+
rows
|
55
|
+
end
|
56
|
+
|
57
|
+
# Ruby's join is recursive and we want to keep the top level arrays.
|
58
|
+
def rows_with_tabs
|
59
|
+
rows.collect { |i| i.join("\t") }
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s(opt=nil)
|
63
|
+
if opt == :tab
|
64
|
+
[heading_with_tabs] + rows_with_tabs
|
65
|
+
else
|
66
|
+
[heading] + rows
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: group_by_summary
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jimjames99
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.7.6
|
20
|
+
- - "~>"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.7'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.7.6
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '5'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '5'
|
61
|
+
description:
|
62
|
+
email:
|
63
|
+
- jim@jimjames.org
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- ".travis.yml"
|
70
|
+
- CODE_OF_CONDUCT.md
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/console
|
76
|
+
- bin/setup
|
77
|
+
- group_by_summary.gemspec
|
78
|
+
- lib/array_named_access.rb
|
79
|
+
- lib/group_by_summary.rb
|
80
|
+
- lib/group_by_summary/version.rb
|
81
|
+
homepage: https://github.com/jimjames99/group_by_summary
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata:
|
85
|
+
allowed_push_host: https://rubygems.org
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.4.7
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Takes array produced from StoreInventory.group(:name, :fruit).pluck(:name,
|
106
|
+
:fruit, 'sum(quantity)') and produces an array of arrays suitable for display in
|
107
|
+
a table.
|
108
|
+
test_files: []
|