org.jruby.RubyModule Java Examples

The following examples show how to use org.jruby.RubyModule. 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: TextAreaReadline.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Hooks this <code>TextAreaReadline</code> instance into the runtime,
 * redefining the <code>Readline</code> module so that it uses this object.
 * This method does not redefine the standard input-output streams. If you
 * need that, use {@link #hookIntoRuntimeWithStreams(Ruby)}.
 *
 * @param runtime The runtime.
 * @see #hookIntoRuntimeWithStreams(Ruby)
 */
public void hookIntoRuntime(final Ruby runtime) {
    this.runtime = runtime;
    /* Hack in to replace usual readline with this */
    runtime.getLoadService().require("readline");
    RubyModule readlineM = runtime.fastGetModule("Readline");

    readlineM.defineModuleFunction("readline", new Callback() {
        public IRubyObject execute(IRubyObject recv, IRubyObject[] args,
                                   Block block) {
            String line = readLine(args[0].toString());
            if (line != null) {
                return RubyString.newUnicodeString(runtime, line);
            } else {
                return runtime.getNil();
            }
        }

        public Arity getArity() {
            return Arity.twoArguments();
        }
    });
}
 
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 module
 * @param name
 * @param args
 * @return
 */
public static IRubyObject instantiateClass(Ruby runtime, String module, String name, IRubyObject... args)
{
	ThreadContext threadContext = runtime.getCurrentContext();
	IRubyObject result = null;

	// try to load the module
	RubyModule rubyModule = runtime.getModule(module);

	if (rubyModule != null)
	{
		// now try to load the class
		RubyClass rubyClass = rubyModule.getClass(name);

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

	return result;
}
 
Example #3
Source File: RubySchema.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * This method registers the class with the given runtime.
 *
 * @param runtime an instance of the Ruby runtime
 * @return        a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    RubyClass result = runtime.defineClass("Schema",runtime.getObject(), ALLOCATOR);

    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubySchema;
        }
    };

    result.includeModule(runtime.getEnumerable());

    result.defineAnnotatedMethods(RubySchema.class);

    return result;
}
 
Example #4
Source File: RubyDataBag.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * This method registers the class with the given runtime. It is not necessary to do this here,
 * but it is simpler to associate the methods necessary to register the class with the class
 * itself, so on the Library side it is possible to just specify "RubyDataBag.define(runtime)".
 *
 * @param runtime an instance of the Ruby runtime
 * @return        a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    // This generates the class object associated with DataBag, and registers it with the
    // runtime. The RubyClass object has all the metadata associated with a Class itself.
    RubyClass result = runtime.defineClass("DataBag", runtime.getObject(), ALLOCATOR);

    // This registers a method which can be used to know whether a module is an
    // instance of the class.
    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyDataBag;
        }
    };

    // This includes the Enumerable module that we specified.
    result.includeModule(runtime.getEnumerable());

    // This method actually reads the annotations we placed and registers
    // all of the methods.
    result.defineAnnotatedMethods(RubyDataBag.class);

    // This returns the RubyClass object with all the new metadata.
    return result;
}
 
Example #5
Source File: ConverterProxy.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static <U, T  extends Converter<U> & OutputFormatWriter<U>> RubyClass register(JRubyAsciidoctor asciidoctor, final Class<T> converterClass) {
    Ruby rubyRuntime = asciidoctor.getRubyRuntime();
    RubyModule module = rubyRuntime.defineModule(getModuleName(converterClass));
    RubyClass clazz = module.defineClassUnder(
            converterClass.getSimpleName(),
            rubyRuntime.getObject(),
            new ConverterProxy.Allocator(converterClass, asciidoctor));
    includeModule(clazz, "Asciidoctor", "Converter");
    includeModule(clazz, "Asciidoctor", "Converter", "BackendTraits");

    clazz.defineAnnotatedMethod(ConverterProxy.class, "initialize");
    clazz.defineAnnotatedMethod(ConverterProxy.class, "convert");

    includeModule(clazz, "Asciidoctor", "Writer");
    clazz.defineAnnotatedMethod(ConverterProxy.class, "write");

    //clazz.defineAnnotatedMethods(ConverterProxy.class);
    return clazz;
}
 
Example #6
Source File: RubyOutputStreamWrapper.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
public static RubyClass getOrCreateOutputStreamWrapperClass(final Ruby rubyRuntime) {
  RubyModule asciidoctorModule = rubyRuntime.getModule("AsciidoctorJ");
  RubyClass outputStreamWrapperClass = asciidoctorModule.getClass(RUBY_CLASS_NAME);
  if (outputStreamWrapperClass != null) {
    return outputStreamWrapperClass;
  }

  final RubyClass rubyClass = asciidoctorModule.defineClassUnder(RUBY_CLASS_NAME, rubyRuntime.getObject(), new ObjectAllocator() {
    @Override
    public IRubyObject allocate(final Ruby runtime, final RubyClass klazz) {
      return new RubyOutputStreamWrapper(runtime, klazz);
    }
  });

  rubyClass.defineAnnotatedMethods(RubyOutputStreamWrapper.class);

  return rubyClass;
}
 
Example #7
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 #8
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 5 votes vote down vote up
@Override
public final void load(final Ruby runtime, final boolean wrap) {
    final RubyModule module = runtime.defineModule("LogStash");

    final RubyClass clazz = runtime.defineClassUnder("Dissector", runtime.getObject(), JavaDissectorLibrary.RubyDissect::new, module);
    clazz.defineAnnotatedMethods(JavaDissectorLibrary.RubyDissect.class);

    final RubyClass runtimeError = runtime.getRuntimeError();
    module.defineClassUnder("FieldFormatError", runtimeError, runtimeError.getAllocator());
    module.defineClassUnder("ConvertDatatypeFormatError", runtimeError, runtimeError.getAllocator());
}
 
Example #9
Source File: JRubyRackInput.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
private static RubyClass getRackInputClass(Ruby runtime) {
  RubyModule module = runtime.getOrCreateModule("RackServlet");
  RubyClass klass = module.getClass("RackInput");
  if (klass == null) {
    klass = module.defineClassUnder("RackInput", runtime.getObject(), ALLOCATOR);
    klass.defineAnnotatedMethods(JRubyRackInput.class);
  }
  return klass;
}
 
Example #10
Source File: SyntaxHighlighterProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private static void includeModule(RubyClass clazz, String moduleName, String... moduleNames) {
  RubyModule module = clazz.getRuntime().getModule(moduleName);
  if (moduleNames != null && moduleNames.length > 0) {
    for (String submoduleName : moduleNames) {
      module = module.getModule(submoduleName);
    }
  }
  clazz.includeModule(module);
}
 
Example #11
Source File: SyntaxHighlighterProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static <T extends SyntaxHighlighterAdapter> RubyClass register(JRubyAsciidoctor asciidoctor, final Class<T> highlighterClass) {
  Ruby rubyRuntime = asciidoctor.getRubyRuntime();
  RubyModule module = rubyRuntime.defineModule(getModuleName(highlighterClass));

  RubyClass syntaxHighlighterBase = rubyRuntime.getModule("Asciidoctor")
      .getModule("SyntaxHighlighter")
      .getClass("Base");

  RubyClass clazz = module.defineClassUnder(
      highlighterClass.getSimpleName(),
      syntaxHighlighterBase,
      new SyntaxHighlighterProxy.Allocator(highlighterClass, asciidoctor));

  clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "initialize");
  clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "hasDocInfo");
  clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "getDocInfo");

  if (StylesheetWriter.class.isAssignableFrom(highlighterClass)) {
    clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "isWriteStylesheet");
    clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "writeStylesheet");
  }

  if (Highlighter.class.isAssignableFrom(highlighterClass)) {
    clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "isHighlight");
    clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "highlight");
  }

  if (Formatter.class.isAssignableFrom(highlighterClass)) {
    clazz.defineAnnotatedMethod(SyntaxHighlighterProxy.class, "format");
  }

  return clazz;
}
 
Example #12
Source File: ConverterProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private static void includeModule(RubyClass clazz, String moduleName, String... moduleNames) {
    RubyModule module = clazz.getRuntime().getModule(moduleName);
    if (moduleNames != null && moduleNames.length > 0) {
        for (String submoduleName: moduleNames) {
            module = module.getModule(submoduleName);
        }
    }
    clazz.includeModule(module);
}
 
Example #13
Source File: RubyDataByteArray.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the DataByteArray class with the Ruby runtime.
 *
 * @param runtime an instance of the Ruby runtime
 * @return a RubyClass object with metadata about the registered class
 */
public static RubyClass define(Ruby runtime) {
    RubyClass result = runtime.defineClass("DataByteArray", runtime.getObject(), ALLOCATOR);

    result.kindOf = new RubyModule.KindOf() {
        public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyDataByteArray;
        }
    };

    result.defineAnnotatedMethods(RubyDataByteArray.class);

    return result;
}
 
Example #14
Source File: RubyExtensionRegistryImpl.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private RubyModule getAsciidoctorModule() {
    return rubyRuntime.getModule("AsciidoctorModule");
}
 
Example #15
Source File: JavaConverterRegistryImpl.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private RubyModule getConverterFactory() {
    return rubyRuntime.getModule("Asciidoctor")
        .getModule("Converter");
}
 
Example #16
Source File: SyntaxHighlighterRegistryImpl.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private RubyModule getSyntaxHighlighterFactory() {
    return rubyRuntime.getModule("Asciidoctor")
        .getModule("SyntaxHighlighter");
}
 
Example #17
Source File: JavaExtensionRegistryImpl.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private RubyModule getAsciidoctorModule() {
    return rubyRuntime.getModule("AsciidoctorModule");
}
 
Example #18
Source File: ProcessorProxyUtil.java    From asciidoctorj with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the Ruby module Asciidoc::Extensions.
 * @param rubyRuntime
 * @return The Ruby object for the module Asciidoc::Extensions.
 */
private static RubyModule getExtensionsModule(Ruby rubyRuntime) {
    RubyModule asciidoctorModule = rubyRuntime.getModule("Asciidoctor");
    return asciidoctorModule.defineOrGetModuleUnder("Extensions");
}
 
Example #19
Source File: ProcessorProxyUtil.java    From asciidoctorj with Apache License 2.0 2 votes vote down vote up
/**
 * For a simple Ruby class name like "Treeprocessor" it returns the associated RubyClass
 * from Asciidoctor::Extensions, e.g. Asciidoctor::Extensions::Treeprocessor
 * @param rubyRuntime
 * @param processorClassName
 * @return The Ruby class object for the given extension class name, e.g. Asciidoctor::Extensions::TreeProcessor
 */
public static RubyClass getExtensionBaseClass(Ruby rubyRuntime, String processorClassName) {
    RubyModule extensionsModule = getExtensionsModule(rubyRuntime);
    return extensionsModule.getClass(processorClassName);
}