Heroku経由でSinatraを使ってみているのですが、ModelとしてSequelを使ってみることにしました。

###Userクラスを作る
DBをオンメモリにしておいて、テーブル作成・クラスの宣言
shell>>
% irb
require 'rubygems'
require 'sequel'

DB = Sequel.sqlite
DB << "CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR(255) not NULL)"

class User < Sequel::Model
end
<<--

これから、User.なんとかでデータの作成などできます。

shell>>
User.create(:name => 'satoko')

User.find(:name => 'satoko')
User.find(:id => 1).update(:name => 'satoko')
User.find(:id => 1).delete
User.first

User.all
=> [#<User @values={:name=>"satoko", :id=>1}>]

DB[:users].filter(:name => 'satoko').sql
=> "SELECT * FROM users WHERE (name = 'satoko')"
<<--

###findは1件操作なので、複数件の場合はfilterで行う
shell>>
User.filter("id > ?", 0).limit(10).each {|u| p[:name]}
=> #<Sequel::SQLite::Dataset: "SELECT * FROM users WHERE (id > 0)">
<<--

また、@orepuriさんに教えてもらったのですが、all、first、eachなどがあって初めてSQLが発行されるそうで、なかなか面白いです

###References
Sequel概要(2年前の資料なので、コード的には古いかもしれません)
http://www.slideshare.net/zhesto/ruby-off-rails-japanese

チートシート
http://sequel.rubyforge.org/rdoc/files/doc/cheat_sheet_rdoc.html
http://cheat.errtheblog.com/s/sequel/

Classリファレンス:ModelやDatasetなど
http://sequel.rubyforge.org/documentation.html

posted by satoko satoko on Wed 16 Sep 2009 at 21:30 with 0 comments
Contents rssrss
NSAssertやNSLogを無効にする:-DNS_BLOCK_ASSERTIONS=1とNS_BLOCK_ASSERTIONS
GDBでview.frameを表示させる
Core Animationについてのリンク集
[iPhone] new BSDライセンスについてライセンス引用のメモ
Implementing a Container View Controller in UIViewController Class Reference
UITableView用のNSIndexPathの作り方
Xcodeのテンプレート、View-based ApplicationとWindow-based Applicationの違い
CopyOnSelectのLion対応
Xcode4のCode Snippetsを別のPCに移動する+gitで管理する
[iPhone] Singleton: iOS4以降はGCDのdispatch_onceを使う
Tags
iphone開発gitIBInterfaceBuilderiOSSIMBLSnippetsterminaltwitterxcodeXcode4
Comments rssrss
guest guest: Muchos turistas buscan la mejor forma de llegar a ... 05/30 03:19
guest guest: San Antonio de Areco es conocido como la cuna de l... 05/27 19:47
guest guest: Optimizar el tiempo durante un viaje de negocios e... 05/18 00:46
guest guest: Para muchos turistas, el transporte publico y la l... 05/15 18:22
Services from s21g
YOMU Web小説リーダー
小説投稿サイトの公式ページを開き、WebView表示と読み上げ向け表示を切り替えて、移動中や作業中にもWeb小説の続きを聴きやすくするiPhoneアプリです。
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
個人開発者のためのホスティング一体型ノートサービス
ハンドミラー
iPhone向けの手鏡アプリ
ツイプロ(twpro)
Twitterプロフィールの高速検索エンジン