jtor-stdlib 0.1.0-java → 0.1.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.
- checksums.yaml +4 -4
- data/lib/jtor-stdlib/base.rb +28 -30
- data/lib/jtor-stdlib/jtor_import.rb +12 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30f6a9eaeea71b4b9ed16ebf62ea7c995ed5b6f0
|
4
|
+
data.tar.gz: 510159c2e64ef1cbc76635a7fba3c204bc143361
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32fc80c2e1a9cd631845d8104e33d15b8360b60e36cee35ffca6f6b687116dc26f5736d242bf0120e577199465cd31361279de43ec24c2e35234634fdb970292
|
7
|
+
data.tar.gz: d187b0b8837f0e94729d9f2c61fd4495c99c65a504d442c283a4b66b4de9c331e58410dcd60de054ac2e62bc7683ea0f12e75a667612c2d9b6cd456b08f3e329
|
data/lib/jtor-stdlib/base.rb
CHANGED
@@ -1,44 +1,42 @@
|
|
1
1
|
module Jtor
|
2
2
|
module StdLib
|
3
|
-
|
3
|
+
module Base
|
4
4
|
@@_lookup = {}
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
define_method(name) { |*args| self.class.lookup(name, *args) }
|
13
|
-
end
|
6
|
+
# Method overloading is not a ruby thing, so we implement a pseudo-lookup
|
7
|
+
# mechanism for method based on the type/count of their args
|
8
|
+
def add_java_method(name, param_types, &block)
|
9
|
+
add_method_to_lookup(name, param_types, &block)
|
10
|
+
unless method_defined?(name)
|
11
|
+
define_method(name) { |*args| self.class.lookup(name, *args) }
|
14
12
|
end
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
15
|
+
def lookup(name, *args)
|
16
|
+
if @@_lookup[name]
|
17
|
+
@@_lookup[name].each do |param_types, method|
|
18
|
+
# NOTE: This is an oversimplification of the way Java determines
|
19
|
+
# which method to invoke. I may work further into this later (see
|
20
|
+
# http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12)
|
21
|
+
next unless params_match(param_types, args)
|
22
|
+
return method.call(*args)
|
25
23
|
end
|
26
|
-
method_missing(name, *args)
|
27
24
|
end
|
25
|
+
method_missing(name, *args)
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
true
|
28
|
+
def params_match(types, values)
|
29
|
+
# TODO: Handle var args (...)
|
30
|
+
types.each_with_index do |type, index|
|
31
|
+
value = values[index]
|
32
|
+
return false unless value.is_a?(type) || value.to_java.is_a?(type)
|
36
33
|
end
|
34
|
+
true
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
37
|
+
def add_method_to_lookup(name, param_types, &block)
|
38
|
+
@@_lookup[name] ||= {}
|
39
|
+
@@_lookup[name][param_types] = block
|
42
40
|
end
|
43
41
|
end
|
44
42
|
end
|
@@ -1,6 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Jtor
|
2
|
+
module Imports
|
3
|
+
def jtor_import(import_string)
|
4
|
+
packages = import_string.split('.')
|
5
|
+
path = File.join(packages[0..-2], "#{packages.last}.rb")
|
6
|
+
Dir[path].each { |p| require File.expand_path(p) }
|
7
|
+
rescue LoadError
|
8
|
+
java_import import_string
|
9
|
+
end
|
10
|
+
end
|
6
11
|
end
|
12
|
+
|
13
|
+
include Jtor::Imports
|