On Nov 28, 11:04 pm, Alex Neth <[EMAIL PROTECTED]> wrote:
> >> My more immediately point is that it is hard, if not impossible, to
> >> write correct code currently with DM.
> > Now that's just hyperbole. Let me state, that off the top of my head,
> > a false save should only occur due to validation failures. If that's
> > not the case in your experience with DM definitely let me know because
> > that's the intent.
>
> ...
>
> > Like I said, if
> > #save returns false in cases other than validation failures, it's a
> > bug.
>
> Not exactly court enforceable, but I'll take it. If you really feel
> that way, I think the method contract should be changed to reflect
> this.
I'm a bit lost to be honest. It's not like we've got DbC here...
you're commenting on the docs maybe?
> I don't think it's hyperbole to say that you can't write "correct"
> code without that guarantee, unless you're ok with putting this in
> every single save call (can't think of a way to do it in an after hook
> since you can't get the return value to my knowledge):
>
> unless foo.save
> if foo.errors.length > 0
> raise UnknownSaveFailure(foo)
> else
> log "Invalid record: ..."
> end
> end
The hook I mentioned should do it. Assuming that validation failures
are the only cause, then hook it with an :after, and check the errors
length.
That way, if you still get a save failure, you can exception it if you
want a little extra guarantee and it'll bubble up:
Foo.hook(:save) { |foo| raise RecordInvalidError.new(foo.errors)
unless foo.errors.empty? }
if foo.save || raise(PersistenceError.new(foo))
...
-Sam
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---