solnic / virtus

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

How to extend so default is assigned when coercion fails? #235

Open adz opened 10 years ago

adz commented 10 years ago

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!

solnic commented 10 years ago

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.

mbj commented 10 years ago

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

solnic commented 10 years ago

@mbj oh?

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

mbj commented 10 years ago

@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

solnic commented 10 years ago

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

mbj commented 10 years ago

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

solnic commented 10 years ago

@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!)

adz commented 10 years ago

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 notifications@github.com 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 .

solnic commented 10 years ago

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

adz commented 10 years ago

OK thanks. On 03/01/2014 6:21 PM, "Piotr Solnica" notifications@github.com 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 .

rob-murray commented 9 years ago

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 :(

vasilakisfil commented 8 years ago

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

solnic commented 8 years ago

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

vasilakisfil commented 8 years ago

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