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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 580ffb47923a02141676ab7cc6174e410e993aa49d53e3a7c02f994926433638
4
- data.tar.gz: 8280b0020a220ed46df11bcb8a557f13ef6c8c0be99e319d3ee6716751120dc8
3
+ metadata.gz: 984228026a6a9d085ce9ba14f6ffa03c022019bd54a4dc87cff276a2294af305
4
+ data.tar.gz: ee52cc9ec944dcec33998ced00ee793c618c278975fe6409987f64991f8dc177
5
5
  SHA512:
6
- metadata.gz: 94a17744698e9b22fe9c084983d6c2c8a10c123c160f2389d583791884edf08045fbf3c6d624c65e8c7facf4b34e375ef4faa75c6d6123bb6f0fb4ac708c05a7
7
- data.tar.gz: aa168215bbc6fcd46e3bfc93b5cb74e360bed7e511084d359bb9de624210359fea0f142cda76c65b93805ec3b2a1f258fecea2592957769e09ca33f83548cebc
6
+ metadata.gz: 8d5b2cfb8a93e2c36bb0236718bc755d28040783b0c21d0929dfdc61cc6e0d61b793dc2a60656d0074f3d48924a2629f4b787614e31e66b086b3db28449a0bda
7
+ data.tar.gz: 16acd796cb344c3b8deffdba51f6779572bc53c8cbfb9a835d6b923f83e262b04f111fe8598758a13afd485a087f08e63ea2ac60a8f2e2557056d0d938393b91
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2022-06-25
3
+ ## [0.2.0] - 2022-06-26
4
+
5
+ - Add usage examples in README.
6
+ - Fix CHANGELOG.md url.
7
+
8
+ ## [0.1.0] - 2022-06-26
4
9
 
5
10
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_pivot (0.1.0)
4
+ hash_pivot (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  # HashPivot
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hash_pivot`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashPivot
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.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/hash_pivotCHANGELOG.md
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: []