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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/deep_cloning.rb +21 -11
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7ae70dc1594781e54e95ed136d33d28e6e9a441d09e71e876042726afa6bcf7
4
- data.tar.gz: 64f20a6d57d21e9f01fc3b6b4f002699e2910b009074e6158745e0c538cc32c1
3
+ metadata.gz: 400b31d36814158862a4a15318f8a4b2cac71548778f7cf6f3bf2238908a5138
4
+ data.tar.gz: 3647ba11e20dbbfa5115800ca8c861453dd69f8fb7d9b1f81acc51c277cbc5e4
5
5
  SHA512:
6
- metadata.gz: 3b9fd95aed554c41cb24b5abd381fe54f8f77e18fae9bcdbf967b448bfbfc24c2bcef81eb57eac2f41a336b6ae3231c62bb99458d2d926c918c7496ca30e5af1
7
- data.tar.gz: b205973f081b28dc6e1293ad59fd46f8f682862b2247444a93a0c144535e3d17570536d058e16157cfbd7726e8f05eaba649a04a4fa8d7f7ea75328d24cfe7e6
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.1.4'.freeze
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
- @opts[:source] << cell if block_given? and not yield(cell, cell, :skip?)
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 |n|
32
- n = yield(n, n, :prepare) if block_given?
33
- walk?(n)
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
- unless @cell.class.name.in?(@opts[:except])
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.in? @opts[:except]
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.in? @opts[:except] # replicated parent?
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
- unless c.class_name.in? @opts[:except]
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
- unless c.class_name.in? @opts[:except]
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.1.4
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-02-18 00:00:00.000000000 Z
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.6
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