diff --git a/lib/dolly/property_manager.rb b/lib/dolly/property_manager.rb index ad04bbe..df2b379 100644 --- a/lib/dolly/property_manager.rb +++ b/lib/dolly/property_manager.rb @@ -17,14 +17,14 @@ def update_attribute end end - def write_attribute key, value + def write_attribute(key, value) value = set_property_value(key, value) instance_variable_set(:"@#{key}", value) - update_doc(key, value) unless value.nil? + update_doc(key, value) end def valid_property?(name) - properties.include? name + properties.include?(name) end def update_doc(key, value) diff --git a/test/property_manager_test.rb b/test/property_manager_test.rb new file mode 100644 index 0000000..e71526f --- /dev/null +++ b/test/property_manager_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class TestDoc < Dolly::Document + property :name, class_name: String + property :email, class_name: String + property :last_name, class_name: String +end + +class PropertyManagerTest < Test::Unit::TestCase + test 'write_attribute with nil value' do + doc = TestDoc.new(name: 'name', last_name: nil, email: 'does not change') + assert_equal(doc.name, 'name') + doc.update_properties(name: nil) + assert_equal(doc.name, nil) + assert_equal(doc.last_name, nil) + assert_equal(doc.email, 'does not change') + end +end