-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add deep_clone and deep_delete #91
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7781fe4
Initial commit.
djberg96 9c3f268
Add deep_clone method and specs.
djberg96 0a3b7b6
Add Hash#deep_delete and specs.
djberg96 1345e71
Add deep_delete to Array.
djberg96 5350247
Minor cleanups.
djberg96 1cc4d73
Update readme.
djberg96 54fc0dd
Move deep_clone to shared module, update readme and specs.
djberg96 6fcb4d6
Remove old comment.
djberg96 4a9a7ec
Remove enumerable.rb.
djberg96 d4f8eae
Remove enumerable spec file.
djberg96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'more_core_extensions/core_ext/enumerable/deep_clone' |
21 changes: 21 additions & 0 deletions
21
lib/more_core_extensions/core_ext/enumerable/deep_clone.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module MoreCoreExtensions | ||
module EnumerableDeepClone | ||
# Create a deep clone of the enumerable object. This is similar to deep_dup | ||
# but uses a Marshal based approach instead. | ||
# | ||
# h1 = {:a => "hello"} | ||
# h2 = h1.deep_clone | ||
# | ||
# h1[:a] << " world" | ||
# | ||
# h1[:a] # "hello world" | ||
# h2[:a] # "hello" | ||
# | ||
def deep_clone | ||
Marshal.load(Marshal.dump(self)) | ||
end | ||
end | ||
end | ||
|
||
Array.send(:include, MoreCoreExtensions::EnumerableDeepClone) | ||
Hash.send(:include, MoreCoreExtensions::EnumerableDeepClone) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
describe Enumerable do | ||
before do | ||
@nested_hash = {'outer' => {:name => 'test', :value => [1, 2], :token => 1}} | ||
@nested_array = [{'outer' => {:name => 'test', :value => [1, 2], :token => 1}}] | ||
end | ||
|
||
context "Hash#deep_clone" do | ||
it "deep clones a hash as expected" do | ||
expect(@nested_hash.deep_clone).to eq(@nested_hash) | ||
end | ||
|
||
it "doesn't modify the clone if the original is modified" do | ||
clone = @nested_hash.deep_clone | ||
@nested_hash['outer'][:name] << 'stuff' | ||
|
||
expect(@nested_hash['outer'][:name]).to eq('teststuff') | ||
expect(clone['outer'][:name]).to eq('test') | ||
end | ||
end | ||
|
||
context "Array#deep_clone" do | ||
it "deep clones an array as expected" do | ||
expect(@nested_array.deep_clone).to eq(@nested_array) | ||
end | ||
|
||
it "doesn't modify the clone if the original is modified" do | ||
clone = @nested_array.deep_clone | ||
@nested_array[0]['outer'][:name] << 'stuff' | ||
|
||
expect(@nested_array[0]['outer'][:name]).to eq('teststuff') | ||
expect(clone[0]['outer'][:name]).to eq('test') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of
#deep_clone
living in https://github.com/ManageIQ/more_core_extensions/blob/master/lib/more_core_extensions/core_ext/shared/nested.rb instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would putting it there mean that users would have to explicitly include it to use it? I guess I'm not entirely clear on that particular module's approach as compared to the others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is meant for nested objects like hashes and arrays. It feels a little more specific than all of enumerable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne ok, updated.