Skip to content

Commit

Permalink
Merge pull request #64 from dry-rb/add-dup
Browse files Browse the repository at this point in the history
Add Mixin#{dup,clone} (fixes #63)
  • Loading branch information
flash-gordon authored Jun 7, 2019
2 parents a2ec972 + 1abeec6 commit 3dd6c3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/dry/container/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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

0 comments on commit 3dd6c3b

Please sign in to comment.