hash_pivot 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 984228026a6a9d085ce9ba14f6ffa03c022019bd54a4dc87cff276a2294af305
4
- data.tar.gz: ee52cc9ec944dcec33998ced00ee793c618c278975fe6409987f64991f8dc177
3
+ metadata.gz: 1f2d44b5f76c95c2ad378d60440653827667b9801f5de25495ca8eed2508f68c
4
+ data.tar.gz: ad8545ca39c0209161d6633518ba4e598ed3cceb5a802abc37ac150a12675dda
5
5
  SHA512:
6
- metadata.gz: 8d5b2cfb8a93e2c36bb0236718bc755d28040783b0c21d0929dfdc61cc6e0d61b793dc2a60656d0074f3d48924a2629f4b787614e31e66b086b3db28449a0bda
7
- data.tar.gz: 16acd796cb344c3b8deffdba51f6779572bc53c8cbfb9a835d6b923f83e262b04f111fe8598758a13afd485a087f08e63ea2ac60a8f2e2557056d0d938393b91
6
+ metadata.gz: fa18ee0660da4d17ec5164b3d9bb0adcb19f538e98331a5c039788961e4ea9a28660c59f819b7a6cf8104e2acf67dc59ecc07f181ea244b1dc75eac2d58db0ff
7
+ data.tar.gz: 56b798dd12140e7fa1b77b2bf807a85eaac4f63792224a475b69f9e12b29f08d0d2bd1019cbc990c5e0cf171fad9877a71513abd9c0eb841e915febe9cb754cc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-06-26
4
+
5
+ - Replace Column with label if Hash is given for pivot_kinds.
6
+ - Update README for label replacing.
7
+
3
8
  ## [0.2.0] - 2022-06-26
4
9
 
5
10
  - Add usage examples in README.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_pivot (0.2.0)
4
+ hash_pivot (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -22,6 +22,13 @@ If bundler is not being used to manage dependencies, install the gem by executin
22
22
 
23
23
  Prepare Array of Hash.
24
24
 
25
+ id | role | team | age
26
+ -- | -- | -- | --
27
+ 1 | guest | rabbit | 1
28
+ 2 | guest | mouse | 2
29
+ 3 | guest | rabbit | 3
30
+ 4 | admin | mouse | 4
31
+
25
32
  ```ruby
26
33
  data = [
27
34
  { id: 1, role: 'guest', team: 'rabbit', age: 1 },
@@ -44,6 +51,11 @@ HashPivot.pivot(data, :role, :team, %w[rabbit mouse])
44
51
  # { :role => "admin", "rabbit" => [], "mouse" => [{ :id => 4, :role => "admin", :team => "mouse", :age => 4 }] }]
45
52
  ```
46
53
 
54
+ role | rabbit | mouse
55
+ -- | -- | --
56
+ guest | {1 guest rabbit 1} {3 guest rabbit 3} | {2 guest mouse 2}
57
+ admin |   | {4 admin mouse 4}
58
+
47
59
  Grouping by `:role` and pivot in `:team`.
48
60
 
49
61
  Pivot column is nil. This means that pivot column is automatically configured.
@@ -69,6 +81,30 @@ HashPivot.pivot(data, :role, :team, %w[rabbit mouse]) { |array| array.sum { |h|
69
81
  # [{ :role => "guest", "rabbit" => 4, "mouse" => 2 }, { :role => "admin", "rabbit" => 0, "mouse" => 4 }]
70
82
  ```
71
83
 
84
+ role | rabbit | mouse
85
+ -- | -- | --
86
+ guest | 4 | 2
87
+ admin | 0 | 4
88
+
89
+ #### Pivot with summarize and replacing label.
90
+
91
+ If you give Hash for pivot kinds like this, pivot data is summarized by block and replace with labels.
92
+
93
+ ```ruby
94
+ pivot_kinds = { 'rabbit' => 'RABBIT AGE',
95
+ 'mouse' => 'MOUSE AGE' }
96
+ ```
97
+
98
+ ```ruby
99
+ HashPivot.pivot(data, :role, :team, pivot_kinds) { |array| array.sum { |h| h[:age] } }
100
+
101
+ # [{ :role => "guest", "RABBIT AGE" => 4, "MOUSE AGE" => 2 }, { :role => "admin", "RABBIT AGE" => 0, "MOUSE AGE" => 4 }]
102
+ ```
103
+
104
+ role | RABBIT AGE | MOUSE AGE
105
+ -- | -- | --
106
+ guest | 4 | 2
107
+ admin | 0 | 4
72
108
 
73
109
  ### Pivot Array of Struct
74
110
 
@@ -24,15 +24,44 @@ module HashPivot
24
24
  end
25
25
  end
26
26
 
27
+ private
28
+
29
+ # @param [Array,Hash] pivot_kinds
30
+ # @param [Array] array
31
+ # @param [Object] pivot_in
32
+ # @param [Proc] block
33
+ # @return [Hash]
27
34
  def pivot_with_sum(pivot_kinds, array, pivot_in, &block)
28
35
  pivot_kinds ||= array.map { |h| h[pivot_in] }.uniq.compact
36
+ case pivot_kinds
37
+ when Array
38
+ pivot_with_sum_with_array_pivot_kinds(pivot_kinds, array, pivot_in, &block)
39
+ when Hash
40
+ pivot_with_sum_with_hash_pivot_kinds(pivot_kinds, array, pivot_in, &block)
41
+ end
42
+ end
43
+
44
+ # @param [Array,Hash] pivot_kinds
45
+ # @param [Array] array
46
+ # @param [Object] pivot_in
47
+ # @param [Proc] block
48
+ # @return [Hash]
49
+ def pivot_with_sum_with_array_pivot_kinds(pivot_kinds, array, pivot_in, &block)
29
50
  pivot_kinds.each_with_object({}) do |pivot_kind, memo|
30
51
  pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
31
- memo[pivot_kind] = if block
32
- yield(pivoted_data)
33
- else
34
- pivoted_data
35
- end
52
+ memo[pivot_kind] = block ? yield(pivoted_data) : pivoted_data
53
+ end
54
+ end
55
+
56
+ # @param [Array,Hash] pivot_kinds
57
+ # @param [Array] array
58
+ # @param [Object] pivot_in
59
+ # @param [Proc] block
60
+ # @return [Hash]
61
+ def pivot_with_sum_with_hash_pivot_kinds(pivot_kinds, array, pivot_in, &block)
62
+ pivot_kinds.each_with_object({}) do |(pivot_kind, pivot_label), memo|
63
+ pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
64
+ memo[pivot_label] = block ? yield(pivoted_data) : pivoted_data
36
65
  end
37
66
  end
38
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashPivot
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_pivot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - junara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-26 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord