Ohm を使っていると色々と不満な点が見えてきます
- @attributes を得る手段がない (#attributes が column_names を返す)
- create!, save! がない
- first, all がない
- ...
これは AR 脳というよりも、ARの洗練されたAPIと比べてしまうと、
Ohm の未成熟さが浮き彫りになってしまうためだと思います。
ohm-arfreaks
そこで、ARのメソッドをOhm上で使えるようにしたラッパーを作成しました。
http://github.com/maiha/ohm-arfreaks
インストール
shell>>
% gem install ohm-arfreaks
<<--
例
ruby>>
require 'ohm'
require 'ohm-arfreaks' # これを追加
class Video < Ohm::Model
attribute :url
set :tags
end
Video.create!(:url=>"a")
Video.first.attributes
=> {: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 相当のことは以下のコードでできるようになります。
ruby>>
File.open("videos.yml", "w+") do |f|
hash = Video.all.inject({}){|h,v| h[v.id]=v.attributes;h}
f.print hash.to_yaml
end
<<--