· 8 years ago · Jun 14, 2018, 10:00 PM
1# It somehow looks as if overwriting property writers in the remixee class works, whereas overwriting
2# property readers doesn’t. All works well if the overwriting methods are def’d in the enhance block in the
3# remixer class (including a module doesn’t work either). However, this is rather undesireable since it means
4# that the code cannot be shared among different remixers. Am I missing something obvious here?
5
6require 'rubygems'
7
8gem 'dm-core', '=0.9.11'
9gem 'dm-is-remixable', '=0.9.11'
10
11require 'dm-core'
12require 'dm-is-remixable'
13
14DataMapper::Logger.new(STDOUT, :debug)
15
16DataMapper.setup(:default, 'sqlite3:memory:')
17
18module Foo
19 include DataMapper::Resource
20 is :remixable
21 property :id, Serial
22 property :foo, String
23 module RemixeeInstanceMethods
24 def foo=(v)
25 puts "called overwritten #{self.class.name}#foo="
26 end
27 def foo
28 puts "called overwritten #{self.class.name}#foo"
29 end
30 end
31end
32
33# doesn't work
34class Bar
35 include DataMapper::Resource
36 property :id, Serial
37 remix n, :foos
38end
39
40# doesn't work
41class Baz
42 include DataMapper::Resource
43 property :id, Serial
44 remix n, :foos
45 enhance :foos do
46 include Foo::RemixeeInstanceMethods
47 end
48end
49
50# THIS WORKS
51# but isn't really desireable since i want to define
52# the shared logic in one place, and not in all clients
53# of the remixable module
54class Bam
55 include DataMapper::Resource
56 property :id, Serial
57 remix n, :foos
58 enhance :foos do
59 def foo=(v)
60 puts "called enhanced #{self.class.name}#foo="
61 end
62 def foo
63 puts "called enhanced #{self.class.name}#foo"
64 end
65 end
66end
67
68Bar.auto_migrate!
69Baz.auto_migrate!
70Bam.auto_migrate!
71
72BarFoo.auto_migrate!
73BazFoo.auto_migrate!
74BamFoo.auto_migrate!
75
76b = Bar.create
77
78puts
79puts "expecting: BarFoo#foo and BarFoo#foo="
80puts "-------------------------------------"
81bf = BarFoo.new
82bf.foo
83bf.foo = "foo"
84
85puts
86puts "expecting: BazFoo#foo and BazFoo#foo="
87puts "-------------------------------------"
88bf = BazFoo.new
89bf.foo
90bf.foo = "foo"
91
92puts
93puts "expecting: BamFoo#foo and BamFoo#foo="
94puts "-------------------------------------"
95bf = BamFoo.new
96bf.foo
97bf.foo = "foo"
98
99# mungo:trippings snusnu$ ruby ../../Desktop/remixable.rb
100# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BarFoo
101# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BazFoo
102# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BamFoo
103# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.003555) DROP TABLE IF EXISTS "bars"
104# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000104) PRAGMA table_info('bars')
105# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000036) SELECT sqlite_version(*)
106# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002571) CREATE TABLE "bars" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
107# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.004250) DROP TABLE IF EXISTS "bazs"
108# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000021) PRAGMA table_info('bazs')
109# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002077) CREATE TABLE "bazs" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
110# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002335) DROP TABLE IF EXISTS "bams"
111# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000026) PRAGMA table_info('bams')
112# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002980) CREATE TABLE "bams" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
113# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.001990) DROP TABLE IF EXISTS "bar_foos"
114# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000020) PRAGMA table_info('bar_foos')
115# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.003108) CREATE TABLE "bar_foos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "foo" VARCHAR(50), "bar_id" INTEGER NOT NULL)
116# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002462) DROP TABLE IF EXISTS "baz_foos"
117# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000035) PRAGMA table_info('baz_foos')
118# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002796) CREATE TABLE "baz_foos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "foo" VARCHAR(50), "baz_id" INTEGER NOT NULL)
119# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002167) DROP TABLE IF EXISTS "bam_foos"
120# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000017) PRAGMA table_info('bam_foos')
121# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002434) CREATE TABLE "bam_foos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "foo" VARCHAR(50), "bam_id" INTEGER NOT NULL)
122# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002933) INSERT INTO "bars" DEFAULT VALUES
123#
124# expecting: BarFoo#foo and BarFoo#foo=
125# -------------------------------------
126# called overwritten BarFoo#foo=
127#
128# expecting: BazFoo#foo and BazFoo#foo=
129# -------------------------------------
130# called overwritten BazFoo#foo=
131#
132# expecting: BamFoo#foo and BamFoo#foo=
133# -------------------------------------
134# called enhanced BamFoo#foo
135# called enhanced BamFoo#foo=