deep-cloning 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/deep_cloning.rb +21 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 400b31d36814158862a4a15318f8a4b2cac71548778f7cf6f3bf2238908a5138
|
4
|
+
data.tar.gz: 3647ba11e20dbbfa5115800ca8c861453dd69f8fb7d9b1f81acc51c277cbc5e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12434df4846e5e403d5d2a23e7ba2a5885b838b6e75de10bf76116f48669be096cb7f8c188a56bd33ba4507940cfae4cc79562e1e33018f33f7ad8c2c734106d
|
7
|
+
data.tar.gz: 7918d04077d789fd6369c6d5ab7d542a7802515c9aca455991db80b537246fa1db4d4ee5478e629c3fb088e29500fdaab0951eb821253380a21e4939f3b6996c
|
data/lib/deep_cloning.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_record'
|
|
3
3
|
module DeepCloning
|
4
4
|
# This is the main class responsible to evaluate the equations
|
5
5
|
class Clone
|
6
|
-
VERSION = '0.
|
6
|
+
VERSION = '0.2.0'.freeze
|
7
7
|
def initialize(root, opts = { except: [], save_root: true })
|
8
8
|
@root = root
|
9
9
|
@opts = opts
|
@@ -24,13 +24,13 @@ module DeepCloning
|
|
24
24
|
@opts[clone.class.name] = { @root.id => clone }
|
25
25
|
end
|
26
26
|
leafs(@root).each do |cell|
|
27
|
-
|
27
|
+
@opts[:source] << cell if block_given? and not skip?(yield(cell, cell, :skip?))
|
28
28
|
end
|
29
29
|
|
30
30
|
while @opts[:source].any?
|
31
|
-
@cell = @opts[:source].detect do |
|
32
|
-
|
33
|
-
walk?(
|
31
|
+
@cell = @opts[:source].detect do |node|
|
32
|
+
node = yield(node, node, :prepare) if block_given?
|
33
|
+
walk?(node)
|
34
34
|
end
|
35
35
|
unless @cell
|
36
36
|
ap @opts[:source].map { |s| "#{s.id} - #{s.class.name}" }
|
@@ -41,7 +41,7 @@ module DeepCloning
|
|
41
41
|
@opts[@cell.class.name] = {} unless @opts[@cell.class.name]
|
42
42
|
next if @opts[@cell.class.name][@cell.id] # already cloned?
|
43
43
|
|
44
|
-
|
44
|
+
if should_copy?(@cell.class.name)
|
45
45
|
clone = @cell.dup
|
46
46
|
parents(clone.class).each do |belongs_to|
|
47
47
|
old_id = clone.send("#{belongs_to.name}_id")
|
@@ -65,7 +65,7 @@ module DeepCloning
|
|
65
65
|
@opts[clone.class.name][@cell.id] = clone
|
66
66
|
end
|
67
67
|
leafs(@cell).each do |cell|
|
68
|
-
@opts[:source] << cell if block_given? and not yield(cell, cell, :skip?)
|
68
|
+
@opts[:source] << cell if block_given? and not skip?(yield(cell, cell, :skip?))
|
69
69
|
end
|
70
70
|
leafs_statuses["#{@cell.class.name}_#{@cell.id}"] = true
|
71
71
|
end
|
@@ -76,7 +76,7 @@ module DeepCloning
|
|
76
76
|
!child.respond_to?("#{parent.name}_id".to_sym) or
|
77
77
|
child.send("#{parent.name}_id").nil? or
|
78
78
|
@opts[parent.class_name][child.send("#{parent.name}_id")] or
|
79
|
-
parent.class_name
|
79
|
+
not should_copy?(parent.class_name)
|
80
80
|
# replicated parent?
|
81
81
|
end
|
82
82
|
|
@@ -87,7 +87,7 @@ module DeepCloning
|
|
87
87
|
if cell.respond_to?("#{p.name}_id".to_sym) and cell.send("#{p.name}_id")
|
88
88
|
class_name = cell.send("#{p.name}").class.name
|
89
89
|
@opts[class_name] = {} unless @opts[class_name]
|
90
|
-
@opts[class_name][cell.send("#{p.name}_id")] or class_name
|
90
|
+
@opts[class_name][cell.send("#{p.name}_id")] or not should_copy?(class_name) # replicated parent?
|
91
91
|
else
|
92
92
|
true
|
93
93
|
end
|
@@ -104,13 +104,13 @@ module DeepCloning
|
|
104
104
|
node = cell.class
|
105
105
|
arr = []
|
106
106
|
node.reflect_on_all_associations(:has_one).each do |c|
|
107
|
-
|
107
|
+
if should_copy?(c.class_name)
|
108
108
|
leaf = cell.send(c.name)
|
109
109
|
arr << leaf if leaf&.persisted? and (@opts[:source].nil? or (not leaf.in? @opts[:source]))
|
110
110
|
end
|
111
111
|
end
|
112
112
|
node.reflect_on_all_associations(:has_many).each do |c|
|
113
|
-
|
113
|
+
if should_copy?(c.class_name)
|
114
114
|
cell.send(c.name).find_in_batches.each do |leafs|
|
115
115
|
leafs.each do |leaf|
|
116
116
|
arr << leaf if leaf&.persisted? and (@opts[:source].nil? or (not leaf.in? @opts[:source]))
|
@@ -124,5 +124,15 @@ module DeepCloning
|
|
124
124
|
def parents(node)
|
125
125
|
parents = node.reflect_on_all_associations(:belongs_to) # name and class_name
|
126
126
|
end
|
127
|
+
|
128
|
+
def should_copy?(klass)
|
129
|
+
return !(klass.in? @opts[:except]) if @opts[:including].nil?
|
130
|
+
klass.in? @opts[:including]
|
131
|
+
end
|
132
|
+
|
133
|
+
def skip?(skip)
|
134
|
+
return skip if skip.in? [true, false]
|
135
|
+
false # If the skip? moment is not passed, its set to false.
|
136
|
+
end
|
127
137
|
end
|
128
138
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep-cloning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nilton Vasques
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-
|
13
|
+
date: 2019-04-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: minitest
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
87
|
rubyforge_project:
|
88
|
-
rubygems_version: 2.7.
|
88
|
+
rubygems_version: 2.7.7
|
89
89
|
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: DeepCloning is a gem that is able to replicate a set of records in depth
|