Rails 2.0.0RC1とActiveModel
Twitterで2.0.0RC1が出たよ、と教えていただきました。
http://blog.rubyonrails.org/2007/11/9/rails-2-0-release-candidate-1
とりあえずsvn exportしてみたら見慣れぬコンポーネントが。その名はActiveModel。
ちょっwww、そんなのPreviewReleaseの頃にはなかった。
とりえあずREADMEを読んでみました。意訳&ごまかし訳多数。レオさんの登場が望まれます。
ActiveModel
http://svn.rubyonrails.org/rails/trunk/activemodel/README
Totally experimental library that aims to extract common model mixins from
ActiveRecord for use in ActiveResource (and other similar libraries).
This is in a very rough state (no autotest or spec rake tasks set up yet),
so please excuse the mess.
ActiveRecordからActiveResourceその他で流用できそうなmixinを取り出してみるテスト。まだautotestやspecすらない粗々な状況なので、手加減してね。
Here's what I plan to extract:
* ActiveModel::Observing
* ActiveModel::Callbacks
* ActiveModel::Validations# for ActiveResource params and ActiveRecord options
* ActiveModel::Scoping
この辺を分離して流用できるようにする予定。
- ActiveModel::Observing (キャッシュのクリアとか (AR::Observing))
- ActiveModel::Callbacks (before_saveとかafter_validationとか)
- ActiveModel::Validations (バリデーション。これを使えるようになるのは喜ぶ人多いでしょうね)
- ActiveModel::Scoping (with_scope()まわり。AResのクエリパラメータの指定とか。)
- ActiveModel::Serialization (to_json,to_xmlはどっちもASのを呼ぶだけだけど、一ヶ所で定義できるようにした、ということかな?)
I'm trying to keep ActiveRecord compatibility where possible, but I'm
annotating the spots where I'm diverging a bit.
ActiveRecordの互換性はできるだけ保とうとおもうけど、、、
感想
ということで驚きましたが、ARとAResでDRYじゃないところを直すためにsuperclassをextractしたよ、と言われれば「ですよねー」というしかないわけです。
これがうまくまとまると、自分でARでもAResでもないモデルクラスを作る場合にすごく嬉しくなるんじゃないでしょうか。あとでもうちょっと詳しく読んでみます。