jruby-coercion 0.0.1-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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jruby-coercion.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,29 @@
1
+ # Jruby::Coercion
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
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ desc "Run specs (default)"
8
+ task :default => :spec
@@ -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 = Jruby::Coercion::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,62 @@
1
+ require "jruby-coercion/version"
2
+ require 'thread'
3
+ require "java"
4
+
5
+ module Jruby
6
+ module Coercion
7
+
8
+ module Coercable
9
+ def to_java(java_type = nil)
10
+ return super if java_type.nil?
11
+ converter = ::Jruby::Coercion::Registry.registry_converts_class_and_to?(self.class, java_type)
12
+
13
+ if converter
14
+ return converter.call(self, java_type)
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
20
+
21
+ class Registry
22
+ class << self
23
+ attr_reader :converter_registry
24
+ end
25
+
26
+ @converter_registry ||= Java::JavaUtilConcurrent::ConcurrentHashMap.new
27
+
28
+ def self.register_converter(ruby_type, java_type, callable = nil, &blk)
29
+ Thread.exclusive do
30
+ callable ||= blk
31
+ ruby_type_converter = (@converter_registry[ruby_type] ||=
32
+ ::Java::JavaUtilConcurrent::ConcurrentHashMap.new)
33
+
34
+ ruby_type_converter[java_type] = callable
35
+ @converter_registry[ruby_type] = ruby_type_converter
36
+ end
37
+ end
38
+
39
+ def self.registry_converts_class?(ruby_type)
40
+ if !ruby_type.nil? && (class_level = converter_registry[ruby_type])
41
+ return class_level
42
+ else
43
+ false
44
+ end
45
+ end
46
+
47
+ def self.registry_converts_class_and_to?(ruby_type, java_type)
48
+ if (!java_type.nil? &&
49
+ (class_level = registry_converts_class?(ruby_type)) &&
50
+ (converter = class_level[java_type]))
51
+
52
+ return converter
53
+ else
54
+ false
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+
62
+ require 'jruby-coercion/numeric'
@@ -0,0 +1,31 @@
1
+ require 'jruby-coercion'
2
+
3
+ class Numeric
4
+ include ::Jruby::Coercion::Coercable
5
+ end
6
+
7
+ # java.math.BigDecimal
8
+ ::Jruby::Coercion::Registry.register_converter(Numeric, java.math.BigDecimal) do |numeric, java_type|
9
+ java_type.new(numeric)
10
+ end
11
+
12
+ ::Jruby::Coercion::Registry.register_converter(Integer, java.math.BigDecimal) do |integer, java_type|
13
+ java_type.new(integer)
14
+ end
15
+
16
+ ::Jruby::Coercion::Registry.register_converter(Fixnum, java.math.BigDecimal) do |fixnum, java_type|
17
+ java_type.new(fixnum)
18
+ end
19
+
20
+ # java.math.BigInteger
21
+ ::Jruby::Coercion::Registry.register_converter(Numeric, java.math.BigInteger) do |numeric, java_type|
22
+ java_type.new("#{numeric}")
23
+ end
24
+
25
+ ::Jruby::Coercion::Registry.register_converter(Integer, java.math.BigInteger) do |integer, java_type|
26
+ java_type.new("#{integer}")
27
+ end
28
+
29
+ ::Jruby::Coercion::Registry.register_converter(Fixnum, java.math.BigInteger) do |fixnum, java_type|
30
+ java_type.new("#{fixnum}")
31
+ end
@@ -0,0 +1,5 @@
1
+ module Jruby
2
+ module Coercion
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
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
+ end
15
+ end
16
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require(:default, :development, :test)
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jruby-coercion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: java
7
+ authors:
8
+ - Brandon Dewitt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-06 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/numeric.rb
83
+ - lib/jruby-coercion/version.rb
84
+ - spec/coercions/big_decimal_spec.rb
85
+ - spec/coercions/big_integer_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: !binary |-
100
+ MA==
101
+ hash: 2
102
+ none: false
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: !binary |-
110
+ MA==
111
+ hash: 2
112
+ none: false
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: helps coerce ruby objects into java type automatically
119
+ test_files:
120
+ - spec/coercions/big_decimal_spec.rb
121
+ - spec/coercions/big_integer_spec.rb
122
+ - spec/spec_helper.rb