Skip to content

Commit

Permalink
Prevent mutation of original data (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
afuno committed Sep 13, 2024
1 parent 80c9101 commit d1155f0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/datory/attributes/serialization/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.to_hash(...)

def prepare(data)
if data.is_a?(Hash)
build(data)
build(data.deep_dup)
else
data
end
Expand Down
12 changes: 8 additions & 4 deletions spec/examples/usual/example1/serial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
describe "singular" do
subject(:perform) { described_class.serialize(serial) }

it { expect { perform }.not_to(change { serial }) }

it do
expect(perform).to match(
{
Expand Down Expand Up @@ -316,6 +318,8 @@

let(:serials) { [serial] }

it { expect { perform }.not_to(change { serials }) }

it do
expect(perform).to contain_exactly(
{
Expand Down Expand Up @@ -514,13 +518,13 @@
end

let(:ratings) do
Usual::Example1::Ratings.new(
imdb: Usual::Example1::Rating.new(
{
imdb: {
value: 8.6,
quantity: 324_000,
link_url: nil # NOTE: This example explicitly passes the value `nil` for the optional attribute.
)
)
}
}
end

context "when the data required for work is valid" do
Expand Down
4 changes: 4 additions & 0 deletions spec/examples/usual/example2/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
describe "singular" do
subject(:perform) { described_class.serialize(product) }

it { expect { perform }.not_to(change { product }) }

it do
expect(perform).to match(
{
Expand All @@ -153,6 +155,8 @@

let(:products) { [product] }

it { expect { perform }.not_to(change { products }) }

it do
expect(perform).to contain_exactly(
{
Expand Down
4 changes: 4 additions & 0 deletions spec/examples/usual/example3/language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
describe "singular" do
subject(:perform) { described_class.serialize(language) }

it { expect { perform }.not_to(change { language }) }

it do
expect(perform).to match(
{
Expand All @@ -152,6 +154,8 @@

let(:languages) { [language] }

it { expect { perform }.not_to(change { languages }) }

it do
expect(perform).to contain_exactly(
{
Expand Down
4 changes: 4 additions & 0 deletions spec/examples/usual/example4/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
describe "singular" do
subject(:perform) { described_class.serialize(comment) }

it { expect { perform }.not_to(change { comment }) }

it do
expect(perform).to match(
{
Expand All @@ -138,6 +140,8 @@

let(:comments) { [comment] }

it { expect { perform }.not_to(change { comments }) }

it do
expect(perform).to contain_exactly(
{
Expand Down

0 comments on commit d1155f0

Please sign in to comment.