]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jrubyscripting] don't manually filter presets. (#13548)
authorCody Cutrer <cody@cutrer.us>
Sat, 5 Nov 2022 14:23:02 +0000 (08:23 -0600)
committerGitHub <noreply@github.com>
Sat, 5 Nov 2022 14:23:02 +0000 (15:23 +0100)
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>
bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java

index 326ffbc594b62d20431a82b5aadeb2493b12af8c..5d74d7b2fd2a1fb5d292a7387eb9318fb7b2e11d 100644 (file)
@@ -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");