solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Boolean array coercion issue #204

Closed MichaelXavier closed 10 years ago

MichaelXavier commented 10 years ago

I've been working on some bugs I've found using virtus with grape. They coerce parameters using Virtus::Attribute.

So If I have a parameter that takes an array of booleans, Virtus::Attributes.build([Virtus::Attributes::Boolean]).coerce(['1', '0']) returns ['1', '0']

Using a basic type like [Float] works fine. Using TrueClass/FalseClass works fine. Using Axiom::Types::Boolean work fine. There's something about Boolean that isn't working. Is grape doing something wrong or is this a bug?

solnic commented 10 years ago

You want this:

Virtus::Attribute.build(['Boolean']).coerce(['1', '0'])

...as it's shorter.

I do admit this is a bug because it should catch that member type is already an attribute sub-class and get primitive from that subclass.

I'll fix it shortly.

solnic commented 10 years ago

@MichaelXavier I just checked grape and its spec suite passes against latest virtus master :smile:

solnic commented 10 years ago

...I mean I had to update the way attribute instances are being built of course

MichaelXavier commented 10 years ago

Sweet! Did you just change it to do Virtus::Attributes.build(type) instead of Virtus::Attributes.build(:a, type)? I'm looking to put in a PR on the grape project that cases off of virtus version and constructs the attribute accordingly. I'll leave it to them to decide if that's the right approach.

solnic commented 10 years ago

@MichaelXavier yeah you need to just pass in the type...grape was one of the reasons why I did this btw