[ruby] Merb を使ってみた -1

gem install してみると merb 0.5.3、ここは 0.9 系が使いたいので git head からインストールする。
(change log を見ると 0.5.3 の次が 0.9.0)

OSX Tiger + ruby 1.8.6 + MacPorts でインストールは滞り無く完了、
途中エラーが出た場合はライブラリや gem の有無を疑う。glib2無いとか、rspec無いとか。

アプリを書こう

参照

datamapper, mock


init.rb を触る。

use_orm :datamapper
use_test :test_unit
use_test :rspec

rake に db: 系のタスクが追加される。

merb

を実行すると database.yml のサンプルが生成される。
その config/database.yml.sample を書き換えて

# This is a sample database file for the DataMapper ORM
:development: &defaults
  :adapter: sqlite3
  :database: db/development.sqlite3
  :host: localhost
#  :username: the_user
#  :password: secrets

:test:
  <<: *defaults
  :database: db/test.sqlite3

もう一度 merb を実行する。今度は通った。

merb-gen
merb-gen resource --help

を叩いて merb-gen で何が出来るかを確認。

Article に対応した model/controller を書いてみよう。

merb-gen resource article

spec が出来たので autotest を走らせる。

model から書いていく。
article_spec.rb

describe Article do
  before(:each) do
    @article = Article.new
  end

  it { @article.should respond_to(:title) }
  it { @article.should respond_to(:body)}
end

article.rb

class Article < DataMapper::Base
  property :title, :string
  property :body, :text
end

(つづく)