deep-cloning 0.1.3 → 0.1.4

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 +16 -5
  3. metadata +6 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8be606e88796177c306fc3363c7d5f4deec819e51ff807841dc35b0de9e7aee7
4
- data.tar.gz: 2b3810d7c27cb615cffd85dfce75a9ecabcf4724f9b24d956e08497aa106b0c6
3
+ metadata.gz: a7ae70dc1594781e54e95ed136d33d28e6e9a441d09e71e876042726afa6bcf7
4
+ data.tar.gz: 64f20a6d57d21e9f01fc3b6b4f002699e2910b009074e6158745e0c538cc32c1
5
5
  SHA512:
6
- metadata.gz: ea68292929342892c413452f369aedf9336e5892180ba3e5e4c964f9f450d2b78b4899c6b053129f86b8952b12c2a00161aeb2b425dd8b541a191baa0a1de72f
7
- data.tar.gz: 607ddbf9fab6837e11691c83790a25952e519a2757ea8b7dad7fb793691a7142d4eead6d230416117d66329949f8161e07b1392855bb6bbd80e0592c96a6ef13
6
+ metadata.gz: 3b9fd95aed554c41cb24b5abd381fe54f8f77e18fae9bcdbf967b448bfbfc24c2bcef81eb57eac2f41a336b6ae3231c62bb99458d2d926c918c7496ca30e5af1
7
+ data.tar.gz: b205973f081b28dc6e1293ad59fd46f8f682862b2247444a93a0c144535e3d17570536d058e16157cfbd7726e8f05eaba649a04a4fa8d7f7ea75328d24cfe7e6
data/lib/deep_cloning.rb CHANGED
@@ -3,11 +3,12 @@ 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.3'.freeze
6
+ VERSION = '0.1.4'.freeze
7
7
  def initialize(root, opts = { except: [], save_root: true })
8
8
  @root = root
9
9
  @opts = opts
10
10
  @opts[:source] = []
11
+ @must_ignore = []
11
12
  end
12
13
 
13
14
  def replicate
@@ -19,7 +20,7 @@ module DeepCloning
19
20
  yield(@root, clone, :before_save) if block_given?
20
21
  clone.save if clone.new_record? # avoid save again if saved on block
21
22
  yield(@root, clone, :after_save) if block_given?
22
- raise clone.errors.full_messages.join(', ') if clone.errors.any?
23
+ raise "#{clone.class} - #{clone.errors.full_messages.join(', ')}" if clone.errors.any?
23
24
  @opts[clone.class.name] = { @root.id => clone }
24
25
  end
25
26
  leafs(@root).each do |cell|
@@ -33,7 +34,7 @@ module DeepCloning
33
34
  end
34
35
  unless @cell
35
36
  ap @opts[:source].map { |s| "#{s.id} - #{s.class.name}" }
36
- raise 'cannot duplicate the hierarchy'
37
+ raise "Cannot duplicate the Hierarchy. You must ignore: #{@must_ignore.join(', ')}"
37
38
  end
38
39
  @opts[:source] -= [@cell]
39
40
 
@@ -71,6 +72,14 @@ module DeepCloning
71
72
  end
72
73
  end
73
74
 
75
+ def safe_child?(child, parent)
76
+ !child.respond_to?("#{parent.name}_id".to_sym) or
77
+ child.send("#{parent.name}_id").nil? or
78
+ @opts[parent.class_name][child.send("#{parent.name}_id")] or
79
+ parent.class_name.in? @opts[:except]
80
+ # replicated parent?
81
+ end
82
+
74
83
  # Need to check the relations instead the models only
75
84
  def walk?(cell)
76
85
  parents(cell.class).map do |p|
@@ -84,9 +93,11 @@ module DeepCloning
84
93
  end
85
94
  else
86
95
  @opts[p.class_name] = {} unless @opts[p.class_name]
87
- !cell.respond_to?("#{p.name}_id".to_sym) or cell.send("#{p.name}_id").nil? or @opts[p.class_name][cell.send("#{p.name}_id")] or p.class_name.in? @opts[:except] # replicated parent?
96
+ safe_child = safe_child?(cell, p)
97
+ @must_ignore << p.class_name unless safe_child
98
+ safe_child
88
99
  end
89
- end.reduce(true) { |result, value| result and value }
100
+ end.all?(&:present?)
90
101
  end
91
102
 
92
103
  def leafs(cell)
metadata CHANGED
@@ -1,14 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep-cloning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nilton Vasques
8
+ - Victor Cordeiro
9
+ - Beatriz Fagundes
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2018-09-10 00:00:00.000000000 Z
13
+ date: 2019-02-18 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: minitest
@@ -55,6 +57,8 @@ dependencies:
55
57
  description: DeepCloning is a gem that is able to replicate a set of records in depth
56
58
  email:
57
59
  - nilton.vasques@gmail.com
60
+ - victorcorcos@gmail.com
61
+ - beatrizsfslima@gmail.com
58
62
  executables: []
59
63
  extensions: []
60
64
  extra_rdoc_files: []