hash_pivot 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +1 -1
- data/README.md +124 -4
- data/hash_pivot.gemspec +1 -1
- data/lib/hash_pivot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 984228026a6a9d085ce9ba14f6ffa03c022019bd54a4dc87cff276a2294af305
|
4
|
+
data.tar.gz: ee52cc9ec944dcec33998ced00ee793c618c278975fe6409987f64991f8dc177
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d5b2cfb8a93e2c36bb0236718bc755d28040783b0c21d0929dfdc61cc6e0d61b793dc2a60656d0074f3d48924a2629f4b787614e31e66b086b3db28449a0bda
|
7
|
+
data.tar.gz: 16acd796cb344c3b8deffdba51f6779572bc53c8cbfb9a835d6b923f83e262b04f111fe8598758a13afd485a087f08e63ea2ac60a8f2e2557056d0d938393b91
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
# HashPivot
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
TODO: Delete this and the text above, and describe your gem
|
5
|
+
Pivot Array of Hash or Array of Struct or ActiveRecord::Relation.
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
@@ -18,7 +16,129 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
18
16
|
|
19
17
|
## Usage
|
20
18
|
|
21
|
-
|
19
|
+
### Pivot Array of Hash
|
20
|
+
|
21
|
+
#### Prepare data
|
22
|
+
|
23
|
+
Prepare Array of Hash.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
data = [
|
27
|
+
{ id: 1, role: 'guest', team: 'rabbit', age: 1 },
|
28
|
+
{ id: 2, role: 'guest', team: 'mouse', age: 2 },
|
29
|
+
{ id: 3, role: 'guest', team: 'rabbit', age: 3 },
|
30
|
+
{ id: 4, role: 'admin', team: 'mouse', age: 4 }
|
31
|
+
]
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Basic usage
|
35
|
+
|
36
|
+
Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` .
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
HashPivot.pivot(data, :role, :team, %w[rabbit mouse])
|
40
|
+
|
41
|
+
# [{ :role => "guest",
|
42
|
+
# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }],
|
43
|
+
# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] },
|
44
|
+
# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }]
|
45
|
+
```
|
46
|
+
|
47
|
+
Grouping by `:role` and pivot in `:team`.
|
48
|
+
|
49
|
+
Pivot column is nil. This means that pivot column is automatically configured.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
HashPivot.pivot(data, :role, :team, nil)
|
53
|
+
|
54
|
+
# [{ :role => "guest",
|
55
|
+
# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }],
|
56
|
+
# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] },
|
57
|
+
# { :role => "admin", "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }]
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Pivot with summarize.
|
61
|
+
|
62
|
+
Pivot data is summarized by block.
|
63
|
+
|
64
|
+
Age is summarized by block.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
HashPivot.pivot(data, :role, :team, %w[rabbit mouse]) { |array| array.sum { |h| h[:age] } }
|
68
|
+
|
69
|
+
# [{ :role => "guest", "rabbit" => 4, "mouse" => 2 }, { :role => "admin", "rabbit" => 0, "mouse" => 4 }]
|
70
|
+
```
|
71
|
+
|
72
|
+
|
73
|
+
### Pivot Array of Struct
|
74
|
+
|
75
|
+
#### Prepare data
|
76
|
+
|
77
|
+
Prepare Array of Struct.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
HashPivotUserStruct = Struct.new(:id, :role, :team, :age, keyword_init: true)
|
81
|
+
data = [
|
82
|
+
HashPivotUserStruct.new(id: 1, role: 'guest', team: 'rabbit', age: 1),
|
83
|
+
HashPivotUserStruct.new(id: 2, role: 'guest', team: 'mouse', age: 2),
|
84
|
+
HashPivotUserStruct.new({ id: 3, role: 'guest', team: 'rabbit', age: 3 }),
|
85
|
+
HashPivotUserStruct.new({ id: 4, role: 'admin', team: 'mouse', age: 4 })
|
86
|
+
]
|
87
|
+
```
|
88
|
+
|
89
|
+
#### Basic usage
|
90
|
+
|
91
|
+
Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` .
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
HashPivot.pivot(data, :role, :team, %w[rabbit mouse], repository: HashPivot::Repository::StructRepository)
|
95
|
+
|
96
|
+
# [{ :role => "guest",
|
97
|
+
# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }],
|
98
|
+
# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] },
|
99
|
+
# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }]
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
### Pivot Array of ActiveRecord::Relation
|
104
|
+
|
105
|
+
#### Prepare data
|
106
|
+
|
107
|
+
Prepare Array of ActiveRecord::Relation.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class MigrateSqlDatabase < ActiveRecord::Migration[6.1]
|
111
|
+
def self.up
|
112
|
+
create_table(:hash_pivot_users) do |t|
|
113
|
+
t.string :role
|
114
|
+
t.string :team
|
115
|
+
t.integer :age
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
HashPivotUser.destroy_all
|
123
|
+
HashPivotUser.create(:hash_pivot_user, id: 1, role: 'guest', team: 'rabbit', age: 1)
|
124
|
+
HashPivotUser.create(:hash_pivot_user, id: 2, role: 'guest', team: 'mouse', age: 2)
|
125
|
+
HashPivotUser.create(:hash_pivot_user, id: 3, role: 'guest', team: 'rabbit', age: 3)
|
126
|
+
HashPivotUser.create(:hash_pivot_user, id: 4, role: 'admin', team: 'mouse', age: 4)
|
127
|
+
```
|
128
|
+
|
129
|
+
#### Basic usage
|
130
|
+
|
131
|
+
Grouping by `:role` and pivot in `:team`. Pivot column is `rabbit or mouse` .
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
HashPivot.pivot(data, :role, :team, %w[rabbit mouse], repository: HashPivot::Repository::ActiveRecordRepository)
|
135
|
+
|
136
|
+
# [{ :role => "guest",
|
137
|
+
# "rabbit" => [{ :id => 1, :role => "guest", :team => "rabbit", :age => 1 }, { :id => 3, :role => "guest", :team => "rabbit", :age => 3 }],
|
138
|
+
# "mouse" => [{ :id => 2, :role => "guest", :team => "mouse", :age => 2 }] },
|
139
|
+
# { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }]
|
140
|
+
```
|
141
|
+
|
22
142
|
|
23
143
|
## Development
|
24
144
|
|
data/hash_pivot.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
18
|
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
-
spec.metadata['changelog_uri'] = "#{spec.homepage}CHANGELOG.md"
|
19
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/hash_pivot/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_pivot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- junara
|
@@ -182,7 +182,7 @@ licenses:
|
|
182
182
|
metadata:
|
183
183
|
homepage_uri: https://github.com/junara/hash_pivot
|
184
184
|
source_code_uri: https://github.com/junara/hash_pivot
|
185
|
-
changelog_uri: https://github.com/junara/
|
185
|
+
changelog_uri: https://github.com/junara/hash_pivot/blob/main/CHANGELOG.md
|
186
186
|
rubygems_mfa_required: 'true'
|
187
187
|
post_install_message:
|
188
188
|
rdoc_options: []
|