- Revision
- 132
- Author
- mward
- Date
- 2007-06-02 00:49:45 -0500 (Sat, 02 Jun 2007)
Log Message
updated IntrospectingRequestAttributeBinder to handle RubyController instances as special cases (the ruby class will be inspected for its instance variables)
Modified Paths
Diff
Modified: trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java (131 => 132)
--- trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java 2007-06-02 05:18:11 UTC (rev 131) +++ trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java 2007-06-02 05:49:45 UTC (rev 132) @@ -1,18 +1,31 @@ package org.codehaus.waffle.bind; import org.codehaus.waffle.WaffleException; +import org.codehaus.waffle.controller.RubyController; +import org.jruby.javasupport.JavaEmbedUtils; +import org.jruby.runtime.builtin.IRubyObject; import javax.servlet.http.HttpServletRequest; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.InvocationTargetException; +import java.util.Map; +import java.util.Set; +/** + * This implementation can handle all standard Java objects and RubyControllers are handled specially (instance_variables) + */ public class IntrospectingRequestAttributeBinder implements RequestAttributeBinder { public void bind(HttpServletRequest request, Object controller) { + if (controller instanceof RubyController) { + handleRubyController(request, (RubyController) controller); + return; + } + try { BeanInfo beanInfo = Introspector.getBeanInfo(controller.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); @@ -32,4 +45,16 @@ throw new WaffleException(e); } } + + private void handleRubyController(HttpServletRequest request, RubyController rubyController) { + IRubyObject iRubyObject = rubyController.getRubyObject(); + //noinspection unchecked + Map<String, IRubyObject> iVars = iRubyObject.getInstanceVariables(); + Set<String> keys = iVars.keySet(); + + for (String key : keys) { + Object value = JavaEmbedUtils.rubyToJava(iRubyObject.getRuntime(), iVars.get(key), Object.class); + request.setAttribute(key.substring(1), value); + } + } }
Modified: trunk/core/src/test/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinderTest.java (131 => 132)
--- trunk/core/src/test/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinderTest.java 2007-06-02 05:18:11 UTC (rev 131) +++ trunk/core/src/test/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinderTest.java 2007-06-02 05:49:45 UTC (rev 132) @@ -6,6 +6,9 @@ import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Test; import org.junit.runner.RunWith; +import org.codehaus.waffle.controller.RubyController; +import org.jruby.Ruby; +import org.jruby.runtime.builtin.IRubyObject; import javax.servlet.http.HttpServletRequest; @@ -38,4 +41,30 @@ } } + @Test + public void rubyInstanceVariablesShouldBeBound() { + String rubyScript = + "class Foo\n" + + " def initialize\n" + + " @name = 'my_name'\n" + + " @number = 1985\n" + + " end\n" + + "end\n" + + "Foo.new"; + + Ruby runtime = Ruby.getDefaultInstance(); + IRubyObject iRubyObject = runtime.evalScript(rubyScript); + RubyController controller = new RubyController(iRubyObject); + + final HttpServletRequest request = context.mock(HttpServletRequest.class); + + context.checking(new Expectations() {{ + one (request).setAttribute("name", "my_name"); + one (request).setAttribute("number", 1985L); + }}); + + IntrospectingRequestAttributeBinder binder = new IntrospectingRequestAttributeBinder(); + binder.bind(request, controller); + } + }
To unsubscribe from this list please visit:
