· 7 years ago · Sep 04, 2018, 11:58 AM
1Rails3 and Datamapper
2gem install dm-core
3gem install dm-migrations
4gem install dm-sqlite-adapter
5gem install dm-chunked_query
6
7require 'rubygems'
8require 'dm-core'
9require 'dm-migrations'
10require 'dm-chunked_query'
11
12class Person
13 include DataMapper::Resource
14
15 property :id, Serial
16 property :name, String
17 property :hobby, String
18 property :country, String
19 property :continent, String
20
21 def self.european
22 all(:continent => 'Europe')
23 end
24
25 def self.hackers
26 all(:hobby => 'Hacking')
27 end
28
29 def self.named(name)
30 all(:name => name)
31 end
32end
33
34DataMapper::Logger.new($stdout, :debug)
35DataMapper.setup(:default, 'sqlite::memory:')
36
37DataMapper.finalize.auto_migrate!
38
391.upto(10) do |i|
40 Person.create(:name => "Alex", :hobby => 'Hacking', :country => "Country-#{i}", :continent => 'Europe')
41end
42
43# you could even skip the explicit call to #all
44# i just left it in there because it reads nicely
45Person.all.european.hackers.named('Alex').chunks(5).each_with_index do |chunk, idx|
46 puts "Rendering page #{idx + 1}"
47 chunk.each do |person|
48 puts "Someone named #{person.name} who lives in #{person.country}"
49 end
50end
51
52__END__
53
54ruby-1.9.2-p180@datamapper mungo:dm-rails snusnu$ ruby test.rb
55 ~ (0.000102) SELECT sqlite_version(*)
56 ~ (0.000132) DROP TABLE IF EXISTS "people"
57 ~ (0.000013) PRAGMA table_info("people")
58 ~ (0.000315) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "hobby" VARCHAR(50), "country" VARCHAR(50), "continent" VARCHAR(50))
59 ~ (0.000049) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-1', 'Europe')
60 ~ (0.000056) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-2', 'Europe')
61 ~ (0.000044) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-3', 'Europe')
62 ~ (0.000043) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-4', 'Europe')
63 ~ (0.000037) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-5', 'Europe')
64 ~ (0.000038) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-6', 'Europe')
65 ~ (0.000035) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-7', 'Europe')
66 ~ (0.000035) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-8', 'Europe')
67 ~ (0.000036) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-9', 'Europe')
68 ~ (0.000039) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-10', 'Europe')
69 ~ (0.000069) SELECT "id", "name", "hobby", "country", "continent" FROM "people" WHERE ("continent" = 'Europe' AND "hobby" = 'Hacking' AND "name" = 'Alex') ORDER BY "id"
70Rendering page 1
71Someone named Alex who lives in Country-1
72Someone named Alex who lives in Country-2
73Someone named Alex who lives in Country-3
74Someone named Alex who lives in Country-4
75Someone named Alex who lives in Country-5
76Rendering page 2
77Someone named Alex who lives in Country-6
78Someone named Alex who lives in Country-7
79Someone named Alex who lives in Country-8
80Someone named Alex who lives in Country-9
81Someone named Alex who lives in Country-10