Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created December 7, 2012 04:50
Show Gist options
  • Save kimoto/4230842 to your computer and use it in GitHub Desktop.
Save kimoto/4230842 to your computer and use it in GitHub Desktop.
O/R MapperであるDataMapperの遅延評価の仕組みのサンプル
#!/bin/env ruby
# encoding: utf-8
# Author: kimoto
require 'data_mapper'
class Channel
include DataMapper::Resource
property :id, Serial
property :name, String
end
DataMapper.setup(:default, "mysql://127.0.0.1/sample")
DataMapper.finalize
DataMapper.auto_upgrade!
100.times{ |n|
Channel.create(:name => "ch#{n}")
}
DataMapper.logger.set_log STDERR, :debug, "", true # ログ出力を有効化
channels = Channel.all
channels.each{ |ch|
ch.name # nop
}
# このとき実際に発行されるSQLは以下のみ
# (0.000365) SELECT `id`, `name` FROM `channels` ORDER BY `id`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment