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

The following examples show how to use org.jruby.RubyHash#put() . 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 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 3
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static RubyHash convertMapToRubyHashWithStrings(Ruby rubyRuntime, Map<String, Object> attributes) {

        if (attributes instanceof RubyHashMapDecorator) {
            return ((RubyHashMapDecorator) attributes).getRubyHash();
        } else if (attributes instanceof RubyHash) {
            return (RubyHash) attributes;
        }

        RubyHash rubyHash = new RubyHash(rubyRuntime);

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

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

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

            RubyString newKey = rubyRuntime.newString(key);
            IRubyObject iRubyValue = toRubyObject(rubyRuntime, value);

            rubyHash.put(newKey, iRubyValue);
        }
        return rubyHash;
    }
 
Example 4
Source File: JRubyRackApplication.java    From rack-servlet with Apache License 2.0 6 votes vote down vote up
private RubyHash convertToRubyHash(Set<Map.Entry<String, Object>> entries) {
  RubyHash hash = newHash(runtime);

  for (Map.Entry<String, Object> entry : entries) {
    String key = entry.getKey();
    Object value = entry.getValue();

    if (key.equals("rack.input")) {
      value = new JRubyRackInput(runtime, (RackInput) value);
    }

    if (key.equals("rack.version")) {
      value = convertToRubyArray((List<Integer>) value);
    }

    hash.put(key, value);
  }

  return hash;
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 4 votes vote down vote up
private static RubyHash addLoggableEvent(final ThreadContext ctx, final IRubyObject rubyEvent, final RubyHash map) {
    map.put("event", rubyEvent.getMetaClass().finvoke(ctx, rubyEvent, "to_hash"));
    return map;
}
 
Example 10
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 4 votes vote down vote up
private static RubyHash createHashInclField(final ThreadContext ctx, final Object fieldValue) {
    final RubyHash hash = createLoggableHash(ctx);
    hash.put("field", fieldValue);
    return hash;
}
 
Example 11
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 4 votes vote down vote up
private void invokeDissection(final ThreadContext ctx, final JrubyEventExtLibrary.RubyEvent rubyEvent, final Event event) {
     // as there can be multiple dissect patterns, any success is a positive metric
    for (final DissectPair dissectPair : dissectors) {
        if (dissectPair.isEmpty()) {
            continue;
        }
        if (!event.includes(dissectPair.javaKey())) {
            invokeFailuresMetric(ctx);
            LOGGER.warn("Dissector mapping, field not found in event", addLoggableEvent(ctx, rubyEvent,
                    createHashInclField(ctx, dissectPair.key())));
            continue;
        }
        // use ruby event here because we want the bytelist bytes
        // from the ruby extract without converting to Java
        final RubyString src = rubyEvent.ruby_get_field(ctx, dissectPair.key()).asString();
        if (src.isNil()) {
            LOGGER.warn("Dissector mapping, no value found for field", addLoggableEvent(ctx, rubyEvent,
                    createHashInclField(ctx, dissectPair.key())));
            invokeFailureTagsAndMetric(ctx, event);
            continue;
        }
        if (src.isEmpty()) {
            invokeFailureTagsAndMetric(ctx, event);
            LOGGER.warn("Dissector mapping, field found in event but it was empty", addLoggableEvent(ctx, rubyEvent,
                    createHashInclField(ctx, dissectPair.key())));
            continue;
        }
        final byte[] bytes = src.getBytes();
        final DissectResult result = dissectPair.dissector().dissect(bytes, event);
        if (result.matched()) {
            if (runMatched) {
                invokeFilterMatched(ctx, rubyEvent);
            }
            invokeMatchesMetric(ctx);
        } else {
            invokeFailureTagsAndMetric(ctx, event);
            final RubyHash loggableMap = createHashInclField(ctx, dissectPair.key());
            loggableMap.put("pattern", dissectPair.dissector().getMapping());
            LOGGER.warn("Dissector mapping, pattern not found", addLoggableEvent(ctx, rubyEvent, loggableMap));
        }
    }
}
 
Example 12
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 13
Source File: RubyHashUtil.java    From asciidoctorj with Apache License 2.0 3 votes vote down vote up
public static RubyHash toNoneSymbolsRubyHash(Ruby rubyRuntime, Map<String, Object> map) {

        RubyHash rubyHash = new RubyHash(rubyRuntime);

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

        for (Entry<String, Object> entry : entrySet) {
            rubyHash.put(toJavaObject(entry.getKey()), toJavaObject(entry.getValue()));
        }

        return rubyHash;

    }