Created
December 15, 2013 11:54
-
-
Save giladmanor/7972127 to your computer and use it in GitHub Desktop.
Requires JRUBY!!!! This little thing helps us lazy people in connecting java and ruby classes.
Pop the javable.rb into your lib and include it where ever you want to use it. this GITS includes:
the javable.rb => does all the work
the javable_init.rb => helps you initialize and have this injected into whatever (see instructions at the end of the …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'javable' | |
#this example uses neo4j.rb. | |
class Bridge < Neo4j::Rails::Model | |
include Javable | |
require_jar_folder #by default imports all .jar files from lib/ | |
def initialize | |
super | |
#Create a ruby instance from the full java class name | |
@instance_of_java = javable("com.wikibrains.core.SomeSmartJavaClass").new | |
end | |
def get_me_some_smart_answers(a) | |
#invoke on a method | |
@instance_of_java.getSmart a | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIT License | |
# written by one lazy-eye developer for Wikibrains.com | |
require 'java' | |
module Javable | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def require_jar_folder lib_folder="lib" | |
jar_folder = File.expand_path("#{Rails.root}/#{lib_folder}", __FILE__) | |
jars = Dir.new(jar_folder).entries.find_all { |x| x =~ /\.jar$/ } | |
jars.each { |jar| require File.expand_path(jar, jar_folder) } | |
class_eval "include Javable::InstanceMethods" | |
end | |
end | |
module InstanceMethods | |
def javable full_java_name | |
name_a = full_java_name.split(".") | |
class_name = name_a.pop | |
package_name = name_a.join("_").camelize | |
eval "Java::#{package_name}::#{class_name}" | |
end | |
end | |
end | |
# Optional: to push this automaticly into all the models etc. | |
# unmark this line: | |
#Neo4j::Rails::Model.send(:include, Javable) | |
#and then include this file in an initializer | |
# refere to next file: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "javable" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment