jruby_coercion 0.0.3-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.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/jruby_coercion.gemspec +24 -0
- data/lib/jruby_coercion.rb +17 -0
- data/lib/jruby_coercion/coercable.rb +32 -0
- data/lib/jruby_coercion/converter.rb +30 -0
- data/lib/jruby_coercion/java_to_ruby/registry.rb +21 -0
- data/lib/jruby_coercion/numeric.rb +33 -0
- data/lib/jruby_coercion/registry.rb +71 -0
- data/lib/jruby_coercion/ruby_to_java/registry.rb +21 -0
- data/lib/jruby_coercion/version.rb +3 -0
- data/spec/coercions/big_decimal_spec.rb +17 -0
- data/spec/coercions/big_integer_spec.rb +0 -0
- data/spec/spec_helper.rb +4 -0
- metadata +127 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Brandon Dewitt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# JrubyCoercion
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'jruby_coercion'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install jruby_coercion
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jruby_coercion/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "jruby_coercion"
|
8
|
+
gem.version = JrubyCoercion::VERSION
|
9
|
+
gem.authors = ["Brandon Dewitt"]
|
10
|
+
gem.email = ["brandonsdewitt+jrubycoercion@gmail.com"]
|
11
|
+
gem.description = %q{ gem to facilitate automatic coercion between jruby/java objects }
|
12
|
+
gem.summary = %q{ helps coerce ruby objects into java type automatically }
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.platform = "java"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
gem.add_development_dependency "rspec"
|
23
|
+
gem.add_development_dependency "pry-nav"
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "java"
|
2
|
+
require "jruby_coercion/version"
|
3
|
+
require "jruby_coercion/converter"
|
4
|
+
|
5
|
+
module JrubyCoercion
|
6
|
+
|
7
|
+
def self.native_type?(java_type)
|
8
|
+
java_type.class == Java::JavaClass
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.ruby_type?(java_type)
|
12
|
+
java_type.class == Class
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'jruby_coercion/numeric'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'jruby_coercion/ruby_to_java/registry'
|
2
|
+
|
3
|
+
module JrubyCoercion
|
4
|
+
module Coercable
|
5
|
+
|
6
|
+
def self.included(klass)
|
7
|
+
unless klass.method_defined?(:jruby_default_to_java)
|
8
|
+
klass.class_eval do
|
9
|
+
alias_method :jruby_default_to_java, :to_java
|
10
|
+
|
11
|
+
def to_java(java_type = nil)
|
12
|
+
converter = ::JrubyCoercion::RubyToJava::Registry.registry_converts_class_and_to?(self.class, java_type)
|
13
|
+
|
14
|
+
if converter
|
15
|
+
return converter.call(self)
|
16
|
+
else
|
17
|
+
jruby_default_to_java(java_type)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def coerce_to?(java_type)
|
23
|
+
return ::JrubyCoercion::RubyToJava::Registry.registry_converts_class_and_to?(self.class, java_type)
|
24
|
+
end
|
25
|
+
alias_method :coerced_to?, :coerce_to?
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module JrubyCoercion
|
5
|
+
|
6
|
+
class Converter < SimpleDelegator
|
7
|
+
extend Forwardable
|
8
|
+
def_delegator :@converter, :call
|
9
|
+
|
10
|
+
attr_reader :converter
|
11
|
+
|
12
|
+
##
|
13
|
+
# Constructor
|
14
|
+
#
|
15
|
+
def initialize(converter)
|
16
|
+
@converter = converter
|
17
|
+
super(!!@converter)
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Public Instance Methods
|
22
|
+
#
|
23
|
+
def to_ary
|
24
|
+
[ self, @converter ]
|
25
|
+
end
|
26
|
+
|
27
|
+
alias_method :coerce, :call
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# require 'jruby_coercion/registry'
|
2
|
+
#
|
3
|
+
# module ::JrubyCoercion::JavaToRuby
|
4
|
+
#
|
5
|
+
# class Registry < ::JrubyCoercion::Registry
|
6
|
+
#
|
7
|
+
# DEFAULT_CONVERTER = lambda { |val| val }
|
8
|
+
#
|
9
|
+
# ##
|
10
|
+
# # Override for new_registry_entry_for_type in JavaToRuby
|
11
|
+
# # that calls
|
12
|
+
# #
|
13
|
+
# def self.new_registry_entry_for_type(from_type)
|
14
|
+
# new_type_registry = Java::JavaUtilConcurrent::ConcurrentHashMap.new
|
15
|
+
# new_type_registry[::JrubyCoercion::Registry::DEFAULT_KEY] = DEFAULT_CONVERTER
|
16
|
+
#
|
17
|
+
# return new_type_registry
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'jruby_coercion'
|
2
|
+
require 'jruby_coercion/coercable'
|
3
|
+
require 'jruby_coercion/ruby_to_java/registry'
|
4
|
+
|
5
|
+
class Numeric
|
6
|
+
include ::JrubyCoercion::Coercable
|
7
|
+
end
|
8
|
+
|
9
|
+
# java.math.BigDecimal
|
10
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Numeric, java.math.BigDecimal) do |numeric|
|
11
|
+
java.math.BigDecimal.new(numeric)
|
12
|
+
end
|
13
|
+
|
14
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Integer, java.math.BigDecimal) do |integer|
|
15
|
+
java.math.BigDecimal.new(integer)
|
16
|
+
end
|
17
|
+
|
18
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Fixnum, java.math.BigDecimal) do |fixnum|
|
19
|
+
java.math.BigDecimal.new(fixnum)
|
20
|
+
end
|
21
|
+
|
22
|
+
# java.math.BigInteger
|
23
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Numeric, java.math.BigInteger) do |numeric|
|
24
|
+
java.math.BigInteger.new("#{numeric}")
|
25
|
+
end
|
26
|
+
|
27
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Integer, java.math.BigInteger) do |integer|
|
28
|
+
java.math.BigInteger.new("#{integer}")
|
29
|
+
end
|
30
|
+
|
31
|
+
::JrubyCoercion::RubyToJava::Registry.register_converter(Fixnum, java.math.BigInteger) do |fixnum|
|
32
|
+
java.math.BigInteger.new("#{fixnum}")
|
33
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'jruby_coercion'
|
3
|
+
require 'jruby_coercion/converter'
|
4
|
+
|
5
|
+
class ::JrubyCoercion::Registry
|
6
|
+
|
7
|
+
DEFAULT_KEY = "JRUBY_COERCION_DEFAULT".freeze
|
8
|
+
|
9
|
+
##
|
10
|
+
# Class Methods
|
11
|
+
#
|
12
|
+
def self.alternative_class(java_type)
|
13
|
+
if ::JrubyCoercion.native_type?(java_type)
|
14
|
+
return java_type.ruby_class
|
15
|
+
else
|
16
|
+
return java_type.java_class
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.converter_registry
|
21
|
+
@converter_registry ||= Java::JavaUtilConcurrent::ConcurrentHashMap.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.new_registry_entry_for_type(from_type)
|
25
|
+
raise "new_registry_entry_for_type must be overridden in child classes"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.register_converter(from_type, to_type, callable = nil, &blk)
|
29
|
+
Thread.exclusive do
|
30
|
+
callable ||= blk
|
31
|
+
to_type ||= DEFAULT_KEY
|
32
|
+
|
33
|
+
from_type_converter = (converter_registry[from_type] ||= new_registry_entry_for_type(from_type))
|
34
|
+
|
35
|
+
from_type_converter[to_type] = callable
|
36
|
+
# Register alternative type java_class/ruby_class
|
37
|
+
from_type_converter[self.alternative_class(to_type)] = callable unless to_type == DEFAULT_KEY
|
38
|
+
|
39
|
+
converter_registry[from_type] = from_type_converter
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.registry_converts_class?(convert_type)
|
44
|
+
if !convert_type.nil? && (class_level = converter_registry[convert_type])
|
45
|
+
return class_level
|
46
|
+
else
|
47
|
+
false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.registry_converts_class_and_to?(from_type, to_type)
|
52
|
+
if (class_level = registry_converts_class?(from_type))
|
53
|
+
to_type ||= DEFAULT_KEY
|
54
|
+
return ::JrubyCoercion::Converter.new(class_level[to_type])
|
55
|
+
else
|
56
|
+
return ::JrubyCoercion::Converter.new(false)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Class Aliases
|
62
|
+
#
|
63
|
+
class << self
|
64
|
+
alias_method :register_conversion, :register_converter
|
65
|
+
alias_method :register_coercion, :register_converter
|
66
|
+
alias_method :registry_converts_type?, :registry_converts_class?
|
67
|
+
alias_method :registry_converts_type_and_to?, :registry_converts_class_and_to?
|
68
|
+
alias_method :registry_for_type_and_to, :registry_converts_type_and_to?
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'jruby_coercion/registry'
|
2
|
+
|
3
|
+
module ::JrubyCoercion::RubyToJava
|
4
|
+
|
5
|
+
class Registry < ::JrubyCoercion::Registry
|
6
|
+
|
7
|
+
DEFAULT_CONVERTER = lambda { |val| val.jruby_default_to_java }
|
8
|
+
|
9
|
+
##
|
10
|
+
# Override for new_registry_entry_for_type in RubyToJava
|
11
|
+
# that calls jruby default to_java when nil is the to_type
|
12
|
+
#
|
13
|
+
def self.new_registry_entry_for_type(from_type)
|
14
|
+
new_type_registry = Java::JavaUtilConcurrent::ConcurrentHashMap.new
|
15
|
+
new_type_registry[::JrubyCoercion::Registry::DEFAULT_KEY] = DEFAULT_CONVERTER
|
16
|
+
|
17
|
+
return new_type_registry
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'jruby_coercion/numeric'
|
4
|
+
|
5
|
+
describe Java::JavaMath::BigDecimal 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
|
File without changes
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jruby_coercion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Brandon Dewitt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: !binary |-
|
21
|
+
MA==
|
22
|
+
none: false
|
23
|
+
requirement: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: !binary |-
|
28
|
+
MA==
|
29
|
+
none: false
|
30
|
+
prerelease: false
|
31
|
+
type: :development
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rspec
|
34
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: !binary |-
|
39
|
+
MA==
|
40
|
+
none: false
|
41
|
+
requirement: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: !binary |-
|
46
|
+
MA==
|
47
|
+
none: false
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: pry-nav
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: !binary |-
|
57
|
+
MA==
|
58
|
+
none: false
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: !binary |-
|
64
|
+
MA==
|
65
|
+
none: false
|
66
|
+
prerelease: false
|
67
|
+
type: :development
|
68
|
+
description: ! ' gem to facilitate automatic coercion between jruby/java objects '
|
69
|
+
email:
|
70
|
+
- brandonsdewitt+jrubycoercion@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- jruby_coercion.gemspec
|
81
|
+
- lib/jruby_coercion.rb
|
82
|
+
- lib/jruby_coercion/coercable.rb
|
83
|
+
- lib/jruby_coercion/converter.rb
|
84
|
+
- lib/jruby_coercion/java_to_ruby/registry.rb
|
85
|
+
- lib/jruby_coercion/numeric.rb
|
86
|
+
- lib/jruby_coercion/registry.rb
|
87
|
+
- lib/jruby_coercion/ruby_to_java/registry.rb
|
88
|
+
- lib/jruby_coercion/version.rb
|
89
|
+
- spec/coercions/big_decimal_spec.rb
|
90
|
+
- spec/coercions/big_integer_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: ''
|
93
|
+
licenses: []
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: !binary |-
|
105
|
+
MA==
|
106
|
+
hash: 2
|
107
|
+
none: false
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: !binary |-
|
115
|
+
MA==
|
116
|
+
hash: 2
|
117
|
+
none: false
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.8.24
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: helps coerce ruby objects into java type automatically
|
124
|
+
test_files:
|
125
|
+
- spec/coercions/big_decimal_spec.rb
|
126
|
+
- spec/coercions/big_integer_spec.rb
|
127
|
+
- spec/spec_helper.rb
|