File tree Expand file tree Collapse file tree 3 files changed +47
-5
lines changed
spec/octocatalog-diff/tests/util Expand file tree Collapse file tree 3 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,9 @@ class Facts
18
18
def initialize ( options = { } , facts = nil )
19
19
@node = options . fetch ( :node , '' )
20
20
@timestamp = false
21
- @options = OctocatalogDiff :: Util :: Util . safe_dup ( options )
21
+ @options = options . dup
22
22
if facts
23
- @facts = { }
24
- facts . each { |k , v | @facts [ k ] = OctocatalogDiff ::Util ::Util . safe_dup ( v ) }
23
+ @facts = OctocatalogDiff ::Util ::Util . deep_dup ( facts )
25
24
else
26
25
case options [ :backend ]
27
26
when :json
@@ -33,8 +32,7 @@ def initialize(options = {}, facts = nil)
33
32
else
34
33
raise ArgumentError , 'Invalid fact source backend'
35
34
end
36
- @facts = { }
37
- @orig_facts . each { |k , v | @facts [ k ] = OctocatalogDiff ::Util ::Util . safe_dup ( v ) }
35
+ @facts = OctocatalogDiff ::Util ::Util . deep_dup ( @orig_facts )
38
36
end
39
37
end
40
38
Original file line number Diff line number Diff line change @@ -20,11 +20,29 @@ def self.object_is_any_of?(object, classes)
20
20
# Utility Method!
21
21
# `.dup` can't be called on certain objects (Fixnum for example). This
22
22
# method returns the original object if it can't be duplicated.
23
+ # @param object [?] Object to consider
24
+ # @return [?] Duplicated object if possible, otherwise the original object
23
25
def self . safe_dup ( object )
24
26
object . dup
25
27
rescue TypeError
26
28
object
27
29
end
30
+
31
+ # Utility Method!
32
+ # This does a "deep" duplication via recursion. Handles hashes and arrays.
33
+ # @param object [?] Object to consider
34
+ # @return [?] Duplicated object
35
+ def self . deep_dup ( object )
36
+ if object . is_a? ( Hash )
37
+ result = { }
38
+ object . each { |k , v | result [ k ] = deep_dup ( v ) }
39
+ result
40
+ elsif object . is_a? ( Array )
41
+ object . map { |ele | deep_dup ( ele ) }
42
+ else
43
+ safe_dup ( object )
44
+ end
45
+ end
28
46
end
29
47
end
30
48
end
Original file line number Diff line number Diff line change 58
58
expect ( object . object_id ) . not_to eq ( result . object_id )
59
59
end
60
60
end
61
+
62
+ describe '#deep_dup' do
63
+ it 'should dupe a hash' do
64
+ hash1 = { 'foo' => 'bar' }
65
+ hash2 = { 'baz' => hash1 }
66
+ hash3 = { 'xxx' => hash2 }
67
+ result = described_class . deep_dup ( hash3 )
68
+ expect ( result [ 'xxx' ] ) . to eq ( hash2 )
69
+ expect ( result [ 'xxx' ] . object_id ) . not_to eq ( hash2 . object_id )
70
+ end
71
+
72
+ it 'should dupe an array' do
73
+ array1 = %w[ foo bar baz ]
74
+ array2 = [ 'foo' , array1 , 'baz' ]
75
+ array3 = [ 'foo' , array2 , 'baz' ]
76
+ result = described_class . deep_dup ( array3 )
77
+ expect ( result [ 1 ] ) . to eq ( array2 )
78
+ expect ( result [ 1 ] . object_id ) . not_to eq ( array2 . object_id )
79
+ end
80
+
81
+ it 'should dupe a string' do
82
+ obj = 'Hello there'
83
+ result = described_class . deep_dup ( obj )
84
+ expect ( result ) . to eq ( obj )
85
+ end
86
+ end
61
87
end
You can’t perform that action at this time.
0 commit comments