Skip to content

Commit 147154b

Browse files
committed
Expose some JRuby runtime configuration parameters
1 parent 6667139 commit 147154b

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎transform-markup/src/main/java/org/ocpsoft/rewrite/transform/markup/JRubyTransformer.java‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.List;
44

5+
import org.jruby.CompatVersion;
6+
import org.jruby.RubyInstanceConfig.CompileMode;
57
import org.jruby.embed.LocalContextScope;
68
import org.jruby.embed.LocalVariableBehavior;
79
import org.jruby.embed.ScriptingContainer;
@@ -13,9 +15,13 @@
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
}

‎transform-markup/src/main/java/org/ocpsoft/rewrite/transform/markup/Markdown.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.jruby.embed.ScriptingContainer;
2222

23-
public class Markdown extends JRubyTransformer
23+
public class Markdown extends JRubyTransformer<Markdown>
2424
{
2525

2626
private final static String SCRIPT = "require 'maruku'\n" +
@@ -64,4 +64,10 @@ public Object runScript(ScriptingContainer container)
6464
return container.runScriptlet(SCRIPT);
6565
}
6666

67+
@Override
68+
public Markdown self()
69+
{
70+
return this;
71+
}
72+
6773
}

‎transform-markup/src/main/java/org/ocpsoft/rewrite/transform/markup/Sass.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.jruby.embed.ScriptingContainer;
2222

23-
public class Sass extends JRubyTransformer
23+
public class Sass extends JRubyTransformer<Sass>
2424
{
2525

2626
private static final String SCRIPT = "require 'sass'\n" +
@@ -49,4 +49,10 @@ public Object runScript(ScriptingContainer container)
4949
return container.runScriptlet(SCRIPT);
5050
}
5151

52+
@Override
53+
public Sass self()
54+
{
55+
return this;
56+
}
57+
5258
}

‎transform-markup/src/main/java/org/ocpsoft/rewrite/transform/markup/Textile.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.jruby.embed.ScriptingContainer;
2222

23-
public class Textile extends JRubyTransformer
23+
public class Textile extends JRubyTransformer<Textile>
2424
{
2525

2626
private static final String SCRIPT = "require 'redcloth'\n" +
@@ -83,4 +83,10 @@ public Object runScript(ScriptingContainer container)
8383

8484
}
8585

86+
@Override
87+
public Textile self()
88+
{
89+
return this;
90+
}
91+
8692
}

0 commit comments

Comments
 (0)