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 option to ignore property if instance is nil. #76

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
2 changes: 2 additions & 0 deletions lib/representable/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def call(fragment)
@object = @binding.create_object(fragment)
end

return if @binding.options[:ignore_nil_instance] && @object == fragment

# DISCUSS: what parts should be in this class, what in Binding?
representable = prepare(@object)
deserialize(representable, fragment, @binding.user_options)
Expand Down
11 changes: 11 additions & 0 deletions test/representable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ def compute_representer(name)
song.name.must_equal obj
song.name.must_be_kind_of mod
end

describe "with :ignore_nil_instance set and the instance being nil" do
representer! do
property :name, :extend => mod, :instance => lambda { |*| nil }, :ignore_nil_instance => true
end

it "does not evaluate the property" do
song = Song.new.extend(representer).from_hash("name" => "Eric's Had A Bad Day")
song.name.must_be_nil
end
end
end

describe "property with :extend" do
Expand Down