jruby_coercion 0.0.3-java → 0.0.4-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  require "java"
2
2
  require "jruby_coercion/version"
3
3
  require "jruby_coercion/converter"
4
+ require "jruby_coercion/coercable"
4
5
 
5
6
  module JrubyCoercion
6
7
 
@@ -14,4 +15,5 @@ module JrubyCoercion
14
15
 
15
16
  end
16
17
 
17
- require 'jruby_coercion/numeric'
18
+ require "jruby_coercion/ruby_to_java/registry"
19
+ require "jruby_coercion/numeric"
@@ -1,5 +1,3 @@
1
- require 'jruby_coercion/ruby_to_java/registry'
2
-
3
1
  module JrubyCoercion
4
2
  module Coercable
5
3
 
@@ -1,4 +1,6 @@
1
+ require 'bigdecimal'
1
2
  require 'jruby_coercion/registry'
3
+ require "jruby_coercion/coercable"
2
4
 
3
5
  module ::JrubyCoercion::RubyToJava
4
6
 
@@ -6,6 +8,17 @@ module ::JrubyCoercion::RubyToJava
6
8
 
7
9
  DEFAULT_CONVERTER = lambda { |val| val.jruby_default_to_java }
8
10
 
11
+ # Setup a default type mapping to correctly respond to #coerce_to?
12
+ # calls and register the default mapping
13
+ DEFAULT_TYPE_MAP = {
14
+ Fixnum => java.lang.Long,
15
+ TrueClass => java.lang.Boolean,
16
+ FalseClass => java.lang.Boolean,
17
+ Float => java.lang.Double,
18
+ BigDecimal => java.math.BigDecimal,
19
+ String => java.lang.String
20
+ }
21
+
9
22
  ##
10
23
  # Override for new_registry_entry_for_type in RubyToJava
11
24
  # that calls jruby default to_java when nil is the to_type
@@ -13,9 +26,22 @@ module ::JrubyCoercion::RubyToJava
13
26
  def self.new_registry_entry_for_type(from_type)
14
27
  new_type_registry = Java::JavaUtilConcurrent::ConcurrentHashMap.new
15
28
  new_type_registry[::JrubyCoercion::Registry::DEFAULT_KEY] = DEFAULT_CONVERTER
29
+
30
+ if (mapped_to = DEFAULT_TYPE_MAP[from_type])
31
+ new_type_registry[mapped_to] = DEFAULT_CONVERTER
32
+ end
16
33
 
17
34
  return new_type_registry
18
35
  end
19
36
  end
20
37
 
21
38
  end
39
+
40
+ # Setup default mappings for jruby
41
+ ::JrubyCoercion::RubyToJava::Registry::DEFAULT_TYPE_MAP.each do |ruby_type, java_type|
42
+ ruby_type.__send__(:include, ::JrubyCoercion::Coercable)
43
+
44
+ ::JrubyCoercion::RubyToJava::Registry.register_converter(ruby_type,
45
+ java_type,
46
+ ::JrubyCoercion::RubyToJava::Registry::DEFAULT_CONVERTER)
47
+ end
@@ -1,3 +1,3 @@
1
1
  module JrubyCoercion
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'bigdecimal'
3
+ require 'jruby_coercion/numeric'
4
+
5
+ describe Java::JavaMath::BigInteger do
6
+ context "Numeric" do
7
+ [
8
+ [3, 3],
9
+ [Integer(3), 3],
10
+ [-100, -100],
11
+ [0, 0]
12
+ ].each do |test_numeric, constructor_arg|
13
+ specify { test_numeric.to_java(described_class).should eq(described_class.new("#{constructor_arg}")) }
14
+ specify { test_numeric.should be_coerced_to(described_class) }
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jruby_coercion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: java
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake