Java Code Examples for org.jruby.RubyModule#defineClassUnder()

The following examples show how to use org.jruby.RubyModule#defineClassUnder() . 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: 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 2
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 3
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 4
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 5
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;
}