tenderlove/psych — pull request #194 by brentdax (2014-05-31 13:33:53+00:00)
Title: Add ability to substitute objects during encoding and decoding

This pull request includes changes that allow users to alter the objects being encoded or decoded by Psych. My own app will use this capability to replace ActiveRecord objects with placeholders that fetch the current version of the object rather than re-creating the old version, but you can probably imagine many other uses for it.

In this version of the pull request, you use this feature by giving a block to calls like `YAML.load` or `YAML.dump`. Each time an object is about to be encoded, or has just been decoded, it is passed to the block. If the block returns a different object than the original, it will be used in the original object's place.

I'm not sure if this is the right interface for this feature—it's probably not something that would be needed often, after all, and you may want to leave the block open for some other use. I'm open to suggestions on other ways to implement this.

This is also my first Psych patch. I've added tests and updated the documentation to explain this feature, but I may have missed other things I should have done. All tests pass on my machine.