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 Mixin#{dup,clone} (fixes #63) #64

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Changes from all 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
Add Mixin#{dup,clone} (fixes #63)
We shoulnt't share `_container` instances across different containers, it leads to bugs and confusion.
flash-gordon committed Jun 7, 2019

Verified

This commit was signed with the committer’s verified signature.
flash-gordon Nikita Shilnikov
commit 1abeec660de3d3c51c6db19664a08f443a650455
16 changes: 16 additions & 0 deletions lib/dry/container/mixin.rb
Original file line number Diff line number Diff line change
@@ -270,6 +270,22 @@ def freeze
def _container
@_container
end

# @api public
def dup
copy = super
copy.instance_variable_set(:@_container, _container.dup)
copy
end

# @api public
def clone
copy = super
unless copy.frozen?
copy.instance_variable_set(:@_container, _container.dup)
end
copy
end
end
end
end
19 changes: 19 additions & 0 deletions spec/support/shared_examples/container.rb
Original file line number Diff line number Diff line change
@@ -616,4 +616,23 @@
expect(container.freeze).to be(container)
end
end

describe '.dup' do
it "returns a copy that doesn't share registered keys with the parent" do
container.dup.register(:foo, 'bar')
expect(container.key?(:foo)).to be false
end
end

describe '.clone' do
it "returns a copy that doesn't share registered keys with the parent" do
container.clone.register(:foo, 'bar')
expect(container.key?(:foo)).to be false
end

it 're-uses frozen container' do
expect(container.freeze.clone).to be_frozen
expect(container.clone._container).to be(container._container)
end
end
end