Module: JrubyCoercion::Coercable

Included in:
Numeric, String
Defined in:
lib/jruby_coercion/coercable.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jruby_coercion/coercable.rb', line 4

def self.included(klass)
  unless klass.method_defined?(:jruby_default_to_java)
    klass.class_eval do 
      alias_method :jruby_default_to_java, :to_java

      def to_java(java_type = nil)
        converter = ::JrubyCoercion::RubyToJava::Registry.registry_converts_class_and_to?(self.class, java_type)

        if converter
          return converter.call(self)
        else
          jruby_default_to_java(java_type)
        end
      end


      def coerce_to?(java_type)
        return ::JrubyCoercion::RubyToJava::Registry.registry_converts_class_and_to?(self.class, java_type)
      end
      alias_method :coerced_to?, :coerce_to?

    end
  end
end