A gem of various methods used in many of my plugins. If you need the namespace, let me know.
Brian Haberer
MIT