rodrigolive/mongoose — Issue #14: MethodModifier-friendly 'save' subroutine
Author: gugod
Created: 2011-12-10 05:12:24+00:00
State: open

Hi @rodrigolive, and all Mongoose users,

Here's thought: It would be nice to make 'save' work nicely with Moose's before/after/around method modifiers.

Currently it is working, data gets saved, but there are unexpected outcomes. For one example, I've been using Mongoose for $client, and some of their data need to be exported to another DB on save. Since Mongoose::Document is a Moose class, I find it handy to say:

after 'save' => sub {
export(....)
};

However, when the document has relations, `save` may be invoked multiple times during the collapsing process, so does the `export` subroutine, hence the problem.

I would like to propose to make 'save' routine friendly to Moose's method modifier because there can be many useful scenarios, the expected result, is to only call the modifier when user explicitly calls 'save'.

I've soved this problem by simply making 'save' a wrapper around '_save', which does the real work. I've made a commit with a small test that demonstrate the effect:

Commit: https://github.com/gugod/mongoose/commit/a4db6d7ea27ea3a1d83e9728eb74b6e15267e836

Without the patch, line 48 of after-save.t https://github.com/gugod/mongoose/blob/a4db6d7ea27ea3a1d83e9728eb74b6e15267e836/t/after-save.t#L48 would shows you that `$cat_count` is 11 instead of 1, which seems to me a bit too much, consider those invocations actually saves to db, but that's an issue the commit does not solve.

Does anyone also like the idea ?