Created
May 31, 2012 01:49
-
-
Save grimen/2840339 to your computer and use it in GitHub Desktop.
Trying to mimic "method missing" in Node.js. Purpose: Allow snake case always. Problem: Don't seem to be possible to forward argument attributes?
This file contains 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
var inspect = require('eyes').inspector({ | |
styles: { | |
all: 'cyan', | |
label: 'underline', | |
other: 'inverted', | |
key: 'bold', | |
special: 'grey', | |
string: 'green', | |
number: 'magenta', | |
bool: 'blue', | |
regexp: 'green' | |
}}); | |
function missinMethod() { | |
console.log("missinMethod()") | |
} | |
// Mimic "method_missing" in Ruby. | |
process.on('uncaughtException', function(err) { | |
inspect(arguments); | |
if (err.arguments.length) { | |
var method_name = err.arguments[0], | |
method_name_camel_case = method_name.replace(/(_[a-z0-9])/mg, function(a) { return a.replace('_', '').toUpperCase(); }) | |
if (typeof eval(method_name_camel_case) === 'function') { | |
console.log("Proxy method: %s => %s", method_name, method_name_camel_case); | |
eval(method_name_camel_case + '()'); | |
} else { | |
console.log("No such method: %s => %s", method_name, method_name_camel_case) | |
// skip | |
} | |
} | |
}); | |
missin_method("foo", "bar"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment