Issues loading java class into gem in JRuby - ruby

I am trying to add JRuby support to my C-extension gem, by writing a Java class and executing it when the gem is installed on JRuby. The code in question lives on this branch, with the important files being the java class and the code to load it when used in JRuby
At this point, I am just trying to ensure I have my gem configured correctly, and fully expect it to fail the specs.
When I try to run rspec (or the gem in IRB for that matter) I get the following backtrace
NameError: cannot load Java class com.dockyard.PgArrayParser
get_proxy_or_package_under_package at org/jruby/javasupport/JavaUtilities.java:54
method_missing at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:51
PgArrayParser at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:6
(root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:3
require at org/jruby/RubyKernel.java:1033
require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
(root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:2
require at org/jruby/RubyKernel.java:1033
require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
(root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:1
require at org/jruby/RubyKernel.java:1033
require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
(root) at /Users/dan/Projects/dockyard/pg_array_parser/spec/spec_helper.rb:1
load at org/jruby/RubyKernel.java:1058
load_spec_files at /Users/dan/Projects/dockyard/pg_array_parser/spec/parser_spec.rb:746
collect at org/jruby/RubyArray.java:2331
load_spec_files at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746
run at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22
run at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69
autorun at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10
call at org/jruby/RubyProc.java:270
call at org/jruby/RubyProc.java:224

Having recently worked on a JRuby extension myself, here are some of the gotchas which I have found through experiment -- and which seem to cause you problems too:
The java package must match the Ruby module in which your class sits. If your class is in the PgArrayParser module, your class must be in the pgarrayparser (all lowercase).
If you want to instantiate a PgArrayParserEngine class, then you BasicLibraryService must be called PgArrayParserEngineService.
When using #JRubyMethod, specify the name of the method.
I have found Yoko's article on the subject most useful, along with this recent post.
I have applied these different elements to your project here, and I can call the method from IRB -- and the first spec passes!

Related

Ruby: Require Fails To Import - Need To Set Root Directory

Forgive my inexperience with Ruby, but I am unable to run a script within a third-party project with the following structure:
˅ alpha
˅ lib
˅ bravo
golf.rb
˅ charlie
˃ delta
˅ echo
foxtrot.rb
require "charlie/delta/echo/__init"
__init.rb
require "bravo/golf"
What should my command-line be to run the script, 'foxtrot.rb', as the following generates an error:
ruby "c:\arby\lib\bravo\charlie\delta\echo\foxtrot.rb"
"'require': cannot load such file -- charlie/delta/echo/__init (LoadError)"
If this is the code inside of __init.rb, it won't work.
require "charlie/delta/echo/__init"
__init.rb
require "bravo/golf"
require tells ruby to load the code inside a ruby file. In order for it to work, the files need to be organized correctly. You can also use require_relative but they still need a relative path from the file calling them. See What is the difference between require_relative and require in Ruby?

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

Creating Ruby Main (command line utility) program with multiple files

I am trying to use the main gem for making command line utilities. This was presented in a recent Ruby Rogues podcast.
If I put all the code in one file and require that file, then rspec gives me an error, as the main dsl regards rpsec as a command line invocation of the main utility.
I can break out a method into a new file and have rspec require that file. Suppose you have this program, but want to put the do_something method in a separate file to test with rspec:
require 'main'
def do_something(foo)
puts "foo is #{foo}"
end
Main {
argument('foo'){
required # this is the default
cast :int # value cast to Fixnum
validate{|foo| foo == 42} # raises error in failure case
description 'the foo param' # shown in --help
}
do_something(arguments['foo'].value)
}
What is the convenient way to distribute/deploy a ruby command line program with multiple files? Maybe create a gem?
You are on the right track for testing - basically you want your "logic" in separate files so you can unit test them. You can then use something like Aruba to do an integration test.
With multiple files, your best bet is to distribute it as a RubyGem. There's lots of resources out there, but the gist of it is:
Put your executable in bin
Put your files in lib/YOUR_APP/whatever.rb where "YOUR_APP" is the name of your app. I'd also recommend namespacing your classes with modules named for your app
In your executable, require the files in lib as if lib were in the load path
In your gemspec, make sure to indicate what your bin files are and what your lib files are (if you generate it with bundle gem and are using git, you should be good to go)
This way, your app will have access to the files in lib at runtime, when installed with RubyGems. In development, you will need to either do bundle exec bin/my_app or RUBYLIB=lib bin/my_app. Point is, RubyGems takes care of the load path at runtime, but not at development time.

Call one Ruby Script from Another

I am having a perplexing problem. I want to call one ruby script from another.
With this in mind, I create a testscript.rb and executed it, it contains this code,
require './paypal.rb'
puts paypal['L_AMT0']
This code returns a number, which is my paypal balance. It relies on a paypal.rb file which uses the ruby-paypal gem. When I do ruby testscript.rb I get the output of my paypal balance, which means it is working properly. This tells me that my method for calling one RB from another is okay, since, in the above scenario, testscript.rb is getting a variable that is returned from paypal.rb.
Using this same logic, I inserted the above code into another program which is called SiriProxy. It is a ruby program. I need it to get my paypal balance.
So Inside that program, I did a require paypal.rb, it failed because it could not find it, so I set an absolute path in require which fixed it.
However, when SiriProxy (the other ruby rb giving me an issue) trys to run puts paypal['L_AMT0'] it results in an error and ends the program.
This is the error,
[Info - Plugin Manager] Say: Checking Paypal Balance
/home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/gems/siriproxy-0.3.0/plugins/siriproxy-example/lib/siriproxy-example.rb:47:in `block in <class:Example>': undefined local variable or method `paypal' for #<SiriProxy::Plugin::Example:0x931a228> (NameError)
from /home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/bundler/gems/cora-1edcfb9073d5/lib/cora/plugin.rb:47:in `instance_exec'
from /home/siriproxy/.rvm/gems/ruby-1.9.3-p194#SiriProxy/bundler/gems/cora-1edcfb9073d5/lib/cora/plugin.rb:47:in `block (2 levels) in process'
In the above output it appears the issue is it does not understand "paypal", as seen here:
undefined local variable or method `paypal'
However, I do not understand why, since in testscript.rb I do the exact same thing and it works.
Can anyone help? Thank you.
Seems like #CodeGnome is right.
From the Kernel documentation:
require(name) → true or false
Loads the given name, returning true if successful and false if the
feature is already loaded.
require_relative(string) → true or false
Ruby tries to load the library named string relative to the requiring
file’s path. If the file’s path cannot be determined a LoadError is
raised. If a file is loaded true is returned and false otherwise.
Loading Files from the Current Directory
I don't know anything about your library or its internals, but it looks like your require statement may be wrong. If you want to load a file from the current directory in Ruby 1.9.3, you should use:
require_relative 'paypal'
Bundler and Gems
If it's a gem that you've installed as part of a bundle (e.g. the gem is defined in a Gemfile), then providing a path in your require statement is wrong. Instead, you need to require the bundled gem as follows:
require 'bundler/setup'
require 'paypal'

How can fixtures be replaced with factories using rails3-generators?

I'm trying to replace fixture generation with factories using rails3-generators:
https://github.com/indirect/rails3-generators#readme
The gem is included in my Gemfile and has been installed:
# Gemfile
gem 'rails3-generators', :group => :development
I added the following to application.rb:
# application.rb
config.generators do |g|
g.stylesheets false
g.fixture_replacement :factory_girl
end
Yet 'rails g model Insect' is still generating fixtures ('insects.yml'). Is this working for others using Rails 3.0.4 and rails3-generators 0.17.4?
'rails g' shows the new generators available (such as Authlogic and Koala), but 'rails g model' still lists fixtures and doesn't refer to factories.
What else should I add to get this to work? Thanks.
Edit: I ran the gem's test suite, which includes a test for this, and it passes. No clue why it doesn't work with my app.
Edit2: I tried again with a test project and get the same result: fixtures instead of factories. If anybody could confirm whether this works for them with Rails 3.0.4 and rails3-generators 0.17.4, that would be helpful too because it would imply that I'm doing something wrong with my projects.
Edit3: It works if I run 'rails g model Insect -r factory_girl'. I thought the generator configuration in application.rb was supposed to take care of that, so this seems to be the source of the problem.
Searching around I found the following, which may help:
Try specifying a directory option for factory_girl's factories:
config.generators do |g|
g.stylesheets false
g.fixture_replacement :factory_girl, :dir => "spec/factories" # or test/factories, as the case may be
end
If you're using Test::Unit, try the following:
config.generators do |g|
g.stylesheets false
g.test_framework :test_unit, :fixture_replacement => :factory_girl
end
In both cases you will still need the rails3-generators gem, although there is a push to get that functionality into factory_girl_rails.
This Rails bug indicates that, at some point, the g.fixture_replacement code may not have worked right. Perhaps a test in 3.0.5 is in order. :)
A short update 9 years later:
instead of "factory_girl_rails" (which is deprecated now) use "factory_bot_rails".
Now, the factory gets created automagically:
$ rails g model tester name:string
Running via Spring preloader in process 31467
invoke active_record
create db/migrate/20200327152901_create_testers.rb
create app/models/tester.rb
invoke rspec
create spec/models/tester_spec.rb
invoke factory_bot
create spec/factories/testers.rb
I use rails 5.2.4, but this should also work with rails 6.

Resources