org.jruby.RubySymbol Java Examples

The following examples show how to use org.jruby.RubySymbol. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarathonRuby.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static RubyHash map2hash(Ruby ruby, Map<String, Object> ourCaps) {
    RubyHash hash = new RubyHash(ruby);
    Set<String> keySet = ourCaps.keySet();
    for (String key : keySet) {
        RubySymbol keySym = RubySymbol.newSymbol(ruby, key);
        Object v = ourCaps.get(key);
        if (v instanceof String) {
            hash.put(keySym, RubyString.newString(ruby, (String) v));
        } else if (v instanceof Boolean) {
            hash.put(keySym, RubyBoolean.newBoolean(ruby, (boolean) v));
        } else if (v instanceof List) {
            hash.put(keySym, map2list(ruby, (List<?>) v));
        } else {
            hash.put(keySym, map2hash(ruby, (Map<String, Object>) v));
        }
    }
    return hash;
}
 
Example #2
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * A type specific conversion routine.
 *
 * @param  rbObject object to convert
 * @return          analogous Pig type
 */
@SuppressWarnings("unchecked")
public static Map<String, ?> rubyToPig(RubyHash rbObject) throws ExecException {
    Map<String, Object> newMap = Maps.newHashMap();
    for (Map.Entry<Object, Object> entry : (Set<Map.Entry<Object, Object>>)rbObject.entrySet()) {
        Object key = entry.getKey();

        if (!(key instanceof String || key instanceof RubyString || key instanceof RubySymbol))
            throw new ExecException("Hash must have String or Symbol key. Was given: " + key.getClass().getName());

        String keyStr = key.toString();
        if (entry.getValue() instanceof IRubyObject) {
            newMap.put(keyStr, rubyToPig((IRubyObject)entry.getValue()));
        } else {
            newMap.put(keyStr, entry.getValue());
        }
    }
    return newMap;
}
 
Example #3
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static Map<Object, Object> convertRubyHashMapToMap(Map<Object, Object> rubyHashMap) {

        Map<Object, Object> map = new HashMap<Object, Object>();

        Set<Entry<Object, Object>> elements = rubyHashMap.entrySet();

        for (Entry<Object, Object> element : elements) {
            if(element.getKey() instanceof RubySymbol) {
                map.put(toJavaString((RubySymbol)element.getKey()), toJavaObject(element.getValue()));
            } else {
                map.put(toJavaObject(element.getKey()).toString(), toJavaObject(element.getValue()));
            }
        }

        return map;

    }
 
Example #4
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> convertRubyHashMapToStringObjectMap(RubyHash rubyHashMap) {

        Map<String, Object> map = new HashMap<String, Object>();

        Set<Entry<Object, Object>> elements = rubyHashMap.entrySet();

        for (Entry<Object, Object> element : elements) {
            if(element.getKey() instanceof RubySymbol) {
                map.put(toJavaString((RubySymbol)element.getKey()), toJavaObject(element.getValue()));
            } else if (element.getKey() instanceof RubyString) {
                map.put(((RubyString) element.getKey()).asJavaString(), toJavaObject(element.getValue()));
            } else if (element.getKey() instanceof String) {
                map.put((String) element.getKey(), toJavaObject(element.getValue()));
            }
        }

        return map;

    }
 
Example #5
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> createJavaMap() {
    Map<String, Object> copy = new HashMap<String, Object>();
    Set<Entry<Object, Object>> rubyEntrySet = rubyHash.entrySet();
    for (Map.Entry<Object, Object> o: rubyEntrySet) {
        String key = null;
        Object value;
        Object rubyKey = o.getKey();
        Object rubyValue = o.getValue();
        if (rubyKey instanceof RubySymbol) {
            key = ((RubySymbol) rubyKey).asJavaString();
        } else if (rubyKey instanceof RubyString) {
            key = ((RubyString) rubyKey).asJavaString();
        } else if (rubyKey instanceof String) {
            key = (String) rubyKey;
        } else if (rubyKey instanceof Long) {
            // Skip it silently, it is a positional attribute
        } else {
            throw new IllegalStateException("Did not expect key " + rubyKey + " of type " + rubyKey.getClass());
        }
        if (key != null) {
            value = convertRubyValue(rubyValue);
            copy.put(key, value);
        }
    }
    return copy;
}
 
Example #6
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private Object convertRubyValue(Object rubyValue) {
    if (rubyValue == null) {
        return null;
    } else if (rubyValue instanceof IRubyObject) {
        IRubyObject rubyObject = (IRubyObject) rubyValue;

        if (RubyClass.KindOf.DEFAULT_KIND_OF.isKindOf(rubyObject, getAbstractNodeClass())) {
            return NodeConverter.createASTNode(rubyObject);
        }
        if (rubyObject instanceof RubySymbol) {
            return ":" + rubyObject.asString();
        }
        return JavaEmbedUtils.rubyToJava((IRubyObject) rubyValue);
    } else {
        return rubyValue;
    }
}
 
Example #7
Source File: DocumentImpl.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Title getStructuredDoctitle() {
    Ruby runtime = getRubyObject().getRuntime();
    RubyHash options = RubyHash.newHash(runtime);
    RubySymbol partitioned = RubySymbol.newSymbol(runtime, "partition");
    options.put(partitioned, RubyBoolean.newBoolean(runtime, true));

    Object doctitle = getRubyProperty("doctitle", options);

    return toJava((IRubyObject) doctitle, Title.class);

}
 
Example #8
Source File: RubyObjectWrapper.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public String getString(String propertyName, Object... args) {
    IRubyObject result = getRubyProperty(propertyName, args);

    if (result instanceof RubyNil) {
        return null;
    } else if (result instanceof RubySymbol) {
        return result.asJavaString();
    } else {
        return result.asJavaString();
    }
}
 
Example #9
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsKey(Object key) {
    if (!(key instanceof String)) {
        return false;
    }
    RubySymbol symbol = rubyHash.getRuntime().getSymbolTable().getSymbol((String) key);
    return rubyHash.containsKey(symbol);
}
 
Example #10
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(Object key) {
    if (!(key instanceof String)) {
        return false;
    }
    RubySymbol symbol = rubyHash.getRuntime().getSymbolTable().getSymbol((String) key);
    Object value = rubyHash.get(symbol);
    return convertRubyValue(value);
}
 
Example #11
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object put(String key, Object value) {
    Object oldValue = get(key);
    RubySymbol symbol = rubyHash.getRuntime().getSymbolTable().getSymbol(key);
    rubyHash.put(symbol, convertJavaValue(value));
    return oldValue;
}
 
Example #12
Source File: RubyHashMapDecorator.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object remove(Object key) {
    if (!(key instanceof String)) {
        return null;
    }
    Object oldValue = get(key);
    RubySymbol symbol = rubyHash.getRuntime().getSymbolTable().getSymbol((String) key);
    rubyHash.remove(symbol);
    return convertRubyValue(oldValue);
}
 
Example #13
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private RubySymbol getRubySymbol(Ruby ruby, String s) {
    return ruby.newSymbol(s.startsWith(":") ? s.substring(1) : s);
}
 
Example #14
Source File: RubyUtils.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public static RubySymbol toSymbol(Ruby rubyRuntime, String key) {
    RubySymbol newSymbol = RubySymbol.newSymbol(rubyRuntime, key);
    return newSymbol;
}
 
Example #15
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public static RubyHash convertMapToRubyHashWithSymbols(Ruby rubyRuntime, Map<String, Object> options) {

        if (options instanceof RubyHashMapDecorator) {
            return ((RubyHashMapDecorator) options).getRubyHash();
        }

        RubyHash rubyHash = new RubyHash(rubyRuntime);

        Set<Entry<String, Object>> optionsSet = options.entrySet();

        for (Entry<String, Object> entry : optionsSet) {

            String key = entry.getKey();
            Object value = entry.getValue();

            RubySymbol newSymbol = RubyUtils.toSymbol(rubyRuntime, key);
            IRubyObject iRubyValue = toRubyObject(rubyRuntime, value);

            rubyHash.put(newSymbol, iRubyValue);

        }

        return rubyHash;

    }
 
Example #16
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private static String toJavaString(RubySymbol element) {
    return element.asJavaString();
}