virtus icon indicating copy to clipboard operation
virtus copied to clipboard

How to extend so default is assigned when coercion fails?

Open adz opened this issue 12 years ago • 14 comments

I really like Virtus and appreciate the high quality and tight focus.

I'm trying to use Virtus as a form-object. I know there has been discussion on this in https://github.com/solnic/virtus/issues/99, so I'm looking at how to properly build atop/use Virtus.

This gist overrides the reader to return default when it isn't the expected type: https://gist.github.com/joakimk/3515735

I'm using something similar but relying on Virtus and using a writer: https://gist.github.com/adz/8215768

However, it's a bit crappy. I'd really like to add an attribute extension like this one (or some other nice extension point): https://github.com/solnic/virtus/blob/master/lib/virtus/attribute/strict.rb

My goal is to assign the default value when coercion fails.

Any help appreciated!

adz avatar Jan 02 '14 06:01 adz

Thanks. I was thinking about having a way to instruct virtus what to do when a coercion fails. I'll look at how we could do this in a clean way and let you know.

solnic avatar Jan 02 '14 12:01 solnic

@solnic I just remember we talked about this ;) Greez morpher.

mbj avatar Jan 02 '14 15:01 mbj

@mbj oh?

I wonder how long it would take to port virtus to use morpher. I would expect it to be simple :)

solnic avatar Jan 02 '14 15:01 solnic

@solnic I think it would be very easy. As a sideeffect you get some "transformation failed and could not produce a valid instance with correct types" - state, for free. With history :D

mbj avatar Jan 02 '14 17:01 mbj

@mbj one thing I am concerned a little is performance, got any benchmarks with morpher already?

solnic avatar Jan 02 '14 17:01 solnic

@solnic No. I expect it to be lots of faster than ducktrap in non tracing / hybrid mode.

mbj avatar Jan 02 '14 17:01 mbj

@mbj I'm so excited about this I'd try it out asap; please link me to some morpher examples if you have some, I could try porting virtus as an experiment just to see how hard it's gonna be (or easy!)

solnic avatar Jan 02 '14 17:01 solnic

Thanks guys... post back here if you want any review from an outsider trying to use such extensions...

also morpher looks pretty crazy (in a cool way!)

On 3 January 2014 03:57, Piotr Solnica [email protected] wrote:

@mbj https://github.com/mbj I'm so excited about this I'd try it out asap; please link me to some morpher examples if you have some, I could try porting virtus as an experiment just to see how hard it's gonna be (or easy!)

— Reply to this email directly or view it on GitHubhttps://github.com/solnic/virtus/issues/235#issuecomment-31468342 .

adz avatar Jan 03 '14 01:01 adz

@adz I will want to add the feature you requested in 1.x series despite the morpher integration which I plan for 2.0

solnic avatar Jan 03 '14 07:01 solnic

OK thanks. On 03/01/2014 6:21 PM, "Piotr Solnica" [email protected] wrote:

@adz https://github.com/adz I will want to add the feature you requested in 1.x series despite the morpher integration which I plan for 2.0

— Reply to this email directly or view it on GitHubhttps://github.com/solnic/virtus/issues/235#issuecomment-31509493 .

adz avatar Jan 04 '14 20:01 adz

My two cents here are

:+1: for "having a way to instruct virtus what to do when a coercion fails"

Here is a quick workaround I have used so that a default value is set if coercion fails - its not ideal but...

attribute :delay, Integer, strict: true, default: Config.default_delay #wherever this is stored

def delay=(value)
  super(value) rescue Config.default_delay #wherever this is stored
end

Set the attribute to strict coercion and then rescue with the default :(

rob-murray avatar Mar 05 '15 09:03 rob-murray

It makes totally sense, if coercion fails set the default. I don't understand why there hasn't been any progress on that.

vasilakisfil avatar Aug 13 '15 10:08 vasilakisfil

@vasilakisfil because clearly nobody had the time to work on it

solnic avatar Aug 13 '15 10:08 solnic

cool I thought it was another reason, I will come up with a solution.

vasilakisfil avatar Aug 13 '15 10:08 vasilakisfil