public
Last active

Trying to mimic "method missing" in Node.js. Purpose: Allow snake case always. Problem: Don't seem to be possible to forward argument attributes?

  • Download Gist
method_missing_example.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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");

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.