Java Code Examples for org.jruby.RubyHash#newHash()

The following examples show how to use org.jruby.RubyHash#newHash() . 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: PigJrubyLibrary.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * A type specific conversion routine for Pig Maps. This only deals with maps
 * with String keys, which is all that Pig supports.
 *
 * @param  ruby          the Ruby runtime to create objects in
 * @param  object        map to convert. In Pig, only maps with String keys are
 *                       supported
 * @return               analogous Ruby type
 * @throws ExecException object contains a key that can't be convert to a Ruby type
 */
public static <T> RubyHash pigToRuby(Ruby ruby, Map<T, ?> object) throws ExecException {
    RubyHash newMap = RubyHash.newHash(ruby);

    boolean checkType = false;

    for (Map.Entry<T, ?> entry : object.entrySet()) {
        T key = entry.getKey();
        if (!checkType) {
            if (!(key instanceof String))
                throw new ExecException("pigToRuby only supports converting Maps with String keys");
            checkType = true;
        }
        newMap.put(key, pigToRuby(ruby, entry.getValue()));
    }

    return newMap;
}
 
Example 2
Source File: AbstractProcessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private static void handleDefaultAttributesAnnotation(Class<? extends Processor> processor, RubyClass rubyClass) {
    Ruby rubyRuntime = rubyClass.getRuntime();
    if (processor.isAnnotationPresent(DefaultAttributes.class)) {
        DefaultAttributes defaultAttributes = processor.getAnnotation(DefaultAttributes.class);
        RubyHash defaultAttrs = RubyHash.newHash(rubyRuntime);
        for (DefaultAttribute defaultAttribute : defaultAttributes.value()) {
            defaultAttrs.put(defaultAttribute.key(), defaultAttribute.value());
        }
        rubyClass.callMethod(rubyRuntime.getCurrentContext(), "option", new IRubyObject[]{
                rubyRuntime.newSymbol("default_attrs"),
                defaultAttrs
        });
    }
}
 
Example 3
Source File: AbstractProcessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private static void handleDefaultAttributeAnnotation(Class<? extends Processor> processor, RubyClass rubyClass) {
    Ruby rubyRuntime = rubyClass.getRuntime();
    if (processor.isAnnotationPresent(DefaultAttribute.class)) {
        DefaultAttribute defaultAttribute = processor.getAnnotation(DefaultAttribute.class);
        RubyHash defaultAttrs = RubyHash.newHash(rubyRuntime);
        defaultAttrs.put(defaultAttribute.key(), defaultAttribute.value());
        rubyClass.callMethod(rubyRuntime.getCurrentContext(), "option", new IRubyObject[]{
                rubyRuntime.newSymbol("default_attrs"),
                defaultAttrs
        });
    }
}
 
Example 4
Source File: PreprocessorReaderImpl.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public void push_include(String data, String file, String path, int lineNumber, Map<String, Object> attributes) {

    RubyHash attributeHash = RubyHash.newHash(getRuntime());
    attributeHash.putAll(attributes);

    getRubyProperty("push_include", data, file, path, lineNumber, attributes);
}
 
Example 5
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Table createTable(StructuralNode parent, Map<String, Object> attributes) {
    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    RubyHash rubyAttributes = RubyHash.newHash(rubyRuntime);
    rubyAttributes.putAll(attributes);

    IRubyObject[] parameters = {
            ((StructuralNodeImpl) parent).getRubyObject(),
            rubyAttributes};
    Table ret = (Table) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.TABLE_CLASS, parameters);
    ret.setAttribute("rowcount", 0, false);
    return ret;
}
 
Example 6
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Column createTableColumn(Table parent, int index, Map<String, Object> attributes) {
    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    RubyHash rubyAttributes = RubyHash.newHash(rubyRuntime);
    rubyAttributes.putAll(attributes);

    IRubyObject[] parameters = {
            ((StructuralNodeImpl) parent).getRubyObject(),
            RubyFixnum.newFixnum(rubyRuntime, index),
            rubyAttributes}; // No cursor parameter yet

    return (Column) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.TABLE_COLUMN_CLASS, parameters);
}
 
Example 7
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Cell createTableCell(Column column, String text, Map<String, Object> attributes) {
    Ruby rubyRuntime = JRubyRuntimeContext.get(column);

    RubyHash rubyAttributes = RubyHash.newHash(rubyRuntime);
    rubyAttributes.putAll(attributes);

    IRubyObject[] parameters = {
            ((ColumnImpl) column).getRubyObject(),
            text != null ? rubyRuntime.newString(text) : rubyRuntime.getNil(),
            rubyAttributes}; // No cursor parameter yet

    return (Cell) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.TABLE_CELL_CLASS, parameters);
}
 
Example 8
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an inner document for the given parent document.
 * Inner documents are used for tables cells with style {@code asciidoc}.
 *
 * @param parentDocument The parent document of the new document.
 * @return A new inner document.
 */
@Override
public Document createDocument(Document parentDocument) {
    Ruby runtime = JRubyRuntimeContext.get(parentDocument);
    RubyHash options = RubyHash.newHash(runtime);
    options.put(
            runtime.newSymbol("parent"),
            ((DocumentImpl) parentDocument).getRubyObject());

    return (Document) NodeConverter.createASTNode(runtime, NodeConverter.NodeType.DOCUMENT_CLASS, runtime.getNil(), options);
}
 
Example 9
Source File: NodeCache.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static NodeCache get(IRubyObject rubyObject) {

        RubyHash cache = (RubyHash) rubyObject.getInstanceVariables().getInstanceVariable(ASCIIDOCTORJ_CACHE);
        if (cache == null) {
            cache = RubyHash.newHash(rubyObject.getRuntime());
            rubyObject.getInstanceVariables().setInstanceVariable(ASCIIDOCTORJ_CACHE, cache);
        }
        return new NodeCache(cache);
    }
 
Example 10
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);

}