deep_pluck 0.1.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/deep_pluck.gemspec +1 -1
- data/lib/deep_pluck/data_combiner.rb +37 -0
- data/lib/deep_pluck/model.rb +15 -40
- data/lib/deep_pluck/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 447d5c0f34cda3c5fc8d241abac799236ff6e09b
|
4
|
+
data.tar.gz: c0aed3f2c80dbf2364ece10fee44f3d97c979f08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ee9e958b7a168d182a56695096cc756fcccaa1b6b07bffb4c027a98848b97a539c0a5e39ed84bdb6d09f84327d7723da0495590656853a16e6343e5aba77515
|
7
|
+
data.tar.gz: cf325186ebb8cc23bd989f9302c10b91d2fe1688b728f704e303a7b3d0cb75bc89ac57822fdb12f81408eea0aef44ffee69ac411c80f55ae55db575925899f89
|
data/deep_pluck.gemspec
CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.add_development_dependency "sqlite3", "~> 1.3"
|
33
33
|
spec.add_development_dependency "minitest", "~> 5.0"
|
34
34
|
|
35
|
-
spec.add_dependency "
|
35
|
+
spec.add_dependency "active_record", "> 3.x"
|
36
36
|
spec.add_dependency "pluck_all", "~> 1.2.2"
|
37
37
|
|
38
38
|
end
|
@@ -0,0 +1,37 @@
|
|
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
|
+
private
|
12
|
+
def make_data_hash(collection, parent, primary_key, column_name)
|
13
|
+
return parent.map{|s| [s[primary_key], s]}.to_h if !collection
|
14
|
+
hash = {}
|
15
|
+
parent.each do |model_hash|
|
16
|
+
key = model_hash[primary_key]
|
17
|
+
array = (hash[key] ? hash[key][column_name] : []) #share the children if id is duplicated
|
18
|
+
model_hash[column_name] = array
|
19
|
+
hash[key] = model_hash
|
20
|
+
end
|
21
|
+
return hash
|
22
|
+
end
|
23
|
+
def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
|
24
|
+
parent.each{|s|
|
25
|
+
next if (id = s[foreign_key]) == nil
|
26
|
+
left = reverse ? children_hash[id] : s
|
27
|
+
right = !reverse ? children_hash[id] : s
|
28
|
+
if collection
|
29
|
+
left[column_name] << right
|
30
|
+
else
|
31
|
+
left[column_name] = right
|
32
|
+
end
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/deep_pluck/model.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'deep_pluck/data_combiner'
|
1
2
|
module DeepPluck
|
2
3
|
class Model
|
3
4
|
#---------------------------------------
|
@@ -23,10 +24,10 @@ module DeepPluck
|
|
23
24
|
relation = relation.where(options[:conditions]) if options[:conditions]
|
24
25
|
return relation
|
25
26
|
end
|
26
|
-
def get_join_table(reflect
|
27
|
+
def get_join_table(reflect)
|
27
28
|
return reflect.options[:through] if reflect.options[:through]
|
28
29
|
return (reflect.options[:join_table] || reflect.send(:derive_join_table)) if reflect.macro == :has_and_belongs_to_many
|
29
|
-
return
|
30
|
+
return nil
|
30
31
|
end
|
31
32
|
def get_primary_key(reflect)
|
32
33
|
return (reflect.belongs_to? ? reflect.klass : reflect.active_record).primary_key
|
@@ -35,12 +36,11 @@ module DeepPluck
|
|
35
36
|
if reverse and (table_name = get_join_table(reflect)) #reverse = parent
|
36
37
|
key = reflect.chain.last.foreign_key
|
37
38
|
else
|
38
|
-
|
39
|
-
table_name = reflect.active_record.table_name
|
40
|
-
key = (reflect.belongs_to? ? reflect.foreign_key : get_primary_key(reflect))
|
39
|
+
key = (reflect.belongs_to? == reverse ? get_primary_key(reflect) : reflect.foreign_key)
|
40
|
+
table_name = (reverse ? reflect.klass : reflect.active_record).table_name
|
41
41
|
end
|
42
|
-
return key
|
43
|
-
return
|
42
|
+
return "#{table_name}.#{key}" if with_table_name
|
43
|
+
return key.to_s #key may be symbol if specify foreign_key in association options
|
44
44
|
end
|
45
45
|
#---------------------------------------
|
46
46
|
# Contruction OPs
|
@@ -80,40 +80,15 @@ module DeepPluck
|
|
80
80
|
relation = with_conditions(reflect, relation)
|
81
81
|
return relation.joins(get_join_table(reflect)).where(relation_key => ids)
|
82
82
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
def set_includes_data(parent, column_name, model)
|
84
|
+
reflect = get_reflect(column_name)
|
85
|
+
reverse = !reflect.belongs_to?
|
86
|
+
foreign_key = get_foreign_key(reflect, reverse: reverse)
|
86
87
|
primary_key = get_primary_key(reflect)
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
parent.each{|s|
|
92
|
-
next if (id = s[foreign_key]) == nil
|
93
|
-
s[children_store_name] = children_hash[id]
|
94
|
-
}
|
95
|
-
else #Child.where(:parent_id => parent.pluck(:id))
|
96
|
-
parent_hash = {}
|
97
|
-
parent.each do |model_hash|
|
98
|
-
key = model_hash[primary_key]
|
99
|
-
if reflect.collection?
|
100
|
-
array = (parent_hash[key] ? parent_hash[key][children_store_name] : []) #share the children if id is duplicated
|
101
|
-
model_hash[children_store_name] = array
|
102
|
-
end
|
103
|
-
parent_hash[key] = model_hash
|
104
|
-
end
|
105
|
-
children = model.load_data{|relation| do_query(parent, reflect, relation) }
|
106
|
-
foreign_key = get_foreign_key(reflect, reverse: true)
|
107
|
-
children.each{|s|
|
108
|
-
next if (id = s[foreign_key]) == nil
|
109
|
-
if reflect.collection?
|
110
|
-
parent_hash[id][children_store_name] << s
|
111
|
-
else
|
112
|
-
parent_hash[id][children_store_name] = s
|
113
|
-
end
|
114
|
-
}
|
115
|
-
end
|
116
|
-
return children
|
88
|
+
children = model.load_data{|relation| do_query(parent, reflect, relation) }
|
89
|
+
#reverse = false: Child.where(:id => parent.pluck(:child_id))
|
90
|
+
#reverse = true : Child.where(:parent_id => parent.pluck(:id))
|
91
|
+
return DataCombiner.combine_data(parent, children, primary_key, column_name, foreign_key, reverse, reflect.collection?)
|
117
92
|
end
|
118
93
|
public
|
119
94
|
def load_data
|
data/lib/deep_pluck/version.rb
CHANGED
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: 0.
|
4
|
+
version: 1.0.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: 2017-03-
|
11
|
+
date: 2017-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,19 +67,19 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: active_record
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 3.x
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 3.x
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: pluck_all
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- gemfiles/4.2.gemfile
|
117
117
|
- gemfiles/5.0.gemfile
|
118
118
|
- lib/deep_pluck.rb
|
119
|
+
- lib/deep_pluck/data_combiner.rb
|
119
120
|
- lib/deep_pluck/model.rb
|
120
121
|
- lib/deep_pluck/version.rb
|
121
122
|
homepage: https://github.com/khiav223577/deep_pluck
|
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
139
|
version: '0'
|
139
140
|
requirements: []
|
140
141
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
142
|
+
rubygems_version: 2.6.8
|
142
143
|
signing_key:
|
143
144
|
specification_version: 4
|
144
145
|
summary: Use deep_pluck as a shortcut to select one or more attributes and include
|