solnic/virtus — pull request #85 by tjarratt (2012-04-24 00:07:42+00:00)
Title: Support DataMapper :required option

DataMapper supports a :required option that allows you to specify properties that might be present, but might not be. This is very useful in scenarios where your property is some type that can throw exceptions for certain values when you coerce it to its native type.

Instead of having to implement error handling on all of your custom attributes that implement `coerce`, you can mark that attribute as being optional and handle the case where it isn't available outside the model itself.

For example, you might have a custom property that takes ruby Strings (from the network, a database, or some other source) and coerces them to a UUID. UUIDs have very specific formatting rules, so if you pass it an empty string, or something that doesn't match a UUID at all, it will likely throw an exception.

`
class TestModel
include Virtus
attribute :name, String
attribute :uuid, UUID, :required => false
end

object = TestModel.new(:name => 'foo') # works, uuid is nil
object = TestModel.new(:name => 'bar', :uuid => nil) # fails, cannot cast nil to UUID
`