Skip to content
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 10 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
* core_ext/array/deletes.rb
* `#delete_blanks` - Deletes all items where the value is blank
* `#delete_nils` - Deletes all items where the value is nil
* `#deep_delete` - Deletes nested hash key elements
* core_ext/array/duplicates.rb
* `#duplicates` - Returns an Array of the duplicates found
* core_ext/array/element_counts.rb
Expand Down Expand Up @@ -56,11 +57,16 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
* `#lineage` - Returns an Array of all superclasses.
* `#leaf_subclasses` - Returns an Array of all descendants which have no subclasses.

#### Enumerable
* core_ext/enumerable/deep_clone.rb
* `#deep_clone` - Performs a Marshal based deep clone (Array, Hash).

#### Hash

* core_ext/hash/deletes.rb
* `#delete_blanks` - Deletes all keys where the value is blank
* `#delete_nils` - Deletes all keys where the value is nil
* `#deep_delete` - Deletes nested hash key elements
* core_ext/hash/nested.rb (see [Shared](#shared))
* `#delete_blank_paths` - Deletes all paths where the value is blank
* core_ext/hash/sorting.rb (see [Shared](#shared))
Expand Down
9 changes: 9 additions & 0 deletions lib/more_core_extensions/core_ext/array/deletes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def delete_nils
def delete_blanks
delete_if { |i| i.blank? }
end

# Deletes all keys and subkeys that match +key+.
#
# [{:a => {:b => 2, :c => 3}}].deep_delete(:b) # => [{:a => {:c => 3}}]
#
def deep_delete(key)
each { |i| i.deep_delete(key) if i.respond_to?(:deep_delete) }
self
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/more_core_extensions/core_ext/enumerable.rb
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 lib/more_core_extensions/core_ext/enumerable/deep_clone.rb
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@djberg96 djberg96 Jul 17, 2020

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne ok, updated.

Marshal.load(Marshal.dump(self))
end
end
end

Array.send(:include, MoreCoreExtensions::EnumerableDeepClone)
Hash.send(:include, MoreCoreExtensions::EnumerableDeepClone)
10 changes: 10 additions & 0 deletions lib/more_core_extensions/core_ext/hash/deletes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def delete_nils
def delete_blanks
delete_if { |k, v| v.blank? }
end

# Deletes all keys and subkeys that match +key+.
#
# {:a => {:b => 2, :c => 3}}.deep_delete(:b) # => {:a => {:c => 3}}
#
def deep_delete(key)
bdunne marked this conversation as resolved.
Show resolved Hide resolved
key = [key] unless key.kind_of?(Array)
key.each { |k| delete(k) }
each_value { |v| v.deep_delete(key) if v.respond_to?(:deep_delete) }
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/core_ext/array/deletes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
expect([nil].delete_blanks).to eq([])
expect([1, [], nil].delete_blanks).to eq([1])
end

it "#deep_delete" do
expect([{}].deep_delete(:c)).to eq([{}])
expect([{:a => {:b => [1, 2], :c => 3}}].deep_delete(:c)).to eq([{:a => {:b => [1, 2]}}])
expect([{:a => {:b => {:c => 1}, :c => 3}}].deep_delete(:c)).to eq([{:a => {:b => {}}}])
end
end
34 changes: 34 additions & 0 deletions spec/core_ext/enumerable/deep_clone.rb
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
6 changes: 6 additions & 0 deletions spec/core_ext/hash/deletes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
expect({:c => nil}.delete_blanks).to eq({})
expect({:a => 1, :b => [], :c => nil}.delete_blanks).to eq({:a => 1})
end

it "#deep_delete" do
expect({}.deep_delete(:c)).to eq({})
expect({:a => {:b => [1, 2], :c => 3}}.deep_delete(:c)).to eq({:a => {:b => [1, 2]}})
expect({:a => {:b => {:c => 1}, :c => 3}}.deep_delete(:c)).to eq({:a => {:b => {}}})
end
end