· 8 years ago · Mar 01, 2018, 03:40 PM
1#!/usr/bin/env ruby
2
3require 'rubygems'
4
5gem 'dm-core', '=0.9.10'
6require 'dm-core'
7
8DataMapper::Logger.new(STDOUT, :debug)
9DataMapper.setup(:default, 'sqlite3:memory:')
10
11
12class Person
13 include DataMapper::Resource
14 property :id, Serial
15 property :name, String
16
17 def greeting
18 "I'm a #{self.class} named #{name}"
19 end
20end
21puts "Person.storage_name = #{Person.storage_name}"
22
23class Student < Person
24 def greeting
25 "Behold, #{super}"
26 end
27end
28puts "Student.storage_name = #{Student.storage_name}"
29
30DataMapper.auto_migrate!
31
32Person.create :name => "person"
33Student.create :name => "student"
34
35Person.all.each do |p|
36 puts "#{p.greeting} and my record is stored in #{p.class.storage_name}"
37end
38
39Student.all.each do |s|
40 puts "#{s.greeting} and my record is stored in #{s.class.storage_name}"
41end
42
43p = Person.first(:name => "person")
44s = Student.first(:name => "student")
45
46if p && s
47 puts "found Person #{p.name} by name"
48 puts "found Student #{s.name} by name"
49end
50
51
52# Person.storage_name = people
53# Student.storage_name = people
54# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.003415) DROP TABLE IF EXISTS "people"
55# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000050) PRAGMA table_info('people')
56# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000031) SELECT sqlite_version(*)
57# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.002617) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50))
58# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.002917) INSERT INTO "people" ("name") VALUES ('person')
59# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.002364) INSERT INTO "people" ("name") VALUES ('student')
60# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000201) SELECT "id", "name" FROM "people" ORDER BY "id"
61# I'm a Person named person and my record is stored in people
62# I'm a Person named student and my record is stored in people
63# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000039) SELECT "id", "name" FROM "people" ORDER BY "id"
64# Behold, I'm a Student named person and my record is stored in people
65# Behold, I'm a Student named student and my record is stored in people
66# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000043) SELECT "id", "name" FROM "people" WHERE ("name" = 'person') ORDER BY "id" LIMIT 1
67# Wed, 18 Feb 2009 03:07:42 GMT ~ debug ~ (0.000037) SELECT "id", "name" FROM "people" WHERE ("name" = 'student') ORDER BY "id" LIMIT 1
68# found Person person by name
69# found Student student by name