Merb1.0 Install & Create First Application & Trouble
Railsよりシンプルで軽くて、まつもとさんも大好きと噂のMerbですが、3日前まで使えてたはずなのに、昨日訳あってOSXを再インストールしたあとに環境を構築し直して実行すると、エラーで実行できずにはまってしまったので、通常のインストール手順とともに対策を。
インストール
$ sudo gem install merb
これだけで完了です。40数個のgemがインストールされた模様です。
アプリケーションを作成。
$ merb-gen app my-first-app Generating with app generator: [ADDED] spec [ADDED] gems [ADDED] tasks/merb.thor [ADDED] .gitignore [ADDED] public/.htaccess [ADDED] tasks/doc.thor [ADDED] public/javascripts/jquery.js [ADDED] doc/rdoc/generators/merb_generator.rb [ADDED] doc/rdoc/generators/template/merb/api_grease.js [ADDED] doc/rdoc/generators/template/merb/index.html.erb [ADDED] doc/rdoc/generators/template/merb/merb.css [ADDED] doc/rdoc/generators/template/merb/merb.rb [ADDED] doc/rdoc/generators/template/merb/merb_doc_styles.css [ADDED] doc/rdoc/generators/template/merb/prototype.js [ADDED] public/favicon.ico [ADDED] public/images/merb.jpg [ADDED] public/merb.fcgi [ADDED] public/robots.txt [ADDED] Rakefile [ADDED] app/controllers/application.rb [ADDED] app/controllers/exceptions.rb [ADDED] app/helpers/global_helpers.rb [ADDED] app/models/user.rb [ADDED] app/views/exceptions/not_acceptable.html.erb [ADDED] app/views/exceptions/not_found.html.erb [ADDED] autotest/discover.rb [ADDED] autotest/merb.rb [ADDED] autotest/merb_rspec.rb [ADDED] config/database.yml [ADDED] config/dependencies.rb [ADDED] config/environments/development.rb [ADDED] config/environments/production.rb [ADDED] config/environments/rake.rb [ADDED] config/environments/staging.rb [ADDED] config/environments/test.rb [ADDED] config/init.rb [ADDED] config/rack.rb [ADDED] config/router.rb [ADDED] public/javascripts/application.js [ADDED] public/stylesheets/master.css [ADDED] merb/merb-auth/setup.rb [ADDED] merb/merb-auth/strategies.rb [ADDED] merb/session/session.rb [ADDED] app/views/layout/application.html.erb
"rails my-first-app"みたいなやつです。
続けて、scaffold的なやつを。
$ cd my-first-app $ merb-gen resource article title:string,content:text Loading init file from /Users/norizo3/merbapp/my-first-app/config/init.rb Loading /Users/norizo3/merbapp/my-first-app/config/environments/development.rb Generating with resource generator: Loading init file from /Users/norizo3/merbapp/my-first-app/config/init.rb Loading /Users/norizo3/merbapp/my-first-app/config/environments/development.rb Loading init file from /Users/norizo3/merbapp/my-first-app/config/init.rb Loading /Users/norizo3/merbapp/my-first-app/config/environments/development.rb Loading init file from /Users/norizo3/merbapp/my-first-app/config/init.rb Loading /Users/norizo3/merbapp/my-first-app/config/environments/development.rb [ADDED] spec/models/article_spec.rb [ADDED] app/models/article.rb [ADDED] spec/requests/articles_spec.rb [ADDED] app/controllers/articles.rb [ADDED] app/views/articles/index.html.erb [ADDED] app/views/articles/show.html.erb [ADDED] app/views/articles/edit.html.erb [ADDED] app/views/articles/new.html.erb [ADDED] app/helpers/articles_helper.rb resources :articles route added to config/router.rb
これで完成です。
あとは「./script/server」みたいな感じにWebサーバを起動するだけです。
$ merb Loading init file from /Users/norizo3/merbapps/my-first-app/config/init.rb Loading /Users/norizo3/merbapps/my-first-app/config/environments/development.rb ~ Connecting to database... ~ Loaded slice 'MerbAuthSlicePassword' ... ~ Parent pid: 303 ~ Compiling routes... ~ Activating slice 'MerbAuthSlicePassword' ... merb : worker (port 4000) ~ Starting Mongrel at port 4000 merb : worker (port 4000) ~ Successfully bound to port 4000
ブラウザからアクセスして確認してみましょう。
と、ここまでが通常の道筋なのですが
昨日は最後のmerbコマンドを実行するとエラーになって起動しませんでした。
$ merb Loading init file from /Users/norizo3/merbapps/my-first-app/config/init.rb Loading /Users/norizo3/merbapps/my-first-app/config/environments/development.rb ~ Connecting to database... /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb:137:in `initialize': wrong number of arguments (8 for 1) (ArgumentError) from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb:137:in `new' from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb:137:in `normalize_uri' from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core/adapters/abstract_adapter.rb:44:in `initialize' from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb:159:in `initialize' from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core.rb:157:in `new' from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.6/lib/dm-core.rb:157:in `setup' from /opt/local/lib/ruby/gems/1.8/gems/merb_datamapper-1.0/lib/merb/orms/data_mapper/connection.rb:44:in `setup_connections' from /opt/local/lib/ruby/gems/1.8/gems/merb_datamapper-1.0/lib/merb/orms/data_mapper/connection.rb:27:in `connect' from /opt/local/lib/ruby/gems/1.8/gems/merb_datamapper-1.0/lib/merb_datamapper.rb:17:in `run' from /opt/local/lib/ruby/gems/1.8/gems/merb-core-1.0/lib/merb-core/bootloader.rb:99:in `run' from /opt/local/lib/ruby/gems/1.8/gems/merb-core-1.0/lib/merb-core/server.rb:172:in `bootup' from /opt/local/lib/ruby/gems/1.8/gems/merb-core-1.0/lib/merb-core/server.rb:42:in `start' from /opt/local/lib/ruby/gems/1.8/gems/merb-core-1.0/lib/merb-core.rb:169:in `start' from /opt/local/lib/ruby/gems/1.8/gems/merb-core-1.0/bin/merb:11 from /opt/local/bin/merb:19:in `load' from /opt/local/bin/merb:19
なにやら引数の数が合わないと言われています。
とりえずソースを追っかけたりしながら調べてみると、11月15日にaddressableというパッケージが2.0.0にアップデートされた事が原因でした。
なんという、ピンポイントでタイムリーな罠...orz
とりあえず、merbが起動するようになるpatchを書いてみました。
diff -urN dm-core-0.9.6.original/lib/dm-core/adapters/data_objects_adapter.rb dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb --- dm-core-0.9.6.original/lib/dm-core/adapters/data_objects_adapter.rb 2008-11-17 21:05:23.000000000 +0900 +++ dm-core-0.9.6/lib/dm-core/adapters/data_objects_adapter.rb 2008-11-17 03:59:23.000000000 +0900 @@ -134,7 +134,7 @@ query = uri_or_options.to_a.map { |pair| pair * '=' } * '&' query = nil if query == '' - return DataObjects::URI.parse(Addressable::URI.new(adapter, user, password, host, port, database, query, nil)) + return DataObjects::URI.parse(Addressable::URI.new(:scheme => adapter, :user => user, :passowrd => password, :host => host, :port => port, :path => database, :query => query, :fragment => nil)) end # TODO: clean up once transaction related methods move to dm-more/dm-transactions
diff -urN addressable-2.0.0.original/lib/addressable/uri.rb addressable-2.0.0/lib/addressable/uri.rb --- addressable-2.0.0.original/lib/addressable/uri.rb 2008-11-17 21:15:04.000000000 +0900 +++ addressable-2.0.0/lib/addressable/uri.rb 2008-11-17 21:18:55.000000000 +0900 @@ -1218,6 +1218,7 @@ self.user = options[:user] if options[:user] self.password = options[:password] if options[:password] self.userinfo = options[:userinfo] if options[:userinfo] + self.specified_port = options[:port].to_s if options[:port] self.host = options[:host] if options[:host] self.port = options[:port] if options[:port] self.authority = options[:authority] if options[:authority] @@ -1226,6 +1227,14 @@ self.fragment = options[:fragment] if options[:fragment] self.validation_deferred = false end + + ## + # The specified_port ecomponent for this URI. + # + # @return [String] The specified_port component. + def specified_port + return @specified_port + end ## # The scheme component for this URI.
とりえずはこれでmerbが起動するようになりますが、かなり応急処置的で足りないとこがあると思うので、アプリを作り込んでいくとすぐボロがでそうです。
ということで、dm-core他関連パッケージがアップデートされるまでは、旧バージョンのaddressableを使った方が無難です。
$ sudo gem uninstall addressable You have requested to uninstall the gem: addressable-2.0.0 dm-core-0.9.6 depends on [addressable (>= 1.0.4)] data_objects-0.9.6 depends on [addressable (>= 1.0.3)] If you remove this gems, one or more dependencies will not be met. Continue with Uninstall? [Yn] Y Successfully uninstalled addressable-2.0.0 $ sudo gem install addressable -v 1.0.4 Successfully installed addressable-1.0.4 1 gem installed Installing ri documentation for addressable-1.0.4... Installing RDoc documentation for addressable-1.0.4...