hash_pivot 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/hash_pivot.gemspec +1 -0
- data/lib/hash_pivot/table.rb +16 -28
- data/lib/hash_pivot/version.rb +1 -1
- data/lib/hash_pivot.rb +18 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 871d8c1d4c6a26913eb825057ee2823b54e9919c5a8faceadc008064fd239243
|
4
|
+
data.tar.gz: 4a5c03ee17d1ab10a11873f5b56419de12fadeaa6a34695e8eb1d3fd48f0d93e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4c7fcd39a18b07f16c8b7facaeb42fdb76969d42879a0c663789458f007692233c9cdb4d4cb6d685d17877b8688e2f17f431c263c8578e1aad67f9b18d56f57
|
7
|
+
data.tar.gz: 9fa1e4cb4becd504a2832dcdf5d221709340a23e39ea9c35d917f797949b1768833675fc8e8874b848bce572f0fd84377d4699de6fdd7829e3153413c216421f
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/hash_pivot.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
18
|
spec.metadata['source_code_uri'] = spec.homepage
|
19
19
|
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
20
|
+
spec.metadata['documentation_uri'] = 'https://rubydoc.info/gems/hash_pivot'
|
20
21
|
|
21
22
|
# Specify which files should be added to the gem when it is released.
|
22
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/hash_pivot/table.rb
CHANGED
@@ -6,19 +6,26 @@ require_relative './repository/struct_repository'
|
|
6
6
|
|
7
7
|
module HashPivot
|
8
8
|
class Table
|
9
|
+
# rubocop:disable Layout/LineLength
|
10
|
+
|
9
11
|
# @param [Array<Hash>] data
|
12
|
+
# @param [Class<HashPivot::Repository::HashRepository,HashPivot::Repository::StructRepository,HashPivot::Repository::ActiveRecordRepository>] repository
|
10
13
|
def initialize(data, repository: HashPivot::Repository::HashRepository)
|
11
14
|
@data = data
|
12
15
|
@repository = repository
|
13
16
|
end
|
14
17
|
|
18
|
+
# rubocop:enable Layout/LineLength
|
19
|
+
|
15
20
|
# @param [Array, Object] group
|
16
21
|
# @param [Object] pivot_in
|
17
|
-
# @param [
|
22
|
+
# @param [Hash,nil] pivot_kinds
|
23
|
+
# @param [Proc] block
|
24
|
+
# @return [Array<Hash>]
|
18
25
|
def pivot(group, pivot_in, pivot_kinds, &block)
|
19
26
|
group = [group] unless group.is_a?(Array)
|
20
27
|
@repository.hash_by_group(@data, group, pivot_in).each_with_object([]) do |(key, array), memo|
|
21
|
-
hash =
|
28
|
+
hash = transpose_with(pivot_kinds, array, pivot_in, &block)
|
22
29
|
|
23
30
|
memo << key.merge(hash)
|
24
31
|
end
|
@@ -26,43 +33,24 @@ module HashPivot
|
|
26
33
|
|
27
34
|
private
|
28
35
|
|
29
|
-
# @param [
|
36
|
+
# @param [Hash,nil] pivot_kinds
|
30
37
|
# @param [Array] array
|
31
38
|
# @param [Object] pivot_in
|
32
39
|
# @param [Proc] block
|
33
40
|
# @return [Hash]
|
34
|
-
def
|
35
|
-
pivot_kinds ||= array
|
36
|
-
|
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)
|
50
|
-
pivot_kinds.each_with_object({}) do |pivot_kind, memo|
|
41
|
+
def transpose_with(pivot_kinds, array, pivot_in, &block)
|
42
|
+
pivot_kinds ||= calculated_pivot_kinds_from(array, pivot_in)
|
43
|
+
pivot_kinds.each_with_object({}) do |(pivot_kind, pivot_label), memo|
|
51
44
|
pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
|
52
|
-
memo[
|
45
|
+
memo[pivot_label] = block ? yield(pivoted_data) : pivoted_data
|
53
46
|
end
|
54
47
|
end
|
55
48
|
|
56
|
-
# @param [Array,Hash] pivot_kinds
|
57
49
|
# @param [Array] array
|
58
50
|
# @param [Object] pivot_in
|
59
|
-
# @param [Proc] block
|
60
51
|
# @return [Hash]
|
61
|
-
def
|
62
|
-
|
63
|
-
pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
|
64
|
-
memo[pivot_label] = block ? yield(pivoted_data) : pivoted_data
|
65
|
-
end
|
52
|
+
def calculated_pivot_kinds_from(array, pivot_in)
|
53
|
+
array.map { |h| h[pivot_in] }.uniq.compact.each_with_object({}) { |kind, memo| memo[kind] = kind }
|
66
54
|
end
|
67
55
|
end
|
68
56
|
end
|
data/lib/hash_pivot/version.rb
CHANGED
data/lib/hash_pivot.rb
CHANGED
@@ -4,12 +4,28 @@ require_relative 'hash_pivot/version'
|
|
4
4
|
require_relative 'hash_pivot/table'
|
5
5
|
require 'hash_pivot/error/not_implemented_error'
|
6
6
|
module HashPivot
|
7
|
+
# rubocop:disable Layout/LineLength
|
8
|
+
|
7
9
|
# @param [Array<Hash>] data
|
8
10
|
# @param [Array] group
|
9
11
|
# @param [Object] pivot_in
|
10
|
-
# @param [Array]
|
12
|
+
# @param [Array,Hash,nil] pivot_header
|
11
13
|
# @return [Hash]
|
12
|
-
|
14
|
+
# @param [Class<HashPivot::Repository::HashRepository,HashPivot::Repository::StructRepository,HashPivot::Repository::ActiveRecordRepository>] repository
|
15
|
+
# @param [Proc] block
|
16
|
+
def self.pivot(data, group, pivot_in, pivot_header, repository: HashPivot::Repository::HashRepository, &block)
|
17
|
+
pivot_kinds = if pivot_header.is_a?(Array)
|
18
|
+
transform_to_hash(pivot_header)
|
19
|
+
else
|
20
|
+
pivot_header
|
21
|
+
end
|
13
22
|
Table.new(data, repository: repository).pivot(group, pivot_in, pivot_kinds, &block)
|
14
23
|
end
|
24
|
+
|
25
|
+
# rubocop:enable Layout/LineLength
|
26
|
+
|
27
|
+
def self.transform_to_hash(array)
|
28
|
+
array.each_with_object({}) { |kind, memo| memo[kind] = kind }
|
29
|
+
end
|
30
|
+
private_class_method :transform_to_hash
|
15
31
|
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.3.
|
4
|
+
version: 0.3.1
|
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-
|
11
|
+
date: 2022-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -183,6 +183,7 @@ metadata:
|
|
183
183
|
homepage_uri: https://github.com/junara/hash_pivot
|
184
184
|
source_code_uri: https://github.com/junara/hash_pivot
|
185
185
|
changelog_uri: https://github.com/junara/hash_pivot/blob/main/CHANGELOG.md
|
186
|
+
documentation_uri: https://rubydoc.info/gems/hash_pivot
|
186
187
|
rubygems_mfa_required: 'true'
|
187
188
|
post_install_message:
|
188
189
|
rdoc_options: []
|