The Artima Developer Community
Sponsored Link

Java Buzz Forum
rjs command line to generate JavaScript from Ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
rjs command line to generate JavaScript from Ruby Posted: Apr 18, 2007 12:23 AM
Reply to this message Reply

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
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

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

E.g. with a sample test.rjs of

page.insert_html :bottom, 'list', "
  • item name
  • " page.visual_effect :highlight, 'list' page.hide 'status-indicator', 'cancel-link'

    I would run % rjs test.rjs and would get:

    new Insertion.Bottom("list", "
  • item name
  • "); 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.

    Read: rjs command line to generate JavaScript from Ruby

    Topic: JetBrains�� TeamCity 2.0: Long-awaited Eclipse community support Previous Topic   Next Topic Topic: [DrunkAndRetired.com Podcast] Episode 90: Properties and Scaling in Muskogee from...

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use