· 8 years ago · Jul 19, 2018, 12:38 PM
1#!/usr/bin/env ruby
2#
3# A one file test to show ...
4require 'rubygems'
5require 'dm-core'
6
7
8# setup the logger
9DataMapper::Logger.new(STDOUT, :debug)
10
11# connect to the DB
12DataMapper.setup(:default, 'sqlite3::memory:')
13
14class Person
15
16 include DataMapper::Resource
17
18 property :id, Serial
19 property :email, String, :unique => true
20 property :thing, Integer
21
22 has n, :mailers # A Person sends mailers.
23 has n, :mailer_people, 'MailerPerson'
24 has n, :conversations, 'Mailer', :through => :mailer_people, :via => :mailer
25
26end
27
28class Mailer
29
30 include DataMapper::Resource
31
32 property :id, Serial
33 property :subject, String
34 property :body, Text
35 property :sent_at, DateTime
36 property :person_id, Integer, :required => false, :min => 0
37
38 belongs_to :person, :required => false
39 has n, :mailer_people, 'MailerPerson'
40 has n, :people, 'Person', :through => :mailer_people
41
42end
43
44class MailerPerson
45 include DataMapper::Resource
46 property :id, Serial
47 property :mailer_id, Integer, :required => true, :min => 0
48 property :person_id, Integer, :required => true, :min => 0
49 belongs_to :mailer
50 belongs_to :person
51end
52
53DataMapper.auto_migrate!
54
55a = Person.new(:email => "greg@example.com")
56a.save
57a = Person.new(:email => "stan@example.com", :thing => 1)
58a.save
59a = Person.new(:email => "jim@example.com", :thing => 1)
60a.save
61
62m = Mailer.new()
63m.subject = "Subject"
64m.body = "Body"
65m.person = Person.first # Greg
66m.people = Person.all(:thing => 1)
67m.save
68
69
70__END__
71
72[ree-1.8.7-2009.10] mungo:snippets snusnu$ ruby advany.rb
73 ~ (0.000038) SELECT sqlite_version(*)
74 ~ (0.000058) DROP TABLE IF EXISTS "people"
75 ~ (0.000013) DROP TABLE IF EXISTS "mailers"
76 ~ (0.000011) DROP TABLE IF EXISTS "mailer_people"
77 ~ (0.000010) PRAGMA table_info("people")
78 ~ (0.000268) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(50), "thing" INTEGER)
79 ~ (0.000008) PRAGMA table_info("mailers")
80 ~ (0.000104) CREATE TABLE "mailers" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "subject" VARCHAR(50), "body" TEXT, "sent_at" TIMESTAMP, "person_id" INTEGER)
81 ~ (0.000007) PRAGMA table_info("mailer_people")
82 ~ (0.000099) CREATE TABLE "mailer_people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "mailer_id" INTEGER NOT NULL, "person_id" INTEGER NOT NULL)
83 ~ (0.000041) INSERT INTO "people" ("email") VALUES ('greg@example.com')
84 ~ (0.000038) INSERT INTO "people" ("email", "thing") VALUES ('stan@example.com', 1)
85 ~ (0.000037) INSERT INTO "people" ("email", "thing") VALUES ('jim@example.com', 1)
86 ~ (0.000036) SELECT "id", "email", "thing" FROM "people" ORDER BY "id" LIMIT 1
87 ~ (0.000036) SELECT "id", "email", "thing" FROM "people" WHERE "thing" = 1 ORDER BY "id"
88 ~ (0.000054) INSERT INTO "mailers" ("subject", "body", "person_id") VALUES ('Subject', 'Body', 1)
89 ~ (0.000047) SELECT "id", "mailer_id", "person_id" FROM "mailer_people" WHERE ("mailer_id" = 1 AND "person_id" = 2) ORDER BY "id" LIMIT 1
90 ~ (0.000051) INSERT INTO "mailer_people" ("mailer_id", "person_id") VALUES (1, 2)
91 ~ (0.000045) SELECT "id", "mailer_id", "person_id" FROM "mailer_people" WHERE ("mailer_id" = 1 AND "person_id" = 3) ORDER BY "id" LIMIT 1
92 ~ (0.000043) INSERT INTO "mailer_people" ("mailer_id", "person_id") VALUES (1, 3)