Class: Inline::Scala
- Inherits:
-
Object
- Object
- Inline::Scala
- Defined in:
- lib/scala_inline.rb
Overview
A Java builder for RubyInline. Provides the basic methods needed to allow assembling a set of Java methods that compile into a class and get bound to the same names in the containing module.
Constant Summary collapse
- JFile =
java.io.File
Instance Method Summary collapse
- #build ⇒ Object
-
#import(cls) ⇒ Object
Add an “import” line with the given class, as in builder.import “java.util.ArrayList”.
-
#initialize(mod) ⇒ Scala
constructor
A new instance of Scala.
- #load ⇒ Object
- #load_cache ⇒ Object
-
#package(pkg) ⇒ Object
Set the package to use for the Java class being generated, as in builder.package “org.foo.bar”.
-
#scala(src) ⇒ Object
Add a Scala method to the built Scala source.
Constructor Details
#initialize(mod) ⇒ Scala
Returns a new instance of Scala.
20 21 22 23 24 25 |
# File 'lib/scala_inline.rb', line 20 def initialize(mod) @context = mod @src = "" @imports = [] @sigs = [] end |
Instance Method Details
#build ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/scala_inline.rb', line 57 def build if @pkg directory = "#{Inline.directory}/#{@pkg.gsub('.', '/')}" unless File.directory? directory then $stderr.puts "NOTE: creating #{directory} for RubyInline" if $DEBUG FileUtils.mkdir_p directory end @name = "Scala#{@src.hash.abs}" @load_name = "#{@pkg}.#{@name}" filename = "#{directory}/#{@name}.scala" imports = "import " + @imports.join("\nimport ") if @imports.size > 0 full_src = " package #{@pkg} #{imports} object #{@name} { #{@src} } " else @load_name = @name = "Java#{@src.hash.abs}" filename = "#{Inline.directory}/#{@name}.scala" imports = "import " + @imports.join("\nimport ") if @imports.size > 0 full_src = " #{imports} object #{@name} { #{@src} } " end File.open(filename, "w") {|file| file.write(full_src)} cmd_args = [filename, "-classpath", "#{System.getProperty('scala.home')}/lib/scala-library.jar", "-d", "#{Inline.directory}"] Main.process(cmd_args.to_java(:string)) end |
#import(cls) ⇒ Object
Add an “import” line with the given class, as in builder.import “java.util.ArrayList”. The imports will be composed into an appropriate block of code and added to the top of the source.
40 41 42 43 44 45 46 |
# File 'lib/scala_inline.rb', line 40 def import(cls) if cls.respond_to? :java_class @imports << cls.java_class.name else @imports << cls.to_s end end |
#load ⇒ Object
95 96 97 98 99 100 |
# File 'lib/scala_inline.rb', line 95 def load @context.module_eval "const_set :#{@name}, ::Java::#{@load_name}" @sigs.each do |sig| @context.module_eval "def #{sig[0]}(*args); #{@name}.#{sig[0]}(*args); end" end end |
#load_cache ⇒ Object
27 28 29 |
# File 'lib/scala_inline.rb', line 27 def load_cache false end |
#package(pkg) ⇒ Object
Set the package to use for the Java class being generated, as in builder.package “org.foo.bar”
33 34 35 |
# File 'lib/scala_inline.rb', line 33 def package(pkg) @pkg = pkg end |
#scala(src) ⇒ Object
Add a Scala method to the built Scala source. This expects the method to be public and static, so it can be called as a function.
50 51 52 53 54 55 |
# File 'lib/scala_inline.rb', line 50 def scala(src) @src << src << "\n" signature = src.match(/def\W+([a-zA-Z0-9_]+)\((.*)\)/) raise "Could not parse method signature" unless signature @sigs << [signature[1], signature[2]] end |