solnic / coercible

Powerful, flexible and configurable coercion library. And nothing more.
MIT License
137 stars 16 forks source link

Array coercion #7

Closed schmurfy closed 10 years ago

schmurfy commented 10 years ago

Hi, I wanted to give a try to this gem but I can't figure out how I could coerce an entire array, I suppose it is possible since Virtus supports it but I have no idea how and where to start looking :(

Here what I wish to achieve:

input  = ["1", 2, 5]
output = [1, 2, 5]

is it possible without iterating myself through the array ?

solnic commented 10 years ago

nope it's not possible, at this moment I don't think coercible will have such complex features, it's only meant for "primitive" coercions.

mbj commented 10 years ago

@solnic +1 for keeping it "sharp".

schmurfy commented 10 years ago

no problem, I was just wondering if there already something builtin I was not requesting it :) could you give me some direction on where this is implemented in Virtus ?

solnic commented 10 years ago

@schmurfy it's semi-broken in master right now but I can tell you how it is going to be done eventually for 1.0.0.

In virtus 0.5.x it's basically Attribute::Array which uses another Attribute-subclass instance based on :member_type setting and then simply coerces everything inside an array using that member type attribute. This is a shared behaviour for all collection-like attributes and it's all here.

In virtus 1.0.0 it'll be slightly different since internals are being sliced into smaller pieces. Attribute uses an object called Accessor which is composed of Reader and Writer. And it's the writer which knows how to write an attribute value. Writers can be used with or without coercers. The missing part now is to move complex coercion logic like member-type coercion to virtus' coercer wrapper (it's an object that wraps coercible) so that an embedded collection attribute can use it instead of entire writer object (or attribute, like in 0.5.x).

schmurfy commented 10 years ago

thanks a lot for the precisions !