Skip to content

Instantly share code, notes, and snippets.

@shingara
Created June 26, 2009 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shingara/136325 to your computer and use it in GitHub Desktop.
Save shingara/136325 to your computer and use it in GitHub Desktop.
require 'rubygems'
gem 'addressable', '2.0.1'
gem 'data_objects', '0.10.0'
gem 'do_sqlite3', '0.10.0'
gem 'dm-core', '0.10.0'
require 'dm-core'
class Post
include DataMapper::Resource
def self.default_repository_name
:post
end
property :id, Serial
property :title, String
property :published, Boolean
end
DataMapper.setup(:post, "sqlite3::memory:")
DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)
DataMapper.auto_migrate!
post = Post.create(:title => 'Post 1', :published => true)
post_2 = Post.create(:title => 'Post 2', :published => false)
p Post.all(:published => true)
p Post.all(:published => false) # No request made. See result in line 43. but without request before
p Post.all(:published.not => true) # Result same than previous request ? no :(
# STDOUT
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000076) SELECT sqlite_version(*)
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000111) DROP TABLE IF EXISTS "posts"
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000025) PRAGMA table_info("posts")
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000515) CREATE TABLE "posts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "title" VARCHAR(50), "published" BOOLEAN)
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000097) INSERT INTO "posts" ("title", "published") VALUES ('Post 1', 't')
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000086) INSERT INTO "posts" ("title", "published") VALUES ('Post 2', 'f')
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000082) SELECT "id", "title", "published" FROM "posts" WHERE "published" = 't' ORDER BY "id"
# [#<Post @id=1 @title="Post 1" @published=true>]
# []
# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000082) SELECT "id", "title", "published" FROM "posts" WHERE "published" <> 't' ORDER BY "id"
# [#<Post @id=2 @title="Post 2" @published=false>]
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment