Created
July 1, 2011 06:30
-
-
Save emmanuel/1057986 to your computer and use it in GitHub Desktop.
Proposal for decoupling dm-validations from Resource internals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # TODO: shouldn't Validations use `before :save` to intercept the save call? | |
| # (for DataMapper::Resource instances, not validated plain Ruby objects) | |
| # | |
| # Admittedly, it may not be possible to support as 'clean' of an interface | |
| # from the caller's perspective (ie., being able to specify the validation | |
| # context with an arg to the #save/#update methods vs in some other manner). | |
| # | |
| # However, changing the method signature of #save and #update is problematic. | |
| # Also, I'm not sure how often an explicit validation context is used, | |
| # and on that basis I question whether the interface should be designed for | |
| # the explicit validation context use-case, as it is currently. | |
| # | |
| # A new interface on the resource could provide a reasonable compromise: | |
| resource.validation_context(:foo).save | |
| # The implementation could be fairly simple, wherein the #validation_context | |
| # call returns a ContextProxy: | |
| module DataMapper::Validations::Resource | |
| def validation_context(context = Undefined) | |
| DataMapper::Validations::ContextProxy.new(self, context) | |
| end | |
| end | |
| # And that, in turn, proxies calls on it into calls much like the current | |
| # implementation (via explicit methods, or #method_missing): | |
| class DataMapper::Validations::ContextProxy | |
| def save | |
| Validations::Context.in_context(@context) { @resource.save } | |
| end | |
| end | |
| # This would allow dm-validations to use a `before :save, :validate` hook to | |
| # intercept the save event and therefore to provide the same functionality as | |
| # currently, but without coupling so tightly with Resource. | |
| # Also, such an architecture would allow preserving (and deprecating) the current | |
| # interface for a transitional period. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment