package org.bson;

import java.io.IOException;

import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.runtime.load.BasicLibraryService;

/**
 * The native implementation of various extensions.
 *
 * @since 2.0.0
 */
public class NativeService implements BasicLibraryService {

  /**
   * Constant for the BSON module name.
   *
   * @since 2.0.0
   */
  private final String BSON = "BSON".intern();

  /**
   * Loads the native extension into the JRuby runtime.
   *
   * Example: service.basicLoad(ruby);
   *
   * @param runtime The Ruby runtime.
   *
   * @return Always returns true if no exception.
   *
   * @since 2.0.0
   */
  public boolean basicLoad(Ruby runtime) throws IOException {
    RubyModule bson = runtime.fastGetModule(BSON);
    new IntegerExtender(bson).define();
    return true;
  }

  /**
   * Provides native extensions around integer operations.
   *
   * @since 2.0.0
   */
  private class IntegerExtender {

    /**
     * Constant for the Integer module name.
     *
     * @since 2.0.0
     */
    private final String INTEGER = "Integer".intern();

    /**
     * The service's integer module to operate on.
     *
     * @since 2.0.0
     */
    private RubyModule integer;

    /**
     * Instantiate a new integer extender.
     *
     * @param bson The parent BSON module.
     *
     * @since 2.0.0.
     */
    private IntegerExtender(final RubyModule bson) {
      this.integer = bson.defineOrGetModuleUnder(INTEGER);
    }

    /**
     * Load the method definitions into the integer module.
     *
     * @return True if the loading was successful.
     *
     * @since 2.0.0.
     */
    public boolean define() {
      return true;
    }
  }
}

@@ -778,18 +778,21 @@
 xten
-der
+sion
 (bson).
+re
 defi
@@ -941,19 +941,20 @@
 gerExten
-der
+sion
  %7B%0A%0A    
@@ -1381,19 +1381,20 @@
 gerExten
-der
+sion
 (final R
@@ -1556,66 +1556,8 @@
   *%0A
-     * @return True if the loading was successful.%0A     *%0A
     
@@ -1592,24 +1592,23 @@
  public 
-boolean 
+void re
 define()
@@ -1612,35 +1612,78 @@
 e() %7B%0A      
-return true
+integer.defineAnnotatedMethods(IntegerExtension.class)
 ;%0A    %7D%0A  %7D%0A

Define annotated methods in redefine