22
33import java .util .List ;
44
5+ import org .jruby .CompatVersion ;
6+ import org .jruby .RubyInstanceConfig .CompileMode ;
57import org .jruby .embed .LocalContextScope ;
68import org .jruby .embed .LocalVariableBehavior ;
79import org .jruby .embed .ScriptingContainer ;
1315 *
1416 * @author Christian Kaltepoth
1517 */
16- public abstract class JRubyTransformer extends StringTransformer
18+ public abstract class JRubyTransformer < T > extends StringTransformer
1719{
1820
21+ private CompileMode compileMode = null ;
22+
23+ private CompatVersion compatVersion = null ;
24+
1925 /**
2026 * Return the load paths to use for {@link ScriptingContainer#setLoadPaths(List)}.
2127 */
@@ -27,6 +33,11 @@ public abstract class JRubyTransformer extends StringTransformer
2733 */
2834 public abstract Object runScript (ScriptingContainer container );
2935
36+ /**
37+ * Just returns the current object as the correct type for make the fluent builder work with subclasses.
38+ */
39+ public abstract T self ();
40+
3041 @ Override
3142 public final String transform (String input )
3243 {
@@ -37,6 +48,16 @@ public final String transform(String input)
3748 // as this container is only used locally, a single threaded transient one works fine
3849 container = new ScriptingContainer (LocalContextScope .SINGLETHREAD , LocalVariableBehavior .TRANSIENT );
3950
51+ // the user may have set a custom CompileMode
52+ if (compileMode != null ) {
53+ container .setCompileMode (compileMode );
54+ }
55+
56+ // the user may have set a customn CompatVersion
57+ if (compatVersion != null ) {
58+ container .setCompatVersion (compatVersion );
59+ }
60+
4061 // scripts typically need to set the load path for 3rd party gems
4162 List <String > loadPaths = getLoadPaths ();
4263 if (loadPaths != null && !loadPaths .isEmpty ()) {
@@ -73,4 +94,22 @@ protected void prepareContainer(ScriptingContainer container)
7394 // nothing
7495 }
7596
97+ /**
98+ * Allows to customize the {@link CompileMode} used by the JRuby runtime.
99+ */
100+ public T compileMode (CompileMode compileMode )
101+ {
102+ this .compileMode = compileMode ;
103+ return self ();
104+ }
105+
106+ /**
107+ * Allows to customize the {@link CompatVersion} used by the JRuby runtime.
108+ */
109+ public T compatVersion (CompatVersion compatVersion )
110+ {
111+ this .compatVersion = compatVersion ;
112+ return self ();
113+ }
114+
76115}
0 commit comments