All kinds of neato stuff from the far reaches of the internets.

MadPhoenix Design Journal

Using TextMate with Rhino

November 29th, 2007 by b

If you regularly read a site like reddit’s programming page, you’ve probably been seeing lots of chatter about Javascript lately. As it turns out, while Javascript was once considered by many to be a toy language, it is now being seen in a new light by programmers familiar with other dynamic languages such as Ruby or Python.

Problem is, Javascript has traditionally been a pain to play with. You’re basically locked into programming in the browser, which is a really cumbersome way to investigate the more interesting features of Javascript such as object prototyping and functional programming.

Enter the good folks at Mozilla who developed Rhino, a JVM-based javascript interpreter geared toward the application and server-side domains. Rhino provides a great way to try your hand at Javascript in your own favorite text editor.

On my Mac, I’m rarely using anything but TextMate to get coding done. Here I’ll show you how to add a new command to the TextMate Javascript bundle so that you can run your script in Rhino by simply pressing Apple-R.

First, you need to get rhino. Download the latest version here (Java SE 6 includes Rhino, however Apple hasn’t been released it for Mac OS 10.5 yet). Put it in a convenient place, such as /usr/local or somewhere in your user directory.

Once we have Rhino somewhere in our filesystem, we need to make a new command in TextMate to bind Apple-R to “Run in Rhino”. Open TextMate and select Bundles -> Bundle Editor -> Show Bundle Editor:

TextMate Bundle Editor Screenshot

  • First click on JavaScript in the list of bundles on the left-hand side.
  • Now click the plus button in the lower-left hand corner and select New Command.
  • You will see a new command show up under JavaScript called “untitled”, waiting for you to replace this with a more meaningful name. Call it something like “Run Script in Rhino”
  • Set “Save” to Nothing
  • Set “Command(s)” to /usr/bin/java -cp /usr/local/rhino/js.jar org.mozilla.javascript.tools.shell.Main -f “$TM_FILEPATH”
  • Set “Input” to Entire Document
  • Set “Output” to Show as HTML
  • Set “Activation” to Key Equivalent, Apple-R (or whatever else you choose)
  • Finally, set “Scope Selector” to source.js
  • Now all you have to do is close the Bundle Editor window, then go to the Bundles Menu and select Bundle Editor -> Reload Bundles

Now just open a JavaScript script, hit Apple-R, and voila! A new window opens with the output of your script

Rhino Running in TextMate

Filed under Uncategorized having No Comments »

Cool Stuff 9.26.07

September 26th, 2007 by b
  • This guy, who, some years back, added tons of common Ruby functionality to Javascript. Schwing!
  • COLOURLovers.com has an amazing selection of user-contributed color palettes. Very nice for graphic design n00bs who don’t know the first thing about colo(u)r theory.
  • LCD Soundsystem. Daft Punk may be playing at their house, but LCD Soundsystem is playing in my head.
Filed under Nonsense having No Comments »

Making Javascript More Like Ruby: Iterators

September 25th, 2007 by b


Array.prototype.each = function(block) {
for( var i = 0; i < this.length; i++) { block(this[i]) }
return this;
}
Array.prototype.collect = Array.prototype.map = function(block) {
var acc = new Array();
this.each(function(item) { acc.push(block(item)) } );
return acc;
}
Array.prototype.filter = function(block) {
var acc = new Array();
this.each(function(item) {
if(block(item) == true) acc.push(item);
});
return acc;
}

Filed under WebDev having No Comments »

Cool Stuff 9.25.07

September 25th, 2007 by b
Filed under Nonsense having No Comments »

Cool Stuff Inaugural

September 12th, 2007 by b

Lately in my readings from the blogosphere (note: please replace this buzzword with something much cooler later), I’ve had a little too much of what my mom liked to call “in one ear, out the other” syndrome. It’s been ages since I’ve updated this blog, and I think the easiest way to keep on it is to post a daily “Cool Stuff I’ve Learned on the Internets” article. Yay!

Filed under Nonsense having No Comments »

An Object-Relational Mapping Class in PHP

June 9th, 2006 by b

So I’ve been interested in building Classes in PHP lately to abstract a lot of the “plumbing” work required for database-backed websites. At first, most of my work was based off of this article from IBM Developerworks on writing an Object-Relational Mapping class in PHP. The idea is pretty simple. There are databases that store all of the information for the website. Think of it as multiple Excel spreadsheets all linked together. The actual code you write to access those databases, however, is object-oriented. For example, if I am working with data about people I might have a people “object” that is like 1 variable that knows all the important information about that person (their age, their eye color, their sexual preferences, whatever). In the past, Objects and Databases were hard to work with, because the way you pull information out of databases is by passing it queries and then reading back the results. For example:

SELECT * FROM people WHERE eye_color = “blue”;

Sends me back the information of every person who matches the criteria (in this case, eye color of blue). In order to get that into my object, you have to do manual assignments which get very tedious. Let’’s say, for example, there is a person object called Branden:

Person Branden = new Person();
Branden.EyeColor = “blue”;
Branden.Age = 22;
Branden.FavoriteDrink = “Bourbon”
Branden.isDrunk = “yes”

And so on and so forth. To do this for every object is really the opposite of what any programmer wants to spend their time doing. What Object-Relational mapping does is let you automatically load an object with the proper information from the database, just by passing it an id. So let’’s say that in the database, person Branden has an id of 2. Now our code reduces to:

PersonORM Branden = new PersonORM();
Branden->load(2);

Much nicer. In this case I tell the load() method what id to use, and it goes to the database and fills in all of the Branden object’’s variables with the proper information. Cool. I can access the data from my php script through overloaded “magic” get/set functions. For example:

Branden->{”age”} = 22;
Branden->update();

The class automatically takes care of generating the proper SQL statement to update the database. The variables that get changed in the object don”t get pushed to the database until I call the XMLDBObject::update() function, preserving the atomic transactions with the database.
I can now use PHP on the server to take this information from the database (without having to touch an SQL statement) and generate a complete HTML page to send back to the user’’s web browser. But wait a minute, doesn”t this all seem a little….1999?

Enter XML and XSLT. Essentially what you need to know is that the ORM class I wrote doesn”t just populate the object with the proper information from the database, it also has the abity to send information about the object in XML form directly to the web browser. Why is this cool? Because it allows you to circumvent generating an entire HTML page on the server in PHP. Instead of sending a well-formed web page (including advertisements), you just send the data. Once the web browser recieves the XML, it can use an XSLT stylesheet to convert the data into an actual web page. This process allows you to greatly reduce the bandwidth coming from the server, and let the user’’s browser do all the heavy lifting. Let’’s say, for example, I want to send an XML document back to the web browser with information about a bunch of artists that will be formatted into a table by the XSL sheet. All I have to do in my PHP script is:

foreach($artist_array as $artist) {
echo $artist->asXML(); }

An example of this use case is whenever you have an XMLHttpRequest (Ajax) object in your javascript that requests information from the php script. You can use the ajax object to request the xml data, and when it recieves it back you run it through the XSLTProcessor with your stylesheet and blam-o: well formed HTML page and all you sent from the server was some XML.
I will be making a page for this library on madphoenixdesign.com, but for the meantime you can find the source code here.

Filed under WebDev having No Comments »