This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: rjs command line to generate JavaScript from Ruby
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Since I want to write Ruby and generate JavaScript, why not use RJS?
To get going, I wanted to take just the pieces that do the RJS work in Rails and use them to just do the JS emitting.
I ended up with a script that takes a file and emits the JS from it:
#!/usr/bin/env ruby
# -----------------------------------------------------------------------------
# rjs - go through an RJS file and output the JavaScript
# -----------------------------------------------------------------------------
require 'rubygems'
gem 'actionpack'
require 'action_controller'
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::PrototypeHelper
include ActionView::Helpers::ScriptaculousHelper
def get_code; File.read ARGV.first; end
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
raise 'rjs: must pass in an RJS file to load' unless ARGV.size > 0
code = get_code
output = ''
eval <<-EoC
output = update_page do |page|
#{code}
end
EoC
puts output
");
new Effect.Highlight("list",{});
["status-indicator", "cancel-link"].each(Element.hide);
I want to extract what is needed here, put it in jruby.jar, and then use RJS as the basis for the generation. It would be nice if RJS wasn't quite so tightly coupled into Rails. Then the fun of:
<script type="text/ruby">
page.select('#items li').each do |value|
value.hide
end
</script>
would be a reality.
NOTE: I have to give a huge kudos to the JRuby team for something. I was using an ancient jruby-complete.jar for the initial implementation of ruby int he browser. I didn't want to do something newer as I didn't want to build JRuby from scratch. Half the time getting an open source project to build is a very painful and time wasting experience.
With JRuby I grabbed the project from Subversion, ran ant, and it freaking worked first time. I am quite amazed. Now, ruby in the browser is running on JRuby head.