Permalink
Browse files

Merge pull request #200 from ruboto/jruby_adapter_and_script_rewrite

* Issue #103 Split Script responsibilities into JRubyAdapter and Script.
  • Loading branch information...
donv committed Jul 2, 2012
2 parents ada55bb + bf6b172 commit 49e7ae67793bbd1e2b57b54f99f44d6ce43703d3
@@ -1,20 +1,20 @@
package THE_PACKAGE;
-import org.ruboto.Script;
+import org.ruboto.JRubyAdapter;
public class InheritingBroadcastReceiver extends org.ruboto.RubotoBroadcastReceiver {
private boolean scriptLoaded = false;
public InheritingBroadcastReceiver() {
super("sample_broadcast_receiver.rb");
- if (Script.isInitialized()) {
+ if (JRubyAdapter.isInitialized()) {
scriptLoaded = true;
}
}
public void onReceive(android.content.Context context, android.content.Intent intent) {
if (!scriptLoaded) {
- if (Script.setUpJRuby(context)) {
+ if (JRubyAdapter.setUpJRuby(context)) {
loadScript();
scriptLoaded = true;
} else {
@@ -1,6 +1,6 @@
package THE_PACKAGE;
-import org.ruboto.Script;
+import org.ruboto.JRubyAdapter;
public class THE_RUBOTO_CLASS THE_ACTION THE_ANDROID_CLASS {
@@ -62,40 +62,44 @@ public void onCreate(Bundle bundle) {
super.onCreate(bundle);
- if (Script.isInitialized()) {
+ if (JRubyAdapter.isInitialized()) {
prepareJRuby();
loadScript();
}
}
// This causes JRuby to initialize and takes a while.
protected void prepareJRuby() {
- Script.put("$context", this);
- Script.put("$activity", this);
- Script.put("$bundle", args[0]);
+ JRubyAdapter.put("$context", this);
+ JRubyAdapter.put("$activity", this);
+ JRubyAdapter.put("$bundle", args[0]);
}
protected void loadScript() {
try {
if (scriptName != null) {
- new Script(scriptName).execute();
String rubyClassName = Script.toCamelCase(scriptName);
System.out.println("Looking for Ruby class: " + rubyClassName);
- Object rubyClass = Script.get(rubyClassName);
+ Object rubyClass = JRubyAdapter.get(rubyClassName);
+ if (rubyClass == null) {
+ System.out.println("Loading script: " + scriptName);
+ JRubyAdapter.exec(new Script(scriptName).getContents());
+ }
+ rubyClass = JRubyAdapter.get(rubyClassName);
if (rubyClass != null) {
System.out.println("Instanciating Ruby class: " + rubyClassName);
- Script.put("$java_activity", this);
- Script.exec("$ruby_activity = " + rubyClassName + ".new($java_activity)");
- rubyInstance = Script.get("$ruby_activity");
- Script.exec("$ruby_activity.on_create($bundle)");
+ JRubyAdapter.put("$java_activity", this);
+ JRubyAdapter.exec("$ruby_activity = " + rubyClassName + ".new($java_activity)");
+ rubyInstance = JRubyAdapter.get("$ruby_activity");
+ JRubyAdapter.exec("$ruby_activity.on_create($bundle)");
}
} else if (configBundle != null) {
- // TODO: Why doesn't this work?
- // Script.callMethod(this, "initialize_ruboto");
- Script.execute("$activity.initialize_ruboto");
// TODO: Why doesn't this work?
- // Script.callMethod(this, "on_create", args[0]);
- Script.execute("$activity.on_create($bundle)");
+ // JRubyAdapter.callMethod(this, "initialize_ruboto");
+ JRubyAdapter.execute("$activity.initialize_ruboto");
+ // TODO: Why doesn't this work?
+ // JRubyAdapter.callMethod(this, "on_create", args[0]);
+ JRubyAdapter.execute("$activity.on_create($bundle)");
}
} catch(IOException e){
e.printStackTrace();
@@ -25,25 +25,25 @@ public THE_RUBOTO_CLASS(String name) {
if (name != null) {
setScriptName(name);
- if (Script.isInitialized()) {
+ if (JRubyAdapter.isInitialized()) {
loadScript();
}
}
}
protected void loadScript() {
- Script.put("$broadcast_receiver", this);
+ JRubyAdapter.put("$broadcast_receiver", this);
if (scriptName != null) {
try {
- new Script(scriptName).execute();
+ JRubyAdapter.exec(new Script(scriptName).getContents());
String rubyClassName = Script.toCamelCase(scriptName);
System.out.println("Looking for Ruby class: " + rubyClassName);
- Object rubyClass = Script.get(rubyClassName);
+ Object rubyClass = JRubyAdapter.get(rubyClassName);
if (rubyClass != null) {
System.out.println("Instanciating Ruby class: " + rubyClassName);
- Script.put("$java_broadcast_receiver", this);
- Script.exec("$ruby_broadcast_receiver = " + rubyClassName + ".new($java_broadcast_receiver)");
- rubyInstance = Script.get("$ruby_broadcast_receiver");
+ JRubyAdapter.put("$java_broadcast_receiver", this);
+ JRubyAdapter.exec("$ruby_broadcast_receiver = " + rubyClassName + ".new($java_broadcast_receiver)");
+ rubyInstance = JRubyAdapter.get("$ruby_broadcast_receiver");
}
} catch(IOException e) {
throw new RuntimeException("IOException loading broadcast receiver script", e);
@@ -54,18 +54,16 @@ protected void loadScript() {
public void onReceive(android.content.Context context, android.content.Intent intent) {
try {
System.out.println("onReceive: " + rubyInstance);
- Script.put("$context", context);
- Script.put("$broadcast_receiver", this);
- Script.put("$intent", intent);
+ JRubyAdapter.put("$context", context);
+ JRubyAdapter.put("$broadcast_receiver", this);
+ JRubyAdapter.put("$intent", intent);
if (rubyInstance != null) {
- Script.exec("$ruby_broadcast_receiver.on_receive($context, $intent)");
+ JRubyAdapter.exec("$ruby_broadcast_receiver.on_receive($context, $intent)");
} else {
- Script.execute("$broadcast_receiver.on_receive($context, $intent)");
+ JRubyAdapter.execute("$broadcast_receiver.on_receive($context, $intent)");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
-
-
@@ -32,27 +32,27 @@ public void onCreate() {
super.onCreate();
- if (Script.setUpJRuby(this)) {
- Script.defineGlobalVariable("$context", this);
- Script.defineGlobalVariable("$service", this);
+ if (JRubyAdapter.setUpJRuby(this)) {
+ JRubyAdapter.defineGlobalVariable("$context", this);
+ JRubyAdapter.defineGlobalVariable("$service", this);
try {
if (scriptName != null) {
System.out.println("Loading service script: " + scriptName);
- new Script(scriptName).execute();
+ JRubyAdapter.exec(new Script(scriptName).getContents());
String rubyClassName = Script.toCamelCase(scriptName);
System.out.println("Looking for Ruby class: " + rubyClassName);
- Object rubyClass = Script.get(rubyClassName);
+ Object rubyClass = JRubyAdapter.get(rubyClassName);
if (rubyClass != null) {
System.out.println("Instanciating Ruby class: " + rubyClassName);
- Script.put("$java_service", this);
- Script.exec("$ruby_service = " + rubyClassName + ".new($java_service)");
- rubyInstance = Script.get("$ruby_service");
- Script.exec("$ruby_service.on_create");
+ JRubyAdapter.put("$java_service", this);
+ JRubyAdapter.exec("$ruby_service = " + rubyClassName + ".new($java_service)");
+ rubyInstance = JRubyAdapter.get("$ruby_service");
+ JRubyAdapter.exec("$ruby_service.on_create");
}
} else {
- Script.execute("$service.initialize_ruboto");
- Script.execute("$service.on_create");
+ JRubyAdapter.execute("$service.initialize_ruboto");
+ JRubyAdapter.execute("$service.on_create");
}
} catch(IOException e) {
e.printStackTrace();
@@ -1,7 +1,5 @@
package org.ruboto;
-import org.ruboto.Script;
-
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -32,7 +30,7 @@ public void onCreate(Bundle bundle) {
splash = -1;
}
- if (Script.isInitialized()) {
+ if (JRubyAdapter.isInitialized()) {
appStarted = true;
}
super.onCreate(bundle);
@@ -48,7 +46,7 @@ public void onResume() {
}
Log.d("RUBOTO", "onResume: Checking JRuby");
- if (Script.isInitialized()) {
+ if (JRubyAdapter.isInitialized()) {
Log.d("RUBOTO", "Already initialized");
fireRubotoActivity();
} else {
@@ -100,7 +98,7 @@ public void onDestroy() {
private void initJRuby(final boolean firstTime) {
new Thread(new Runnable() {
public void run() {
- final boolean jrubyOk = Script.setUpJRuby(EntryPointActivity.this);
+ final boolean jrubyOk = JRubyAdapter.setUpJRuby(EntryPointActivity.this);
if (jrubyOk) {
Log.d("RUBOTO", "onResume: JRuby OK");
prepareJRuby();
Oops, something went wrong.

0 comments on commit 49e7ae6

Please sign in to comment.