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

Use Setter in OverwriteOnNil if a :setter is defined #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion lib/representable/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ module Representable
end

OverwriteOnNil = ->(input, options) do
input.nil? ? (SetValue.(input, options); Pipeline::Stop) : input
if input.nil?
function = options[:binding][:setter] ? Setter : SetValue
Copy link
Member

Choose a reason for hiding this comment

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

This is correct, but we can't inspect this at runtime for performance reasons, we have to maintain two Override functions and set them at compile time.

Copy link
Author

Choose a reason for hiding this comment

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

I see, so rewrite the PR to make the pipeline factory pick which OverwriteOnNil function to use?

Copy link
Member

Choose a reason for hiding this comment

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

Exactly! Thanks! 😬

function.(input, options)
Pipeline::Stop
else
input
end
end

Default = ->(input, options) do
Expand Down
5 changes: 5 additions & 0 deletions test/getter_setter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ class GetterSetterTest < BaseTest
subject.instance_eval { def name=(*); raise; end; self }
subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name.must_equal "Hello Eyes Without A Face"
end

it "uses :setter when parsing if set value is nil" do
subject.instance_eval { def name=(*); raise('used SetValue instead of Setter'); end; self }
subject.from_hash({"name" => nil}, user_options: {welcome: "Hello"}).song_name.must_equal "Hello "
end
end