Permalink
Browse files

Changed attr readers and writers to preserve nil values

  • Loading branch information...
comeara committed Jun 4, 2009
1 parent 4069d16 commit a2b13a10a043ec69ea9a56f214554e399ca6f0ba
Showing with 16 additions and 2 deletions.
  1. +4 −2 lib/constantize_attribute.rb
  2. +12 −0 test/constantize_attribute_test.rb
@@ -11,11 +11,13 @@ def constantize_attribute *attribute_names
module_eval <<-end_eval, __FILE__, __LINE__
def #{name}= value
write_attribute(:#{name}, value.to_s)
value = value.nil? ? nil : value.to_s
write_attribute(:#{name}, value)
end
def #{name}
read_attribute(:#{name}).constantize
value = read_attribute(:#{name})
value.nil? ? nil : value.constantize
end
end_eval
@@ -53,4 +53,16 @@ def test_supports_multiple_attributes_in_one_call
def test_constantize_attributes_is_the_same_as_constantize_attribute
assert_equal ConstantizeAttribute.instance_method(:constantize_attribute), ConstantizeAttribute.instance_method(:constantize_attributes)
end
def test_writer_passes_nil_along_as_nil
m = SomeModel.new
m.foo = nil
assert_nil m.attributes[:foo]
end
def test_reader_passes_nil_along_as_nil
m = SomeModel.new
m.attributes[:foo] = nil
assert_nil m.foo
end
end

0 comments on commit a2b13a1

Please sign in to comment.