Virtus: Coercing boolean attributes

Posted Almost 11 years ago. Visible to the public.

TLDR

Do it like this:

attribute :active, Virtus::Attribute::Boolean

Long story

In Virtus Show archive.org snapshot you define attribute with their type like this:

attribute :name, String
attribute :birthday, Date

When defining a boolean attributes, you will probably write it like this:

attribute :active, Boolean

The problem is, there is not actually a Boolean class in Ruby (there's only TrueClass and FalseClass), so use Virtus::Attribute::Boolean instead.

The reason while Boolean sometimes survives compilation is that many libraries define a global Boolean class for some reason. That class however won't work with virtus.

Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-06-06 16:23)