Ohm を使っていると色々と不満な点が見えてきます

  • @attributes を得る手段がない (#attributes が column_names を返す)
  • create!, save! がない
  • first, all がない
  • ...

これは AR 脳というよりも、ARの洗練されたAPIと比べてしまうと、 Ohm の未成熟さが浮き彫りになってしまうためだと思います。

ohm-arfreaks

そこで、ARのメソッドをOhm上で使えるようにしたラッパーを作成しました。

http://github.com/maiha/ohm-arfreaks

インストール

   1  % gem install ohm-arfreaks

   1  require 'ohm'
   2  require 'ohm-arfreaks'   # これを追加
   3  
   4  class Video < Ohm::Model
   5    attribute :url
   6    set       :tags
   7  end
   8  
   9  Video.create!(:url=>"a")
  10  Video.first.attributes
  11  => {:url=>"a", :tags=>[]}

以下のメソッドが利用可能です (0.1.0 現在)

  • self.primary_key
  • self.columns
  • self.column_names
  • self.content_columns
  • self.columns_hash
  • self.create!
  • save!
  • self.first
  • self.last
  • self.count
  • self.delete_all
  • new_record?
  • attributes

ARと比べると使えるメソッドは少ないですが、 これだけでも随分Ohmが快適になると思います。

backup

例えば、redis はどこに DB を作っているかわかり辛いので 時々バックアップが欲しくなるのですが、 昔懐かしのar_fixtures plugin 相当のことは以下のコードでできるようになります。

   1  File.open("videos.yml", "w+") do |f|
   2    hash = Video.all.inject({}){|h,v| h[v.id]=v.attributes;h}
   3    f.print hash.to_yaml
   4  end

posted by Png maiha on Tue 16 Mar 2010 at 19:16
Contents
[NoSQL] ohm-arfreaks (redis用のAR風ORM) の使い方
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ