jruby-scala 0.1.0
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/.document +5 -0
- data/.gitignore +24 -0
- data/LICENSE +25 -0
- data/README.rdoc +29 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/jruby_scala.rb +12 -0
- data/lib/jruby_scala/core_ext.rb +4 -0
- data/lib/jruby_scala/core_ext/java.rb +3 -0
- data/lib/jruby_scala/core_ext/module.rb +5 -0
- data/lib/jruby_scala/core_ext/module/traits.rb +102 -0
- data/lib/jruby_scala/core_ext/operator_translations.rb +131 -0
- data/lib/jruby_scala/core_ext/proc.rb +5 -0
- data/lib/jruby_scala/core_ext/proc/conversions.rb +19 -0
- data/lib/jruby_scala/core_ext/proc/scala_function.rb +25 -0
- data/spec/jruby_scala/core_ext/module/traits_spec.rb +70 -0
- data/spec/jruby_scala/core_ext/operator_translations_spec.rb +39 -0
- data/spec/jruby_scala/core_ext/proc/conversions_spec.rb +17 -0
- data/spec/jruby_scala/core_ext/proc/scala_function_spec.rb +44 -0
- data/spec/jruby_scala_spec.rb +11 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +8 -0
- metadata +101 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2010, System Insights, Inc.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
3. Neither the name of System Insights, Inc. nor the names of
|
13
|
+
its contributors may be used to endorse or promote products derived from
|
14
|
+
this software without specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= jruby-scala
|
2
|
+
|
3
|
+
Scala integration/interop library for JRuby.
|
4
|
+
|
5
|
+
Much of the code for this library is originally from an article written by
|
6
|
+
Daniel Spiewak:
|
7
|
+
|
8
|
+
http://www.codecommit.com/blog/ruby/integrating-scala-into-jruby
|
9
|
+
|
10
|
+
many thanks to Daniel for his great article and implementation.
|
11
|
+
|
12
|
+
== Note on Patches/Pull Requests
|
13
|
+
|
14
|
+
* Fork the project.
|
15
|
+
* Make your feature addition or bug fix.
|
16
|
+
* Add tests for it. This is important so I don't break it in a
|
17
|
+
future version unintentionally.
|
18
|
+
* Commit, do not mess with rakefile, version, or history.
|
19
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
20
|
+
* Send me a pull request. Bonus points for topic branches.
|
21
|
+
|
22
|
+
== Copyright
|
23
|
+
|
24
|
+
Copyright (c) 2010 System Insights, Inc. See LICENSE for details.
|
25
|
+
|
26
|
+
This work also contains code derived from public sources.
|
27
|
+
|
28
|
+
Portions Copyright (c) 2010 Daniel Spiewak
|
29
|
+
All rights reserved.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jruby-scala"
|
8
|
+
gem.summary = %Q{A Scala integration library for JRuby}
|
9
|
+
gem.description = %Q{A Scala integration library for JRuby that allows using Procs as Scala functions, including Scala traits into Ruby modules, and more.}
|
10
|
+
gem.email = "ingersoll@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/wemrysi/jruby-scala"
|
12
|
+
gem.authors = ["Emrys Ingersoll"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "yard", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
44
|
+
end
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/jruby_scala.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'jruby_scala/core_ext/operator_translations'
|
3
|
+
|
4
|
+
module JrubyScala
|
5
|
+
module CoreExt
|
6
|
+
module Module
|
7
|
+
# Enables the inclusion of Scala traits into Ruby modules.
|
8
|
+
#
|
9
|
+
# TODO: Currently, JRuby throws an exception when attempting to load
|
10
|
+
# many of the top-level traits: List, Iterator, Equiv, Ordered, etc.
|
11
|
+
# Oddly enough, scala.FunctionN works fine. We need to investigate
|
12
|
+
# why this happens.
|
13
|
+
#
|
14
|
+
module Traits
|
15
|
+
def self.included(base)
|
16
|
+
base.module_eval do
|
17
|
+
alias_method :include_without_scala_trait_support, :include
|
18
|
+
alias_method :include, :include_with_scala_trait_support
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Mixes the given modules into the current module, including Scala
|
23
|
+
# Traits.
|
24
|
+
def include_with_scala_trait_support(*modules)
|
25
|
+
modules.each do |m|
|
26
|
+
if m.respond_to?(:java_class) and m.java_class.interface?
|
27
|
+
loader = m.java_class.class_loader
|
28
|
+
unless loader.nil?
|
29
|
+
mixin_methods_from_trait(loader.load_class(m.java_class.to_s), loader)
|
30
|
+
unless self < JrubyScala::CoreExt::OperatorTranslations
|
31
|
+
include_without_scala_trait_support(JrubyScala::CoreExt::OperatorTranslations)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if defined?(@@trait_methods)
|
37
|
+
define_method(:scala_reflective_trait_methods) {@@trait_methods}
|
38
|
+
end
|
39
|
+
|
40
|
+
include_without_scala_trait_support(m)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Mixes the methods from a Scala trait into the module.
|
45
|
+
#
|
46
|
+
# @param trait_class The trait to mixin
|
47
|
+
# @param [java.lang.ClassLoader] loader The Java ClassLoader for the trait
|
48
|
+
# @param [Set] done The set of traits that have already been mixed in,
|
49
|
+
# for memoization purposes
|
50
|
+
def mixin_methods_from_trait(trait_class, loader, done = Set.new)
|
51
|
+
return if done.include?(trait_class)
|
52
|
+
done << trait_class
|
53
|
+
|
54
|
+
begin
|
55
|
+
klass = loader.load_class("#{trait_class.name}$class")
|
56
|
+
rescue java.lang.ClassNotFoundException
|
57
|
+
# Unable to load the <TraitName>$class, probably because this is
|
58
|
+
# a standard Java interface, not a Scala trait, so just return.
|
59
|
+
return
|
60
|
+
end
|
61
|
+
|
62
|
+
# TODO: Should this happen before attempting to load the special
|
63
|
+
# <TraitName>$class?
|
64
|
+
trait_class.interfaces.each do |iface|
|
65
|
+
mixin_methods_from_trait(iface, loader, done)
|
66
|
+
end
|
67
|
+
|
68
|
+
klass.declared_methods.each do |meth|
|
69
|
+
mods = meth.modifiers
|
70
|
+
|
71
|
+
if java.lang.reflect.Modifier.static?(mods) and java.lang.reflect.Modifier.public?(mods)
|
72
|
+
@@trait_methods ||= []
|
73
|
+
|
74
|
+
unless meth.name.include?('$')
|
75
|
+
module_eval <<-CODE
|
76
|
+
def #{meth.name}(*args, &block)
|
77
|
+
args.insert(0, self)
|
78
|
+
args << block unless block.nil?
|
79
|
+
args.map! {|a| defined?(a.java_object) ? a.java_object : a}
|
80
|
+
scala_reflective_trait_methods[#{@@trait_methods.size}].invoke(nil, args.to_java)
|
81
|
+
end
|
82
|
+
CODE
|
83
|
+
|
84
|
+
@@trait_methods << meth
|
85
|
+
else
|
86
|
+
# Method name contains special characters, so use a fallback
|
87
|
+
# implementation with define_method.
|
88
|
+
#
|
89
|
+
# Caveat: This impl can't deal with methods with function
|
90
|
+
# arguments (blocks)
|
91
|
+
define_method(meth.name) do |*args|
|
92
|
+
args.insert(0, self)
|
93
|
+
meth.invoke(nil, args.to_java)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module JrubyScala
|
2
|
+
module CoreExt
|
3
|
+
# Provides translations between the standard set of Ruby operators and their
|
4
|
+
# Scala counterparts allowing the natural use of Ruby operators on Scala
|
5
|
+
# objects.
|
6
|
+
#
|
7
|
+
# Currently, translates the ruby-style array/hash access operator #[] and
|
8
|
+
# handles Proc methods on objects that extend scala.FunctionN. Other
|
9
|
+
# operators may be added as necessary.
|
10
|
+
#
|
11
|
+
# @example Ruby-style hash access
|
12
|
+
# h = scala.collection.immutable.HashMap.new
|
13
|
+
# h = h.update(0, 4)
|
14
|
+
# puts h[0] --> 4
|
15
|
+
#
|
16
|
+
module OperatorTranslations
|
17
|
+
# A mapping of ruby operators to their respective method invocations in
|
18
|
+
# Scala
|
19
|
+
OPERATORS = {
|
20
|
+
"=" => "$eq",
|
21
|
+
">" => "$greater",
|
22
|
+
"<" => "$less",
|
23
|
+
"+" => "$plus",
|
24
|
+
"-" => "$minus",
|
25
|
+
"*" => "$times",
|
26
|
+
"/" => "div",
|
27
|
+
"!" => "$bang",
|
28
|
+
"@" => "$at",
|
29
|
+
"#" => "$hash",
|
30
|
+
"%" => "$percent",
|
31
|
+
"^" => "$up",
|
32
|
+
"&" => "$amp",
|
33
|
+
"~" => "$tilde",
|
34
|
+
"?" => "$qmark",
|
35
|
+
"|" => "$bar",
|
36
|
+
"\\" => "$bslash"}
|
37
|
+
|
38
|
+
def self.included(base)
|
39
|
+
alias_method :method_missing_without_scala_operator_translations, :method_missing
|
40
|
+
alias_method :method_missing, :method_missing_with_scala_operator_translations
|
41
|
+
end
|
42
|
+
|
43
|
+
def method_missing_with_scala_operator_translations(sym, *args)
|
44
|
+
if sym == :[] or (sym == :call and type_of_scala_function?(self))
|
45
|
+
apply(*args)
|
46
|
+
elsif sym == :arity and (ar = type_of_scala_function?(self))
|
47
|
+
ar
|
48
|
+
elsif sym == :binding and type_of_scala_function?(self)
|
49
|
+
binding
|
50
|
+
elsif sym == :to_proc and type_of_scala_function?(self)
|
51
|
+
self
|
52
|
+
else
|
53
|
+
method_missing_without_scala_operator_translations(sym, *args)
|
54
|
+
=begin
|
55
|
+
str = sym.to_s
|
56
|
+
str = $&[1] + '_=' if str =~ /^(.*[^\]=])=$/
|
57
|
+
OPERATORS.each {|from, to| str.gsub!(from, to)}
|
58
|
+
|
59
|
+
if respond_to?(str)
|
60
|
+
send(str.to_sym, *args)
|
61
|
+
else
|
62
|
+
method_missing_without_scala_operator_translations(sym, *args)
|
63
|
+
end
|
64
|
+
=end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Predicate returning whether the given object extends one of the Scala
|
71
|
+
# FunctionN traits.
|
72
|
+
#
|
73
|
+
# @param [Object] An object to interrogate
|
74
|
+
#
|
75
|
+
# @return [Fixnum or FalseClass] The arity if the object is a function or
|
76
|
+
# false if not
|
77
|
+
#
|
78
|
+
def type_of_scala_function?(obj)
|
79
|
+
if obj.java_kind_of? scala.Function0
|
80
|
+
0
|
81
|
+
elsif obj.java_kind_of? scala.Function1
|
82
|
+
1
|
83
|
+
elsif obj.java_kind_of? scala.Function2
|
84
|
+
2
|
85
|
+
elsif obj.java_kind_of? scala.Function3
|
86
|
+
3
|
87
|
+
elsif obj.java_kind_of? scala.Function4
|
88
|
+
4
|
89
|
+
elsif obj.java_kind_of? scala.Function5
|
90
|
+
5
|
91
|
+
elsif obj.java_kind_of? scala.Function6
|
92
|
+
6
|
93
|
+
elsif obj.java_kind_of? scala.Function7
|
94
|
+
7
|
95
|
+
elsif obj.java_kind_of? scala.Function8
|
96
|
+
8
|
97
|
+
elsif obj.java_kind_of? scala.Function9
|
98
|
+
9
|
99
|
+
elsif obj.java_kind_of? scala.Function10
|
100
|
+
10
|
101
|
+
elsif obj.java_kind_of? scala.Function11
|
102
|
+
11
|
103
|
+
elsif obj.java_kind_of? scala.Function12
|
104
|
+
12
|
105
|
+
elsif obj.java_kind_of? scala.Function13
|
106
|
+
13
|
107
|
+
elsif obj.java_kind_of? scala.Function14
|
108
|
+
14
|
109
|
+
elsif obj.java_kind_of? scala.Function15
|
110
|
+
15
|
111
|
+
elsif obj.java_kind_of? scala.Function16
|
112
|
+
16
|
113
|
+
elsif obj.java_kind_of? scala.Function17
|
114
|
+
17
|
115
|
+
elsif obj.java_kind_of? scala.Function18
|
116
|
+
18
|
117
|
+
elsif obj.java_kind_of? scala.Function19
|
118
|
+
19
|
119
|
+
elsif obj.java_kind_of? scala.Function20
|
120
|
+
20
|
121
|
+
elsif obj.java_kind_of? scala.Function21
|
122
|
+
21
|
123
|
+
elsif obj.java_kind_of? scala.Function22
|
124
|
+
22
|
125
|
+
else
|
126
|
+
false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'jruby_scala/core_ext/proc/scala_function'
|
2
|
+
|
3
|
+
module JrubyScala
|
4
|
+
module CoreExt
|
5
|
+
module Proc
|
6
|
+
module Conversions
|
7
|
+
# Converts a Proc object into a Scala function of the same arity
|
8
|
+
#
|
9
|
+
# @return [JrubyScala::CoreExt::Proc::Function] A Scala-compatible proc
|
10
|
+
def to_function
|
11
|
+
eval "Function#{arity}.new(self)"
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :java_object, :to_function
|
15
|
+
alias_method :scala_object, :to_function
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module JrubyScala
|
2
|
+
module CoreExt
|
3
|
+
module Proc
|
4
|
+
class ScalaFunction
|
5
|
+
def initialize(delegate)
|
6
|
+
@delegate = delegate
|
7
|
+
end
|
8
|
+
|
9
|
+
def apply(*args)
|
10
|
+
@delegate.call(*args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
SCALA_MAX_ARITY = 22
|
15
|
+
|
16
|
+
for n in 0..SCALA_MAX_ARITY
|
17
|
+
eval <<-CLASS
|
18
|
+
class Function#{n} < ScalaFunction
|
19
|
+
include scala.Function#{n}
|
20
|
+
end
|
21
|
+
CLASS
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
include JrubyScala::CoreExt::Module
|
3
|
+
|
4
|
+
describe "JrubyScala::CoreExt::Module::Traits" do
|
5
|
+
describe "when included into a module" do
|
6
|
+
it "should still allow normal Java interfaces to be mixed-in" do
|
7
|
+
class Car
|
8
|
+
include java.lang.Comparable
|
9
|
+
|
10
|
+
attr_reader :speed
|
11
|
+
|
12
|
+
def initialize(speed)
|
13
|
+
@speed = speed
|
14
|
+
end
|
15
|
+
|
16
|
+
def compare_to(other)
|
17
|
+
speed <=> other.speed
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
pinto = Car.new(45)
|
22
|
+
porsche = Car.new(180)
|
23
|
+
veyron = Car.new(240)
|
24
|
+
|
25
|
+
cars = java.util.TreeSet.new
|
26
|
+
cars.add(porsche)
|
27
|
+
cars.add(pinto)
|
28
|
+
cars.add(veyron)
|
29
|
+
|
30
|
+
cars.first.should == pinto
|
31
|
+
cars.lower(veyron).should == porsche
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "when a Scala trait is included" do
|
35
|
+
before(:all) do
|
36
|
+
class MySet
|
37
|
+
include scala.collection.Set
|
38
|
+
|
39
|
+
def initialize(*elements)
|
40
|
+
@elements = elements
|
41
|
+
end
|
42
|
+
|
43
|
+
def contains(elem)
|
44
|
+
@elements.include?(elem)
|
45
|
+
end
|
46
|
+
|
47
|
+
def size
|
48
|
+
@elements.size
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
@s1 = MySet.new(1, 2, 3, 4, 5, 6, 7, 8)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should mix-in defined methods on Scala traits" do
|
56
|
+
@s1.should respond_to(:isEmpty)
|
57
|
+
@s1.isEmpty.should be_false
|
58
|
+
MySet.new.isEmpty.should be_true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should mix-in defined methods on super traits" do
|
62
|
+
@s1.should respond_to(:compose)
|
63
|
+
f = lambda {|x| x + 3}.to_function
|
64
|
+
g = @s1.compose(f)
|
65
|
+
g.apply(3).should be_true
|
66
|
+
g.apply(7).should be_false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe "JrubyScala::CoreExt::OperatorTranslations" do
|
4
|
+
before(:all) do
|
5
|
+
@h = scala.collection.immutable.HashMap.new
|
6
|
+
@h = @h.update(0, 5)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should translate #[] to #apply" do
|
10
|
+
@h.should_receive(:apply).with(0)
|
11
|
+
@h[0]
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "when an object extends a Scala FunctionN" do
|
15
|
+
it "should translate #call to #apply" do
|
16
|
+
@h.should_receive(:apply).with(0)
|
17
|
+
@h.call(0)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "arity method" do
|
21
|
+
it "should return the arity of the extended FunctionN trait" do
|
22
|
+
@h.should be_a_java_kind_of(scala.Function1)
|
23
|
+
@h.arity.should == 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "binding method" do
|
28
|
+
it "should expose the private binding method" do
|
29
|
+
lambda {@h.binding}.should_not raise_error(NoMethodError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "to_proc method" do
|
34
|
+
it "should return the object itself" do
|
35
|
+
@h.to_proc.should == @h
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
include JrubyScala::CoreExt::Proc
|
3
|
+
|
4
|
+
describe "JrubyScala::CoreExt::Proc::Conversion" do
|
5
|
+
describe "to_function" do
|
6
|
+
it "should return an instance of FunctionN of the proper arity" do
|
7
|
+
l2 = lambda {|x, y|}
|
8
|
+
l3 = lambda {|x, y, z|}
|
9
|
+
|
10
|
+
f2 = l2.to_function
|
11
|
+
f2.should be_an_instance_of(Function2)
|
12
|
+
|
13
|
+
f3 = l3.to_function
|
14
|
+
f3.should be_an_instance_of(Function3)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
include JrubyScala::CoreExt::Proc
|
3
|
+
|
4
|
+
describe "JrubyScala::CoreExt::Proc::ScalaFunction" do
|
5
|
+
describe "apply" do
|
6
|
+
it "should apply the delegate object to the passed arguments" do
|
7
|
+
d1 = lambda {|x| x * 3}
|
8
|
+
d2 = lambda {|x| "#{x} red balloons"}
|
9
|
+
|
10
|
+
arg = 4
|
11
|
+
|
12
|
+
sf1 = ScalaFunction.new(d1)
|
13
|
+
sf1.apply(arg).should == 12
|
14
|
+
|
15
|
+
sf2 = ScalaFunction.new(d2)
|
16
|
+
sf2.apply(arg).should == "4 red balloons"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should define a FunctionN class for N = 0..SCALA_MAX_ARITY" do
|
21
|
+
proc_ext = JrubyScala::CoreExt::Proc
|
22
|
+
module Scala
|
23
|
+
include_package('scala')
|
24
|
+
end
|
25
|
+
|
26
|
+
(0..SCALA_MAX_ARITY).each do |arity|
|
27
|
+
klass = proc_ext.const_get("Function#{arity}")
|
28
|
+
f = klass.new(lambda {})
|
29
|
+
f.should be_a_kind_of(ScalaFunction)
|
30
|
+
f.should be_a_kind_of(Scala.const_get("Function#{arity}"))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "FunctionN" do
|
35
|
+
it "should be usable as a Scala Function" do
|
36
|
+
sum = Function2.new(lambda {|i, tuple| i + tuple._2})
|
37
|
+
hm = scala.collection.immutable.HashMap.new
|
38
|
+
hm = hm.update('a', 1)
|
39
|
+
hm = hm.update('b', 2)
|
40
|
+
hm = hm.update('c', 3)
|
41
|
+
hm.fold_left(0, sum).should == 6
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "JrubyScala" do
|
4
|
+
before(:all) do
|
5
|
+
@hmap = Java::ScalaCollectionImmutable::HashMap
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should provide a top-level java-like namespace for scala elements" do
|
9
|
+
scala.collection.immutable.HashMap.should == @hmap
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jruby-scala
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emrys Ingersoll
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-19 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: A Scala integration library for JRuby that allows using Procs as Scala functions, including Scala traits into Ruby modules, and more.
|
36
|
+
email: ingersoll@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/jruby_scala.rb
|
52
|
+
- lib/jruby_scala/core_ext.rb
|
53
|
+
- lib/jruby_scala/core_ext/java.rb
|
54
|
+
- lib/jruby_scala/core_ext/module.rb
|
55
|
+
- lib/jruby_scala/core_ext/module/traits.rb
|
56
|
+
- lib/jruby_scala/core_ext/operator_translations.rb
|
57
|
+
- lib/jruby_scala/core_ext/proc.rb
|
58
|
+
- lib/jruby_scala/core_ext/proc/conversions.rb
|
59
|
+
- lib/jruby_scala/core_ext/proc/scala_function.rb
|
60
|
+
- spec/jruby_scala/core_ext/module/traits_spec.rb
|
61
|
+
- spec/jruby_scala/core_ext/operator_translations_spec.rb
|
62
|
+
- spec/jruby_scala/core_ext/proc/conversions_spec.rb
|
63
|
+
- spec/jruby_scala/core_ext/proc/scala_function_spec.rb
|
64
|
+
- spec/jruby_scala_spec.rb
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/wemrysi/jruby-scala
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: "0"
|
87
|
+
version:
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A Scala integration library for JRuby
|
95
|
+
test_files:
|
96
|
+
- spec/jruby_scala_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/jruby_scala/core_ext/operator_translations_spec.rb
|
99
|
+
- spec/jruby_scala/core_ext/module/traits_spec.rb
|
100
|
+
- spec/jruby_scala/core_ext/proc/conversions_spec.rb
|
101
|
+
- spec/jruby_scala/core_ext/proc/scala_function_spec.rb
|