query: tag:jruby

Rails + Cucumber で Culerity を使うと、
以下のような WARNING が大量に発生する。
これでは、いくらテストがgreenでも精神はredになってしまう。

shell>>
% cucumber features/foo.feature
Using the default profile...
2010/01/20 18:43:57 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
警告: Obsolete content type encountered: 'text/javascript'.
2010/01/20 18:43:58 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
警告: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/amazonJQ/amazonJQ-combined-core-5113._V228305908_.js', but got 'application/x-javascript'.
(...)
.

1 scenario (1 passed)
1 step (1 passed)
<<--

この警告メッセージは、 HtmlUnit が吐いているものであるが、
Culerity::RemoteBrowserProxy のオプション(log_level)
で制御することができる。
具体的には、
"script/generate culerity" を利用した場合、
以下の step ファイル内で定義されている。

shell>>
% vi features/step_definitions/culerity_steps.rb

  • $browser.log_level = :warning
  • $browser.log_level = :severe
    <<--

(celerity の default は :off だが、culerity が :warning にしている)

上記の修正によって、WARNING の表示が抑制される。

shell>>
% cucumber features/foo.feature
Using the default profile...
.

1 scenario (1 passed)
1 step (1 passed)
<<--

新規プロジェクトの度に毎回変更するのが面倒であれば、
generator の template を直接変更するのもいいかもしれない。

shell>>
% vi $GEM_PATH/gems/culerity-0.2.7/rails_generators/culerity/templates/features/step_definitions/culerity_steps.rb
<<--

指定可能な log_level は、
(高) :all, :severe, :warning, :info, :config, :fine, :finer, :finest, :off (低)
であり、実際に以下のコードで確認することができる。(JRuby)

ruby>>
% irb
irb(main):001:0> java.util.logging.Level.constants
=> ["FINE", "CONFIG", "FINEST", "FINER", "WARNING", "SEVERE", "OFF", "INFO", "ALL"]
<<--

log level に関するさらなる詳細は java.util.logging.Level を参照のこと。

http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/util/logging/Level.html

posted by maiha maiha on Wed 20 Jan 2010 at 09:50 with 0 comments

(以下は PostgreSQL を例にしてますが、他のドライバでも通用すると思います)

install

まずは JRuby 用のドライバをインストールします。

shell>>
% gem install activerecord-jdbcpostgresql-adapter
<<--

設定

通常は database.yml に "adapter: jdbcpostgresql" と記述して利用するのですが、ここでは別の方法を紹介します。
内容は簡単で以下のコードを評価するだけです。

ruby>>
if RUBY_PLATFORM == "java"
require "active_record/connection_adapters/jdbcpostgresql_adapter"
end
<<--

(Railsであれば "config/initializers/jruby_compat.rb" などに記述しておくとよいでしょう)

メリット

  • MRI と同じアダプタ名 "postgresql" を利用できる
  • 変更なしで MRI, JRuby のどちらでも動作する

実行例

shell>>
% rvm use system
% ./script/console

User.count
=> 6

User.connection.class
=> ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

% rvm use jruby
% ./script/console

User.count
=> 6

User.connection.class
=> ActiveRecord::ConnectionAdapters::JdbcAdapter
<<--

JRuby はまだ発展途上ですので、
開発や挙動確認で MRI を利用する局面が多々あります。
そんな折、この方法であればいちいち database.yml の内容を変更する必要がないため、非常にシームレス&快適に利用できると思います。

posted by maiha maiha on Mon 18 Jan 2010 at 23:16 with 0 comments

GAEなどでファイル数の多いライブラリを使うために、gemのパッケージから不要そうなファイルを消してjarファイルにまとめるために作ったスクリプトです。

ruby>>
#!/usr/bin/env ruby

def run(command)
puts command
system command
end

case ARGV[0]
when 'install'
run "jgem install -i ./gems --no-ri --no-rdoc #{ARGV[1]}"
when 'uninstall'
run "jgem uninstall -i ./gems #{ARGV[1]}"
end

Dir['gems/gems/'].each do |dir|
%w(
Generaters TODO README.
README
spec// test// examples// tasks//
tutorial// demo// website/**/*
CHANGES CHANGES.txt CHANGELOG
LICENSE MIT-LICENSE Rakefile CONTRIBUTORS
Manifest.txt History.txt install.rb setup.rb
).each do |pattern|
Dir[File.join(dir, pattern)].each do |path|
run "rm -rf #{path}"
end
end
end

Dir['gems/cache', 'gems/bin', 'gems/doc'].each do |path|
run "rm -rf #{path}"
end

run "jar cf lib/merb.jar -C ./gems ."
<<--

posted by genki genki on Mon 7 Sep 2009 at 19:14 with 2 comments
14th Tue

Endless Ruby

HAMLを使うようになってから、endの無いrubyが欲しい今日この頃。
あわや自作する直前、先人の仕事を見つけました。

[ANN] Endless Ruby 0.0.2

endless.rb is a pre-processor for ruby which allows you to use python-ish
indentation to delimit scopes, instead of having to type 'end' every time.

良いですね。残念ながら、現時点ではrubylexerのrequireでエラーが出るようですが、一定の制約付きでもかまわないので動くようになるとありがたいです。
JRubyのCharles Nutterも割と
肯定的なコメント
を寄せていますね。

Oh dear god no!

Seriously though...neat :)

-- Charlie

JRubyのオプションに--endlessがつく?

posted by genki genki on Tue 14 Jul 2009 at 17:33 with 0 comments

The edge version of do_postgres with JRuby causes misterious error like this.

pre>>
~ (1) SELECT "id", "digest", "fetched_at", "created_at", "site_id" FROM "pages" WHERE ("site_id" IN (1)) ORDER BY "id"
~ ERROR: syntax error at or near "("merb : worker (port 4000) ~ ERROR: syntax error at or near "(" - (PostgresError)
<<--

The SQL that had been performed is here.

sql>>
SELECT "id", "digest", "fetched_at", "created_at", "site_id" FROM "pages" WHERE ("site_id" IN (1)) ORDER BY "id"
<<--

This is able to be performed successfully at the psql console.
So I wonder what the error message means.

Updates

I found the bug.

For example, this code

pre>>
Page.repository.adapter.execute('SELECT * FROM "pages" WHERE ("id" IN ?) AND ("site_id" = ?)', [1], nil)
<<--

generates this sql

sql>>
SELECT * FROM "pages" WHERE ("id" IN (1)) AND ("site_id" = (NULL))
<<--

(NULL) causes parsing error.

This issue happens if the first value is an Array and the succeeding values are not Arrays.

posted by takiuchi takiuchi on Sun 7 Jun 2009 at 20:06 with 0 comments

Hello, Merbists!

Today, I explain how to develop Merb apps that runs on GAE/J environment by using dm-datastore-adapter.

First of all, here is whole source code of an example app. Please check it out.

After checking it out,
you must edit appengine-web.xml file. Open it by editor and change the application name to yours.
And then you should make war directory by typing this command.

pre>>
MERB_ROOT% jruby -S warble war
<<--

It generates files under tmp/war.

So far, you are ready to deploy this app to GAE/J
Of course, you need an account of GAE/J to do it. Please get it in advance :-)

Let us go to deploy by this command.

pre>>
MERB_ROOT% appcfg.sh update -e {youraccount@gmail.com} tmp/war
<<--

This process takes a time for the first time.
If the log didn't say any errors, you got success!

Now your first Merb app on GAE/J is here at
http://{your app name}.appspot.com/

Congrats!

Further improvement is your home work :-p

APPENDIX

All required gems are being packed into jar file located at lib/merb.jar.
This enables you to pass the limit of which you can upload only 1000 files to GAE.
If you add more gems to the jar file, you can do it like this

  • unpacking it
  • add gems
  • and repack it

Enjoy!

posted by takiuchi takiuchi on Fri 24 Apr 2009 at 07:54 with 2 comments

JRubyで相対パスを含む場合に、Jarファイルの中のファイルを読めるようにするためのモンキーパッチの最新版です。

ruby>>
def cleanup_path(path)
if path.to_s.match(/^file:/) && path.is_a?(String)
jar_path, inner_path = path.split('!', 2)
inner_path = File.expand_path(inner_path)
path = [jar_path, inner_path].join('!')
end
path
end

alias :require_original :require
def require(path)
require_original cleanup_path(path)
rescue Exception => e
raise e unless path.to_s.match(/^file:/)
end

alias :load_original :load
def load(path)
load_original cleanup_path(path)
rescue Exception => e
raise e unless path.to_s.match(/^file:/)
end

class Dir
class << self
alias :aref_original :[]
def
aref_original *(args.map{|path| cleanup_path(path)})
end
end
end
class File
class << self
alias :mtime_original :mtime
def mtime(path)
if path.match(/^file:/)
jar_file, = path.split('!', 2)
path = jar_file.sub(/^file:/, '')
end
mtime_original(path)
end
end
end
<<--

posted by genki genki on Tue 21 Apr 2009 at 02:41 with 0 comments

GAEを使う上での大きな制限として、ファイル数1000までというのがあります。
これを乗り越えるために、unpackしたGemの中から不要なファイルを掃除したりする必要があったのですが、
関連するGem群をjarファイルにまとめる事でこの問題を乗り越えられます。

上記のファイルをlibの下に配置して、config/init.rb
あたりでrequire_fixを読み込みます。
これはjarファイルの中のrbファイルの読み込みに関するバグを
回避するためのものです。

実際に以下のサイトでmerb.jarを使って運用しています。

これによって、事実上ファイル数制限に左右されずにアプリケーションを開発する事が出来るようになります。
ただ、1ファイルのサイズ制限(10MB)が存在するので、jarファイルが大きくなりすぎた場合は分割する必要があります。

posted by genki genki on Sun 19 Apr 2009 at 08:25 with 0 comments

JRubyを使っていて、JavaのArrayListを作りたい場合には、以下のようにすると良いようです。

ruby>>
java.util.Arrays.as_list([1,2].to_java).getClass.to_s

=> "class java.util.Arrays$ArrayList"

<<--

posted by genki genki on Fri 17 Apr 2009 at 11:03 with 2 comments

I shipped new dm-datastore-adapter today.

This update is a long jump from previous version. It includes following functions

  • Transaction. It utilizes the DataStore's Transaction API
  • OR conditions
    ruby>>
    Post.all(:id => [1,2])
    <<--
  • NOT conditions
    ruby>>
    Post.all(:id.not => 4)
    <<--

So now we can use most of functions defined in DM by using this adapter.

Enjoy!

posted by takiuchi takiuchi on Fri 17 Apr 2009 at 05:44 with 0 comments

dependencyでdata_objectsを指定すると、GAE/Jでなぜか突然exitしてしまうので理由を調べていたところ、data_objectsのtransaction.rbで以下のようなコードがあり、そこで落ちている事が分かりました。

ruby>>
module DataObjects

class Transaction
HOST = "#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
<<--

Socket::gethostbyname を使っているところですね。

posted by genki genki on Tue 14 Apr 2009 at 04:25 with 0 comments

Today I shipped a new DataMapper plugin that enables us to easily develop Merb-apps been worked with GAE/J.

By using this plugin, you can seamlessly develop your Merb-apps between local and GAE/J environment.

For example, this site is powered by Merb/DM with the dm-datastore-adapter and running on the GAE/J

(This service is under construction :-p)

As a matter of fact, because it is still being alpha status,
you must treat various issues regarding gem dependencies yet.
I appreciate any kind of feedback and of course patches :-)

Enjoy!

posted by takiuchi takiuchi on Mon 13 Apr 2009 at 05:43 with 11 comments

DataMapper用のDataStore用アダプタ、dm-datastore-adapterを作りました。

これを使えば、MerbアプリをほぼそのままGoogle App Engine for Javaの上で動かせるようになります。
(実際にはgemの依存関係など、細かい調整が必要になりますが)

実際に以下のサイトでdm-datastore-adapterを使っています。

ローカル環境でも別なアダプタを利用すれば、普通のMerbアプリとして動作テストできるので、開発効率が飛躍的に向上します。

posted by genki genki on Mon 13 Apr 2009 at 05:37 with 0 comments

現在のところ、JRubyでjgem install do_sqlite3などによってdo_sqlite3をインストールしようとすると、Native extensionを使っているという事でエラーが出ます。

pre>>
Building native extensions. This could take a while...
/usr/local/jruby/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError)
<<--

この問題を解決するには、最新版のdatamapper/doをgithubからとってきて、自分でbuildする必要があります。

http://github.com/datamapper/do/tree/master

doをgit cloneしたら、まずはdata_objectsディレクトリに移動し、以下のようにしてdata_objectsをインストールします。

pre>>
% jruby -S rake package
% sudo jgem install pkg/data_objects-0.9.12.gem
<<--

続いて、do_jdbcディレクトリに移動し、do_jdbcをコンパイルしてインストールします。

pre>>
% jruby -S rake compile
% jruby -S rake package
% sudo jgem install pkg/do_jdbc-0.9.12-java.gem
<<--

最後に、do_sqlite3 ディレクトリに移動し、以下のようにインストールします。

pre>>
% jruby -S rake package
% sudo jgem install pkg/do_sqlite3-0.9.12-java.gem
<<--

以上でインストール完了です。

posted by genki genki on Sun 12 Apr 2009 at 03:34 with 0 comments

GAEjのファイル数制限が厳しいので、rails.jarを試作してみました。

rails-2.3.2.jar

jrubyのバグなのか、jarファイルの中身に対してrequireをする時に、
pathの中に../..のような感じの指定が入っているとうまくロードできないようなので、その問題を回避するモンキーパッチをあてる必要がありました。

require_fix.rb

ruby>>
alias :require_original :require
def require(path)
path = File.expand_path(path) if path.to_s.match(/^file:/)
require_original path
end

alias :load_original :load
def load(path)
path = File.expand_path(path) if path.to_s.match(/^file:/)
load_original path
end
<<--

それでこんな感じに使えます。

pre>>
% java -jar $JRUBY_HOME/lib/jruby-complete.jar -rrequire_fix -rrails-2.3.2.jar -S rails -v
Rails 2.3.2
<<--

実際にはlibの下に設置する感じになりますね。

posted by genki genki on Sat 11 Apr 2009 at 09:41 with 0 comments

2日前ぐらいからいろいろ頑張ってたのですが、ようやくGAEjの本番環境でMerbアプリを動かせました。

http://jmerbist.appspot.com/

ss

使っているgemをfreezeする仕組みがあるフレームワークであれば、どれでも以外と簡単に動きそうな感じがします。

いやー、これは色々面白い事が出来そうですね!

posted by genki genki on Thu 9 Apr 2009 at 17:18 with 0 comments

以前、Merb用のaliasの設定をご紹介しましたが、今度はjruby版です。

sh>>
alias jm='PATH=./bin:$PATH jruby -S merb'
alias jmi='jm -i'
alias jmg='PATH=./bin:$PATH jruby -S merb-gen'
<<--

rubyとjrubyを両方使ってると、

pre>>
% jruby -S merb
<<--

のように書く事が多いので、aliasを設定しておくと便利ですね。

posted by genki genki on Thu 9 Apr 2009 at 01:45 with 0 comments

最新のhpricotをjruby(1.2)で利用する手順をメモしておきます。

まず、githubから最新のhpricotをcloneしてきます。

あとは、以下のようにしてjruby用のパッケージをbuildしてインストールします。

pre>>
% jruby -S rake package_java
% sudo jgem install pkg/hpricot-0.8.235-jruby.gem
<<--

これでOK.

posted by genki genki on Wed 8 Apr 2009 at 16:49 with 0 comments

思い立ってJRubyの環境を入れてみる事にしました。

ちょうどバージョン1.2が出ているようなので、ソースを落としてきます。

http://dist.codehaus.org/jruby/1.2.0/jruby-src-1.2.0.tar.gz

落としてきたファイルは/usr/local/jruby-1.2.0 あたりに展開します。
展開したら、Javaらしくantでbuildします。

pre>>
% ant
<<--

あとは、以下のように~/.zshrcなどで環境変数を設定します。

sh>>
export JRUBY_HOME=/usr/local/jruby-1.2.0
export PATH=:$JRUBY_HOME/bin:$PATH
<<--

これで、ひとまずjrubyが動くようになります。

pre>>
% jruby -v
jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-04-08 rev 6586) [i386-java]
<<--

posted by genki genki on Wed 8 Apr 2009 at 06:45 with 0 comments