Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# It somehow looks as if overwriting property writers in the remixee class works, whereas overwriting
# property readers doesn’t. All works well if the overwriting methods are def’d in the enhance block in the
# remixer class (including a module doesn’t work either). However, this is rather undesireable since it means 
# that the code cannot be shared among different remixers. Am I missing something obvious here?

require 'rubygems'

gem 'dm-core',         '=0.9.11'
gem 'dm-is-remixable', '=0.9.11'

require 'dm-core'
require 'dm-is-remixable'

DataMapper::Logger.new(STDOUT, :debug)

DataMapper.setup(:default, 'sqlite3:memory:')

module Foo
  include DataMapper::Resource
  is :remixable
  property :id,  Serial
  property :foo, String
  module RemixeeInstanceMethods
    def foo=(v)
      puts "called overwritten #{self.class.name}#foo="
    end
    def foo
      puts "called overwritten #{self.class.name}#foo"
    end
  end
end

# doesn't work
class Bar
  include DataMapper::Resource
  property :id, Serial
  remix n, :foos
end

# doesn't work
class Baz
  include DataMapper::Resource
  property :id, Serial
  remix n, :foos
  enhance :foos do
    include Foo::RemixeeInstanceMethods
  end
end

# THIS WORKS
# but isn't really desireable since i want to define
# the shared logic in one place, and not in all clients
# of the remixable module
class Bam
  include DataMapper::Resource
  property :id, Serial
  remix n, :foos
  enhance :foos do
    def foo=(v)
      puts "called enhanced #{self.class.name}#foo="
    end
    def foo
      puts "called enhanced #{self.class.name}#foo"
    end
  end
end

Bar.auto_migrate!
Baz.auto_migrate!
Bam.auto_migrate!

BarFoo.auto_migrate!
BazFoo.auto_migrate!
BamFoo.auto_migrate!

b = Bar.create

puts
puts "expecting: BarFoo#foo and BarFoo#foo="
puts "-------------------------------------"
bf = BarFoo.new
bf.foo
bf.foo = "foo"

puts
puts "expecting: BazFoo#foo and BazFoo#foo="
puts "-------------------------------------"
bf = BazFoo.new
bf.foo
bf.foo = "foo"

puts
puts "expecting: BamFoo#foo and BamFoo#foo="
puts "-------------------------------------"
bf = BamFoo.new
bf.foo
bf.foo = "foo"

# mungo:trippings snusnu$ ruby ../../Desktop/remixable.rb 
# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BarFoo
# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BazFoo
# Tue, 31 Mar 2009 02:00:41 GMT ~ info ~ Generating Remixed Model: BamFoo
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.003555) DROP TABLE IF EXISTS "bars"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000104) PRAGMA table_info('bars')
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000036) SELECT sqlite_version(*)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002571) CREATE TABLE "bars" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.004250) DROP TABLE IF EXISTS "bazs"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000021) PRAGMA table_info('bazs')
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002077) CREATE TABLE "bazs" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002335) DROP TABLE IF EXISTS "bams"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000026) PRAGMA table_info('bams')
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002980) CREATE TABLE "bams" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.001990) DROP TABLE IF EXISTS "bar_foos"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000020) PRAGMA table_info('bar_foos')
# 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)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002462) DROP TABLE IF EXISTS "baz_foos"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000035) PRAGMA table_info('baz_foos')
# 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)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002167) DROP TABLE IF EXISTS "bam_foos"
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.000017) PRAGMA table_info('bam_foos')
# 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)
# Tue, 31 Mar 2009 02:00:41 GMT ~ debug ~ (0.002933) INSERT INTO "bars" DEFAULT VALUES
# 
# expecting: BarFoo#foo and BarFoo#foo=
# -------------------------------------
# called overwritten BarFoo#foo=
# 
# expecting: BazFoo#foo and BazFoo#foo=
# -------------------------------------
# called overwritten BazFoo#foo=
# 
# expecting: BamFoo#foo and BamFoo#foo=
# -------------------------------------
# called enhanced BamFoo#foo
# called enhanced BamFoo#foo=