Java Code Examples for org.jruby.Ruby#getClass()

The following examples show how to use org.jruby.Ruby#getClass() . 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: RubyScriptTest.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Test(enabled = false)
public void resultsCapturesJavaError() throws Exception {
    RubyScript script = new RubyScript(out, err, converToCode(SCRIPT_CONTENTS_ERROR_FROM_JAVA),
            new File(System.getProperty(Constants.PROP_PROJECT_DIR), "dummyfile.rb").getAbsolutePath(), false, null,
            Constants.FRAMEWORK_SWING);
    script.setDriverURL("");
    Ruby interpreter = script.getInterpreter();
    assertTrue("Collector not defined", interpreter.isClassDefined("Collector"));
    RubyClass collectorClass = interpreter.getClass("Collector");
    IRubyObject presult = JavaEmbedUtils.javaToRuby(interpreter, result);
    IRubyObject collector = collectorClass.newInstance(interpreter.getCurrentContext(), new IRubyObject[0], new Block(null));
    IRubyObject rubyObject = interpreter.evalScriptlet("proc { my_function }");
    try {
        collector.callMethod(interpreter.getCurrentContext(), "callprotected", new IRubyObject[] { rubyObject, presult });
    } catch (Throwable t) {

    }
    assertEquals(1, result.failureCount());
    Failure[] failures = result.failures();
    assertTrue("Should end with TestRubyScript.java. but has " + failures[0].getTraceback()[0].fileName,
            failures[0].getTraceback()[0].fileName.endsWith("TestRubyScript.java"));
    assertEquals("throwError", failures[0].getTraceback()[0].functionName);
}
 
Example 2
Source File: ScriptUtils.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * instantiateClass
 * 
 * @param runtime
 * @param name
 * @param args
 * @return
 */
public static IRubyObject instantiateClass(Ruby runtime, String name, IRubyObject... args)
{
	ThreadContext threadContext = runtime.getCurrentContext();
	IRubyObject result = null;

	// try to load the class
	RubyClass rubyClass = runtime.getClass(name);

	// instantiate it, if it exists
	if (rubyClass != null)
	{
		result = rubyClass.newInstance(threadContext, args, Block.NULL_BLOCK);
	}

	return result;
}
 
Example 3
Source File: NodeConverter.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public RubyClass getRubyClass(Ruby runtime) {

            if (path.length == 1) {
                return runtime.getClass(path[0]);
            } else {
                RubyModule object = runtime.getModule(path[0]);

                RubyClass rubyClass = object.getClass(path[1]);

                if (path.length == 2) {
                    return rubyClass;
                } else {
                    return rubyClass.getClass(path[2]);
                }
            }
        }
 
Example 4
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This is a ruby method which takes an array of arguments and constructs a Tuple schema from them. The name
 * will be set automatically.
 *
 * @param context the context the method is being executed in
 * @param self    the RubyClass for the Class object this was invoked on
 * @param arg     a list of arguments to instantiate the new RubySchema
 * @return        the new RubySchema
 */
@JRubyMethod(meta = true, name = {"t", "tuple"})
public static RubySchema tuple(ThreadContext context, IRubyObject self, IRubyObject arg) {
    if (arg instanceof RubyArray) {
        Schema s = rubyArgToSchema(arg);
        Ruby runtime = context.getRuntime();
        return new RubySchema(runtime, runtime.getClass("Schema"), s);
    } else {
        throw new RuntimeException("Bad argument given to Schema.tuple");
    }
}
 
Example 5
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This is a ruby method which takes an array of arguments and constructs a Map schema from them. The name
 * will be set automatically.
 *
 * @param context the context the method is being executed in
 * @param self    the RubyClass for the Class object this was invoked on
 * @param arg     a list of arguments to instantiate the new RubySchema
 * @return        the new RubySchema
 */
@JRubyMethod(meta = true, name = {"m", "map"})
public static RubySchema map(ThreadContext context, IRubyObject self, IRubyObject arg) {
    Schema s = tuple(context, self, arg).getInternalSchema();
    Ruby runtime = context.getRuntime();
    try {
        return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(new Schema.FieldSchema("map_0", s.getField(0).schema, DataType.MAP)));
    } catch (FrontendException e) {
        throw new RuntimeException("Error making map", e);
    }
}
 
Example 6
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This is a ruby method which takes an array of arguments and constructs a Bag schema from them. The name
 * will be set automatically.
 *
 * @param context the context the method is being executed in
 * @param self    the RubyClass for the Class object this was invoked on
 * @param arg     a list of arguments to instantiate the new RubySchema
 * @return        the new RubySchema
 */
@JRubyMethod(meta = true, name = {"b", "bag"})
public static RubySchema bag(ThreadContext context, IRubyObject self, IRubyObject arg) {
    Schema s = tuple(context, self, arg).getInternalSchema();
    Ruby runtime = context.getRuntime();
    try {
        return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(new Schema.FieldSchema("bag_0", s, DataType.BAG)));
    } catch (FrontendException e) {
        throw new RuntimeException("Error making map", e);
    }
}
 
Example 7
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This is a version of [] which allows the range to be specified as such: [1,2].
 *
 * @param context the context the method is being executed in
 * @param arg1    a Fixnum start index
 * @param arg2    a Fixnum end index
 * @return        the RubySchema object encapsulated the found Schema
 */
@JRubyMethod(name = {"[]", "slice"})
public RubySchema get(ThreadContext context, IRubyObject arg1, IRubyObject arg2) {
    if (arg1 instanceof RubyFixnum && arg2 instanceof RubyFixnum) {
        Ruby runtime = context.getRuntime();
        int min = (int)((RubyFixnum)arg1).getLongValue();
        int max = (int)((RubyFixnum)arg2).getLongValue() - 1;
        return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(internalSchema.getFields().subList(min, max + 1)), false);
    } else {
        throw new RuntimeException("Bad arguments given to get function: ( " + arg1.toString() + " , " + arg2.toString()+ " )");
    }
}
 
Example 8
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Given a field name this string will search the RubySchema for a FieldSchema
 * with that name and return it encapsulated in a Schema.
 *
 * @param context the context the method is being executed in
 * @param arg     a RubyString serving as an alias to look
 *                for in the Schema
 * @return        the found RubySchema
 */
@JRubyMethod
public RubySchema find(ThreadContext context, IRubyObject arg) {
    if (arg instanceof RubyString) {
        Ruby runtime = context.getRuntime();
        return new RubySchema(runtime, runtime.getClass("Schema"), RubySchema.find(internalSchema, arg.toString()), false);
    } else {
        throw new RuntimeException("Invalid arguement passed to find: " + arg);
    }
}
 
Example 9
Source File: RubySchema.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This method allows access into the Schema nested in the encapsulated Schema. For example,
 * if the encapsulated Schema is a bag Schema, this allows the user to access the schema of
 * the interior Tuple.
 *
 * @param context the context the method is being executed in
 * @return        a RubySchema encapsulating the nested Schema
 */
@JRubyMethod(name = {"get", "inner", "in"})
public RubySchema get(ThreadContext context) {
    if (internalSchema.size() != 1)
        throw new RuntimeException("Can only return nested schema if there is one schema to get");
    Ruby runtime = context.getRuntime();
    try {
        return new RubySchema(runtime, runtime.getClass("Schema"), internalSchema.getField(0).schema, false);
    } catch (FrontendException e) {
        throw new RuntimeException("Schema does not have a nested FieldScema", e);
    }
}
 
Example 10
Source File: RubyDataBag.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns a copy of the encapsulated DataBag.
 *
 * @param context the context the method is being executed in
 * @return        the copied RubyDataBag
 */
//TODO see if a deepcopy is necessary as well (and consider adding to DataBag and Tuple)
@JRubyMethod
public RubyDataBag clone(ThreadContext context) {
    DataBag b = mBagFactory.newDefaultBag();
    for (Tuple t : this)
        b.add(t);
    Ruby runtime = context.getRuntime();
    return new RubyDataBag(runtime, runtime.getClass("DataBag"), b);
}
 
Example 11
Source File: RubySchema.java    From spork with Apache License 2.0 4 votes vote down vote up
/**
 * @param context the context the method is being executed in
 * @return        a RubySchema copy of the Schema
 */
@JRubyMethod
public RubySchema clone(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    return new RubySchema(runtime, runtime.getClass("Schema"), internalSchema);
}
 
Example 12
Source File: RubyDataByteArray.java    From spork with Apache License 2.0 3 votes vote down vote up
/**
 * This method accepts as an argument anything that could be passed to set (ie a
 * RubyDataByteArray, RubyString, or byte array). It then adds those values together.
 *
 * @param context the context the method is being executed in
 * @param arg     any argument that can validly initialize a RubyDataByteArray
 * @return        a <u>new</u> RubyDataByteArray that is the concatentation of
 *                the encapsulated DataByteArray and arg
 */
@JRubyMethod(name = "+")
public IRubyObject plus(ThreadContext context, IRubyObject arg) {
    Ruby runtime = context.getRuntime();
    RubyDataByteArray toAdd = new RubyDataByteArray(runtime, runtime.getClass("DataByteArray")).initialize(arg);
    return new RubyDataByteArray(runtime, runtime.getClass("DataByteArray"), internalDBA, toAdd.getDBA());
}
 
Example 13
Source File: RubySchema.java    From spork with Apache License 2.0 2 votes vote down vote up
/**
 * This is a helper method to generate a RubySchema of the given type without an alias.
 *
 * @param context the context the method is being executed in
 * @param type    the DataType.PIGTYPE value to make the Schema from
 * @return        a RubySchema object encapsulated a Schema of the specified type
 */
private static RubySchema makeNullAliasRubySchema(ThreadContext context, byte type) {
   Ruby runtime = context.getRuntime();
   return new RubySchema(runtime, runtime.getClass("Schema"), new Schema(new Schema.FieldSchema(null, type)));
}
 
Example 14
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 2 votes vote down vote up
/**
 * A type specific conversion routine.
 *
 * @param ruby   the Ruby runtime to create objects in
 * @param object object to convert
 * @return       analogous Ruby type
 */
public static RubyDataBag pigToRuby(Ruby ruby, DataBag object) {
    return new RubyDataBag(ruby, ruby.getClass("DataBag"), object);
}
 
Example 15
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 2 votes vote down vote up
/**
 * A type specific conversion routine.
 *
 * @param ruby   the Ruby runtime to create objects in
 * @param object object to convert
 * @return       analogous Ruby type
 */
public static RubySchema pigToRuby(Ruby ruby, Schema object) {
    return new RubySchema(ruby, ruby.getClass("Schema"), object);
}
 
Example 16
Source File: PigJrubyLibrary.java    From spork with Apache License 2.0 2 votes vote down vote up
/**
 * A type specific conversion routine.
 *
 * @param ruby   the Ruby runtime to create objects in
 * @param object object to convert
 * @return       analogous Ruby type
 */
public static RubyDataByteArray pigToRuby(Ruby ruby, DataByteArray object) {
    return new RubyDataByteArray(ruby, ruby.getClass("DataByteArray"), object);
}