deep_pluck 1.2.1 → 1.3.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: 005fdc992119dbb5eceeb1f62ad7ab4c884af86cf556dacba454eb389f45ec7b
4
- data.tar.gz: bd2e0b8915f71a956238bf2b2a6cd32243c5bf7c18374fd5b0111c20f5550100
3
+ metadata.gz: be904969a21ee38baa9e44b48a57be74b2b8b0a9189732d17a3e226afa75d13f
4
+ data.tar.gz: 92cc2e3650d2173d29ad048a417e46b3bb7cf93e39633a43ed247a4011fc7189
5
5
  SHA512:
6
- metadata.gz: 3fc6109cac1f4afbd414d63aa5b92fda46ec48fde691d5456c8e6d71582e8bd01dbffdf4f1244e2c825a7790f3278113ceafecc6166fcd8aea80fc84e2c66fb7
7
- data.tar.gz: 04c2c14548aaee28873a2167cd88ab86cab91143bc315b717d03446e10357c22247d5db851c8b577aedb3ef6961b5a4a411c9f83eb172b146b3f606ed9d89220
6
+ metadata.gz: 50d4a7a590da8a8800bce18dd4228572b551916b060be591c6dcb1957b1bb676e583c240b07687f00207a74019be09bf81427e84904e39bfdc178cc156cb1bd0
7
+ data.tar.gz: 4aab9a20e9bed08be0537644e07e12844df6059b50dff0d4198609c53e95ae6305506df378aa7c103de2d5dbbb49021dd17f7576e934df2f332b89a8b9c216c2
@@ -1,40 +1,53 @@
1
- module DeepPluck
2
- module DataCombiner
3
- class << self
4
- def combine_data(parent, children, primary_key, column_name, foreign_key, reverse, collection)
5
- source = reverse ? parent : children
6
- target = !reverse ? parent : children
7
- data_hash = make_data_hash(collection, source, primary_key, column_name)
8
- assign_values_to_parent(collection, target, data_hash, column_name, foreign_key, reverse: reverse)
9
- return children
10
- end
11
-
12
- private
13
-
14
- def make_data_hash(collection, parent, primary_key, column_name)
15
- return parent.map{|s| [s[primary_key], s] }.to_h if !collection
16
- hash = {}
17
- parent.each do |model_hash|
18
- key = model_hash[primary_key]
19
- array = (hash[key] ? hash[key][column_name] : []) # share the children if id is duplicated
20
- model_hash[column_name] = array
21
- hash[key] = model_hash
22
- end
23
- return hash
24
- end
25
-
26
- def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
27
- parent.each do |s|
28
- next if (id = s[foreign_key]) == nil
29
- left = reverse ? children_hash[id] : s
30
- right = !reverse ? children_hash[id] : s
31
- if collection
32
- left[column_name] << right
33
- else
34
- left[column_name] = right
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
1
+ # frozen_string_literal: true
2
+
3
+ module DeepPluck
4
+ module DataCombiner
5
+ class << self
6
+ def combine_data(parent, children, primary_key, column_name, foreign_key, reverse, collection)
7
+ source = reverse ? parent : children
8
+ target = !reverse ? parent : children
9
+ data_hash = make_data_hash(collection, source, primary_key, column_name, reverse)
10
+ assign_values_to_parent(collection, target, data_hash, column_name, foreign_key, reverse: reverse)
11
+ return children
12
+ end
13
+
14
+ private
15
+
16
+ def make_data_hash(collection, parent, primary_key, column_name, reverse)
17
+ hash = {}
18
+
19
+ parent.each do |model_hash|
20
+ key = model_hash[primary_key]
21
+
22
+ if reverse
23
+ if collection
24
+ array = (hash[key] ? hash[key][column_name] : []) # share the children if id is duplicated
25
+ model_hash[column_name] = array # set the value of this association key to empty array by default
26
+ else
27
+ model_hash[column_name] = nil # set the value of this association key to be nil by default
28
+ end
29
+ end
30
+
31
+ hash[key] = model_hash
32
+ end
33
+
34
+ return hash
35
+ end
36
+
37
+ def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
38
+ parent.each do |data|
39
+ id = data[foreign_key]
40
+
41
+ left = reverse ? children_hash[id] : data
42
+ right = !reverse ? children_hash[id] : data
43
+
44
+ if collection
45
+ left[column_name] << right
46
+ else
47
+ left[column_name] = right
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_compatibility'
2
4
  require 'rails_compatibility/unscope_where'
3
5
  require 'rails_compatibility/build_joins'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepPluck
2
- VERSION = '1.2.1'
4
+ VERSION = '1.3.0'
3
5
  end
data/lib/deep_pluck.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'deep_pluck/version'
2
4
  require 'deep_pluck/model'
3
5
  require 'active_record'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_pluck
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-06 00:00:00.000000000 Z
11
+ date: 2023-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler