[jrubyscripting] don't manually filter presets. (#13548)
simply just don't overwrite any constants that already exist refs https://github.com/openhab/openhab-core/pull/3113 Signed-off-by: Cody Cutrer <cody@cutrer.us>pull/13659/head
parent
c81790cebf
commit
bbc744e3ff
|
@ -15,7 +15,6 @@ package org.openhab.automation.jrubyscripting.internal;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -47,26 +46,12 @@ public class JRubyScriptEngineFactory extends AbstractScriptEngineFactory {
|
|||
|
||||
private final JRubyScriptEngineConfiguration configuration = new JRubyScriptEngineConfiguration();
|
||||
|
||||
// Filter out the File entry to prevent shadowing the Ruby File class which breaks Ruby in spectacularly
|
||||
// difficult ways to debug.
|
||||
private static final Set<String> FILTERED_PRESETS = Set.of("File", "Files", "Path", "Paths");
|
||||
private static final Set<String> INSTANCE_PRESETS = Set.of();
|
||||
|
||||
private final javax.script.ScriptEngineFactory factory = new org.jruby.embed.jsr223.JRubyEngineFactory();
|
||||
|
||||
private final List<String> scriptTypes = Stream
|
||||
.concat(factory.getExtensions().stream(), factory.getMimeTypes().stream())
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
|
||||
// Adds @ in front of a set of variables so that Ruby recognizes them as instance variables
|
||||
private static Map.Entry<String, Object> mapInstancePresets(Map.Entry<String, Object> entry) {
|
||||
if (INSTANCE_PRESETS.contains(entry.getKey())) {
|
||||
return Map.entry("@" + entry.getKey(), entry.getValue());
|
||||
} else {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
// Adds $ in front of a set of variables so that Ruby recognizes them as global variables
|
||||
private static Map.Entry<String, Object> mapGlobalPresets(Map.Entry<String, Object> entry) {
|
||||
if (Character.isLowerCase(entry.getKey().charAt(0)) && !(entry.getValue() instanceof Class)
|
||||
|
@ -102,8 +87,6 @@ public class JRubyScriptEngineFactory extends AbstractScriptEngineFactory {
|
|||
scopeValues //
|
||||
.entrySet() //
|
||||
.stream() //
|
||||
.filter(map -> !FILTERED_PRESETS.contains(map.getKey())) //
|
||||
.map(JRubyScriptEngineFactory::mapInstancePresets) //
|
||||
.map(JRubyScriptEngineFactory::mapGlobalPresets) //
|
||||
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); //
|
||||
|
||||
|
@ -126,7 +109,7 @@ public class JRubyScriptEngineFactory extends AbstractScriptEngineFactory {
|
|||
private void importClassesToRuby(ScriptEngine scriptEngine, Map<String, Object> objects) {
|
||||
try {
|
||||
scriptEngine.put("__classes", objects);
|
||||
final String code = "__classes.each { |(name, klass)| Object.const_set(name, klass.ruby_class) }";
|
||||
final String code = "__classes.each { |(name, klass)| Object.const_set(name, klass.ruby_class) unless Object.const_defined?(name, false) }";
|
||||
scriptEngine.eval(code);
|
||||
// clean up our temporary variable
|
||||
scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).remove("__classes");
|
||||
|
|
Loading…
Reference in New Issue