· 8 years ago · Mar 09, 2018, 12:50 AM
1require 'rubygems'
2
3gem 'addressable', '2.0.1'
4gem 'data_objects', '0.10.0'
5gem 'do_sqlite3', '0.10.0'
6gem 'dm-core', '0.10.0'
7require 'dm-core'
8
9class Post
10 include DataMapper::Resource
11
12 def self.default_repository_name
13 :post
14 end
15
16 property :id, Serial
17 property :title, String
18 property :published, Boolean
19end
20
21DataMapper.setup(:post, "sqlite3::memory:")
22DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)
23DataMapper.auto_migrate!
24
25
26post = Post.create(:title => 'Post 1', :published => true)
27post_2 = Post.create(:title => 'Post 2', :published => false)
28
29p Post.all(:published => true)
30p Post.all(:published => false) # No request made. See result in line 43. but without request before
31p Post.all(:published.not => true) # Result same than previous request ? no :(
32
33
34# STDOUT
35# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000076) SELECT sqlite_version(*)
36# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000111) DROP TABLE IF EXISTS "posts"
37# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000025) PRAGMA table_info("posts")
38# 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)
39# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000097) INSERT INTO "posts" ("title", "published") VALUES ('Post 1', 't')
40# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000086) INSERT INTO "posts" ("title", "published") VALUES ('Post 2', 'f')
41# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000082) SELECT "id", "title", "published" FROM "posts" WHERE "published" = 't' ORDER BY "id"
42# [#<Post @id=1 @title="Post 1" @published=true>]
43# []
44# Fri, 26 Jun 2009 06:04:45 GMT ~ debug ~ (0.000082) SELECT "id", "title", "published" FROM "posts" WHERE "published" <> 't' ORDER BY "id"
45# [#<Post @id=2 @title="Post 2" @published=false>]
46#