From 772fa7d58617935a7a70ee27931ee5179dc6bb9e Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sun, 10 Jan 2010 11:37:51 +0200 Subject: [PATCH] Invoke ruby methods with their javascript property names. --- ext/v8/converters.cpp | 8 +++++++- ext/v8/converters.h | 2 ++ ext/v8/v8.cpp | 3 +++ lib/v8/to.rb | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ext/v8/converters.cpp b/ext/v8/converters.cpp index fa18d56..87ccea6 100644 --- a/ext/v8/converters.cpp +++ b/ext/v8/converters.cpp @@ -9,6 +9,8 @@ namespace { std::string UNDEFINED_STR("undefined"); } +VALUE V8_To; + VALUE V82RB(Handle& value) { convert_v8_to_rb_t convert; VALUE result; @@ -42,9 +44,13 @@ Local RB2V8(VALUE value) { int len = RARRAY(methods)->len; for (int i = 0; i < len; i++) { VALUE method_name = RARRAY(methods)->ptr[i]; + VALUE camel_method_name = rb_funcall(V8_To, rb_intern("camelcase"), 1, method_name); VALUE method = rb_funcall(value, rb_intern("method"), 1, method_name); Local keystr = (String *)*RB2V8(method_name); - tmpl->Set(keystr, RB2V8(method)); + Local camelstr = (String *)*RB2V8(camel_method_name); + Local fun = RB2V8(method); + tmpl->Set(keystr, fun); + tmpl->Set(camelstr, fun); } return tmpl->NewInstance(); } diff --git a/ext/v8/converters.h b/ext/v8/converters.h index c0f79cb..f43df98 100644 --- a/ext/v8/converters.h +++ b/ext/v8/converters.h @@ -6,6 +6,8 @@ #include "convert_v8.h" #include +extern VALUE V8_To; + typedef RubyValueSource > convert_rb_to_v8_t; typedef V8HandleSource convert_v8_to_rb_t; diff --git a/ext/v8/v8.cpp b/ext/v8/v8.cpp index 6c50971..2a938ed 100644 --- a/ext/v8/v8.cpp +++ b/ext/v8/v8.cpp @@ -6,6 +6,7 @@ #include "v8_script.h" #include "v8_template.h" #include "v8_standalone.h" +#include "converters.h" #include @@ -29,6 +30,8 @@ extern "C" { rb_mModule = rb_define_module("V8"); rb_define_singleton_method(rb_mModule, "what_is_this?", (VALUE(*)(...)) v8_what_is_this, 1); + + V8_To = rb_define_module_under(rb_mModule, "To"); //native module setup VALUE rb_mNative = rb_define_module_under(rb_mModule, "C"); diff --git a/lib/v8/to.rb b/lib/v8/to.rb index b6c8305..bd44012 100644 --- a/lib/v8/to.rb +++ b/lib/v8/to.rb @@ -20,6 +20,10 @@ module V8 value end end + + def camelcase(str) + str.to_s.gsub(/_(\w)/) {$1.upcase} + end end end end \ No newline at end of file