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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f2d44b5f76c95c2ad378d60440653827667b9801f5de25495ca8eed2508f68c
4
- data.tar.gz: ad8545ca39c0209161d6633518ba4e598ed3cceb5a802abc37ac150a12675dda
3
+ metadata.gz: 871d8c1d4c6a26913eb825057ee2823b54e9919c5a8faceadc008064fd239243
4
+ data.tar.gz: 4a5c03ee17d1ab10a11873f5b56419de12fadeaa6a34695e8eb1d3fd48f0d93e
5
5
  SHA512:
6
- metadata.gz: fa18ee0660da4d17ec5164b3d9bb0adcb19f538e98331a5c039788961e4ea9a28660c59f819b7a6cf8104e2acf67dc59ecc07f181ea244b1dc75eac2d58db0ff
7
- data.tar.gz: 56b798dd12140e7fa1b77b2bf807a85eaac4f63792224a475b69f9e12b29f08d0d2bd1019cbc990c5e0cf171fad9877a71513abd9c0eb841e915febe9cb754cc
6
+ metadata.gz: b4c7fcd39a18b07f16c8b7facaeb42fdb76969d42879a0c663789458f007692233c9cdb4d4cb6d685d17877b8688e2f17f431c263c8578e1aad67f9b18d56f57
7
+ data.tar.gz: 9fa1e4cb4becd504a2832dcdf5d221709340a23e39ea9c35d917f797949b1768833675fc8e8874b848bce572f0fd84377d4699de6fdd7829e3153413c216421f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.1] - 2022-06-28
4
+
5
+ - Refactoring code
6
+ - Add document url
7
+
3
8
  ## [0.3.0] - 2022-06-26
4
9
 
5
10
  - Replace Column with label if Hash is given for pivot_kinds.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_pivot (0.3.0)
4
+ hash_pivot (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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.
@@ -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 [Array] pivot_kinds
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 = pivot_with_sum(pivot_kinds, array, pivot_in, &block)
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 [Array,Hash] pivot_kinds
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 pivot_with_sum(pivot_kinds, array, pivot_in, &block)
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)
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[pivot_kind] = block ? yield(pivoted_data) : pivoted_data
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 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
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashPivot
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
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] pivot_kinds
12
+ # @param [Array,Hash,nil] pivot_header
11
13
  # @return [Hash]
12
- def self.pivot(data, group, pivot_in, pivot_kinds, repository: HashPivot::Repository::HashRepository, &block)
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.0
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-27 00:00:00.000000000 Z
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: []