Skip to content

Commit

Permalink
Implement deep cloning of Configuration options (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz authored Dec 21, 2024
1 parent 43f5285 commit 6118b66
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/trestle/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def as_json(options=nil)
end
end

def initialize_copy(original)
@options = original.options.deep_dup
end

def inspect
"#<#{self.class.name || "Anonymous(Trestle::Configurable)"}>"
end
Expand Down
30 changes: 30 additions & 0 deletions spec/trestle/configurable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@
end
end

describe "#dup" do
let(:original_options) { config.options }
let(:duplicate) { config.dup }

it "returns a new instance with a clone of the options" do
expect(duplicate.options).to eq(original_options)
expect(duplicate.options).not_to be(original_options)
end
end

describe "#clone" do
let(:original_options) { config.options }
let(:duplicate) { config.clone }

it "returns a new instance with a clone of the options" do
expect(duplicate.options).to eq(original_options)
expect(duplicate.options).not_to be(original_options)
end
end

describe ".option" do
it "defines accessors for the option" do
configurable.option :myoption
Expand Down Expand Up @@ -112,5 +132,15 @@
config.first.second.third = "value"
expect(config.as_json({})).to eq({ first: { second: { third: "value" } } })
end

describe "#dup" do
it "creates clones of nested options" do
config.first.second.third = "value"
duplicate = config.dup

expect(duplicate.second.options).to eq(config.second.options)
expect(duplicate.second.options).not_to be(config.second.options)
end
end
end
end

0 comments on commit 6118b66

Please sign in to comment.