Sorry for posting this here. I'm very tired and I thought I was replying to the Grails users list.

I guess you won't be able to help me with Grails issues here... ;)

Thanks. Everything I needed to know from JRuby, I could already use by this morning. Sorry for not posting the solution here before.

Now, I need to figure out how to handle with Grails black magic...

Thank you guys,

Rodrigo.

Em 03-10-2011 23:36, Rodrigo Rosenfeld Rosas escreveu:
Hi Nick, thank you very much for your response.

I forgot to say in this topic, but I already achieved what I wanted, for now, with regards to embedding JRuby with gems support using Bundler. I can already run both Rspec and Capybara from Groovy since this morning.

But I've been fighting with Grails since then trying to use the domain classes inside my Ruby scripts. What happens is that I can't get the classes with GORM injected so I can't save instances to the database through those classes from Ruby.

The Grails-Ruby plugin is a very simple one and is not intended to be used with Ruby gems yet.

Take this script example:

scripts/RunSpecs.groovy:

@Grab(group='org.jruby', module='jruby-complete', version='1.6.4')
import org.jruby.embed.*
import org.jruby.CompatVersion

import org.codehaus.groovy.grails.commons.ApplicationHolder
includeTargets << grailsScript("_GrailsRun")

target('default': "Run specs") {
    depends(checkVersion, configureProxy, packageApp, parseArguments)
    runApp()
    c = new ScriptingContainer()
    c.setCompatVersion(CompatVersion.RUBY1_9)
    c.setAttribute(AttributeName.BASE_DIR, new File('.').absolutePath)
    c.runScriptlet(PathType.RELATIVE, 'src/ruby/lib/runner.rb')

    c.put('@application', ApplicationHolder.application)
    c.put('@domain_classes', ApplicationHolder.application.domainClasses)
def userClass = ApplicationHolder.application.getClassForName('example.User')
    c.put('User', userClass)
    println(userClass.count())
c.runScriptlet('require "java"; require "irb/completion"; require "irb"; IRB.start')
}

grails-app/domain/example/User.groovy:

package example

class User {
    String username
    String email
    String password

    static constraints = {
    }
}

Gemfile: (after installing JRuby, run "mkdir src/ruby; jruby -S gem install bundler; jruby -S bundle install src/ruby/gems)

# A sample Gemfile
source "http://rubygems.org";

gem "rspec"
gem "capybara"


src/ruby/lib/runner.rb:

ENV['GEM_HOME'] = './src/ruby/gems/jruby/1.8'
require 'rubygems'
require 'rspec/core'

spec_list = Dir['./src/ruby/specs/*_spec.rb']
RSpec::Core::Runner.run spec_list, $stderr, $stdout



src/ruby/specs/basic_spec.rb:

require 'capybara/rspec'

describe 'Truth' do
  example 'true should be true' do
    true.should == true
  end
end


Now, this is the part that is getting me intro trouble:

    c.put('@application', ApplicationHolder.application)
    c.put('@domain_classes', ApplicationHolder.application.domainClasses)
def userClass = ApplicationHolder.application.getClassForName('example.User')
    c.put('User', userClass)
    println(userClass.count())
c.runScriptlet('require "java"; require "irb/completion"; require "irb"; IRB.start')

You should notice how I can call ApplicationHolder.application.domainClasses in the Groovy source, but I can't do that in Ruby with "@application.domain_classes" in the interactive session.

Even if I pass the User class itself, although I can invoke "count()" on it in the Groovy script, I can't do that in the Ruby land, as well as any other GORM injected method.

I have absolutely no clue why those methods are available in the class from the Groovy script, but not from the Ruby script...

Although I can use Capybara without GORM support, I'd like to set up some tests with custom data before running the Capybara DSL.

If you have any clue, please tell me.

Thanks,

Rodrigo.


Em 03-10-2011 22:54, Nick Klauer escreveu:
I'm having a hard time following what you want out of this, so apologies if I ask some possibly silly questions.

    ...

    <snip>

     So, I'd like to know what is the recommended way for including
    RubyGems support while embedding JRuby.


Perhaps the question is more complicated than this, but have you seen Nick Sieger's Warbler <https://github.com/jruby/warbler> gem or gems-in-a-jar <http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar>, which explains how you can bundle any number of gems into a jar with the jruby-complete.jar with it? You could probably script out the gems-in-a-jar to create something for you bundled up (especially if you use Maven or the Maven Ant plugin). It also appears that Warbler will let you just bundle an entire repo of things into a .jar, so it might very well do what you're wanting out of the box. I don't know.

     My initial thoughts was to create a Gemfile including Capybara
    and Rspec and run "bundle install src/ruby/gems". Then, I would
    create my specs under src/ruby/specs and would like to be able to
    run "require 'capybara-rspec'" from there.


If you just need the gems as artifacts for use, you might be able to just use `bundle package`, then include them into the .jar, but that would require some prototyping (I'm not too familiar with whether this is possible or not).


    I also hope to be able to use the classes defined in my Grails
    application from inside the specs, like my GORM domain classes.


For that, you'd probably need to compose which methods you want to expose in shims such as what Yoko Harada demonstrated with RedBridge <http://redbridge-at-strangeloop2011.herokuapp.com/slideshow> integration.

     ...
    <snip>

    Any ideas will be greatly appreciated.


Just some simple Googling, but I see that someone took Compass and ported it to Grails, called Grass <https://github.com/altosz/grass>. There's also apparently a Grails-Ruby <http://grails.org/plugin/ruby> plugin out there. That might include most of the functionality you're looking for (embedding Ruby into Grails, potential shimming of methods/functions). In any event, I'm not familiar enough with Grails to comment on any of that, but reading code is always useful when it deals directly with the domain you're trying to dig into. But these are just my two cents. May they be useful to you. If not, good luck with the project nonetheless.

-Nick Klauer


Reply via email to