Commit | Line | Data |
fcec2383 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
047248f9 |
6 | use Test::More; |
7 | |
8 | BEGIN { |
f808a99e |
9 | eval "use DBM::Deep 1.0003;"; |
77a18c28 |
10 | plan skip_all => "DBM::Deep 1.0003 (or greater) is required for this test" if $@; |
a60285b3 |
11 | eval "use DateTime::Format::MySQL;"; |
12 | plan skip_all => "DateTime::Format::MySQL is required for this test" if $@; |
7ff56534 |
13 | plan tests => 88; |
047248f9 |
14 | } |
15 | |
fcec2383 |
16 | use Test::Exception; |
17 | |
18 | BEGIN { |
0f051d58 |
19 | # in case there are leftovers |
20 | unlink('newswriter.db') if -e 'newswriter.db'; |
21 | } |
22 | |
23 | END { |
24 | unlink('newswriter.db') if -e 'newswriter.db'; |
25 | } |
26 | |
7ff56534 |
27 | |
fcec2383 |
28 | |
b68e5362 |
29 | =pod |
30 | |
31 | This example creates a very basic Object Database which |
32 | links in the instances created with a backend store |
33 | (a DBM::Deep hash). It is by no means to be taken seriously |
77a18c28 |
34 | as a real-world ODB, but is a proof of concept of the flexibility |
35 | of the ::Instance protocol. |
b68e5362 |
36 | |
37 | =cut |
38 | |
fcec2383 |
39 | BEGIN { |
40 | |
8479d544 |
41 | package Moose::POOP::Meta::Instance; |
fcec2383 |
42 | use Moose; |
43 | |
44 | use DBM::Deep; |
45 | |
46 | extends 'Moose::Meta::Instance'; |
47 | |
48 | { |
b68e5362 |
49 | my %INSTANCE_COUNTERS; |
fcec2383 |
50 | |
51 | my $db = DBM::Deep->new({ |
047248f9 |
52 | file => "newswriter.db", |
53 | autobless => 1, |
54 | locking => 1, |
fcec2383 |
55 | }); |
fcec2383 |
56 | |
fcec2383 |
57 | sub _reload_db { |
b68e5362 |
58 | #use Data::Dumper; |
59 | #warn Dumper $db; |
047248f9 |
60 | $db = undef; |
fcec2383 |
61 | $db = DBM::Deep->new({ |
047248f9 |
62 | file => "newswriter.db", |
63 | autobless => 1, |
64 | locking => 1, |
65 | }); |
fcec2383 |
66 | } |
67 | |
68 | sub create_instance { |
b68e5362 |
69 | my $self = shift; |
5cf3dbcf |
70 | my $class = $self->associated_metaclass->name; |
b68e5362 |
71 | my $oid = ++$INSTANCE_COUNTERS{$class}; |
72 | |
73 | $db->{$class}->[($oid - 1)] = {}; |
fcec2383 |
74 | |
75 | $self->bless_instance_structure({ |
b68e5362 |
76 | oid => $oid, |
77 | instance => $db->{$class}->[($oid - 1)] |
fcec2383 |
78 | }); |
79 | } |
80 | |
81 | sub find_instance { |
82 | my ($self, $oid) = @_; |
5cf3dbcf |
83 | my $instance = $db->{$self->associated_metaclass->name}->[($oid - 1)]; |
b68e5362 |
84 | $self->bless_instance_structure({ |
85 | oid => $oid, |
86 | instance => $instance |
87 | }); |
88 | } |
89 | |
90 | sub clone_instance { |
91 | my ($self, $instance) = @_; |
92 | |
93 | my $class = $self->{meta}->name; |
94 | my $oid = ++$INSTANCE_COUNTERS{$class}; |
95 | |
96 | my $clone = tied($instance)->clone; |
fcec2383 |
97 | |
98 | $self->bless_instance_structure({ |
99 | oid => $oid, |
b68e5362 |
100 | instance => $clone |
101 | }); |
102 | } |
fcec2383 |
103 | } |
104 | |
105 | sub get_instance_oid { |
106 | my ($self, $instance) = @_; |
107 | $instance->{oid}; |
108 | } |
109 | |
fcec2383 |
110 | sub get_slot_value { |
111 | my ($self, $instance, $slot_name) = @_; |
112 | return $instance->{instance}->{$slot_name}; |
113 | } |
114 | |
115 | sub set_slot_value { |
116 | my ($self, $instance, $slot_name, $value) = @_; |
117 | $instance->{instance}->{$slot_name} = $value; |
118 | } |
119 | |
120 | sub is_slot_initialized { |
121 | my ($self, $instance, $slot_name, $value) = @_; |
122 | exists $instance->{instance}->{$slot_name} ? 1 : 0; |
123 | } |
124 | |
125 | sub weaken_slot_value { |
e67a0fca |
126 | confess "Not sure how well DBM::Deep plays with weak refs, Rob says 'Write a test'"; |
fcec2383 |
127 | } |
128 | |
129 | sub inline_slot_access { |
130 | my ($self, $instance, $slot_name) = @_; |
131 | sprintf "%s->{instance}->{%s}", $instance, $slot_name; |
132 | } |
133 | |
8479d544 |
134 | package Moose::POOP::Meta::Class; |
8ecb1fa0 |
135 | use Moose; |
fcec2383 |
136 | |
137 | extends 'Moose::Meta::Class'; |
138 | |
139 | override 'construct_instance' => sub { |
d7af0635 |
140 | my $class = shift; |
141 | my $params = @_ == 1 ? $_[0] : {@_}; |
142 | return $class->get_meta_instance->find_instance($params->{oid}) |
143 | if $params->{oid}; |
fcec2383 |
144 | super(); |
145 | }; |
fcec2383 |
146 | |
8479d544 |
147 | } |
fcec2383 |
148 | { |
8479d544 |
149 | package Moose::POOP::Object; |
8479d544 |
150 | use metaclass 'Moose::POOP::Meta::Class' => ( |
5cf3dbcf |
151 | instance_metaclass => 'Moose::POOP::Meta::Instance' |
fcec2383 |
152 | ); |
153 | use Moose; |
154 | |
155 | sub oid { |
156 | my $self = shift; |
157 | $self->meta |
158 | ->get_meta_instance |
159 | ->get_instance_oid($self); |
160 | } |
8479d544 |
161 | |
162 | } |
163 | { |
fcec2383 |
164 | package Newswriter::Author; |
fcec2383 |
165 | use Moose; |
166 | |
8479d544 |
167 | extends 'Moose::POOP::Object'; |
fcec2383 |
168 | |
169 | has 'first_name' => (is => 'rw', isa => 'Str'); |
170 | has 'last_name' => (is => 'rw', isa => 'Str'); |
171 | |
172 | package Newswriter::Article; |
fcec2383 |
173 | use Moose; |
174 | use Moose::Util::TypeConstraints; |
175 | |
176 | use DateTime::Format::MySQL; |
177 | |
8479d544 |
178 | extends 'Moose::POOP::Object'; |
fcec2383 |
179 | |
180 | subtype 'Headline' |
181 | => as 'Str' |
182 | => where { length($_) < 100 }; |
183 | |
184 | subtype 'Summary' |
185 | => as 'Str' |
186 | => where { length($_) < 255 }; |
187 | |
188 | subtype 'DateTimeFormatString' |
189 | => as 'Str' |
190 | => where { DateTime::Format::MySQL->parse_datetime($_) }; |
191 | |
192 | enum 'Status' => qw(draft posted pending archive); |
193 | |
194 | has 'headline' => (is => 'rw', isa => 'Headline'); |
195 | has 'summary' => (is => 'rw', isa => 'Summary'); |
196 | has 'article' => (is => 'rw', isa => 'Str'); |
197 | |
198 | has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString'); |
199 | has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString'); |
200 | |
201 | has 'author' => (is => 'rw', isa => 'Newswriter::Author'); |
202 | |
203 | has 'status' => (is => 'rw', isa => 'Status'); |
204 | |
205 | around 'start_date', 'end_date' => sub { |
206 | my $c = shift; |
207 | my $self = shift; |
208 | $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_; |
b68e5362 |
209 | DateTime::Format::MySQL->parse_datetime($c->($self) || return undef); |
fcec2383 |
210 | }; |
211 | } |
212 | |
213 | { # check the meta stuff first |
8479d544 |
214 | isa_ok(Moose::POOP::Object->meta, 'Moose::POOP::Meta::Class'); |
215 | isa_ok(Moose::POOP::Object->meta, 'Moose::Meta::Class'); |
216 | isa_ok(Moose::POOP::Object->meta, 'Class::MOP::Class'); |
fcec2383 |
217 | |
8479d544 |
218 | is(Moose::POOP::Object->meta->instance_metaclass, |
219 | 'Moose::POOP::Meta::Instance', |
fcec2383 |
220 | '... got the right instance metaclass name'); |
221 | |
8479d544 |
222 | isa_ok(Moose::POOP::Object->meta->get_meta_instance, 'Moose::POOP::Meta::Instance'); |
fcec2383 |
223 | |
8479d544 |
224 | my $base = Moose::POOP::Object->new; |
225 | isa_ok($base, 'Moose::POOP::Object'); |
fcec2383 |
226 | isa_ok($base, 'Moose::Object'); |
227 | |
8479d544 |
228 | isa_ok($base->meta, 'Moose::POOP::Meta::Class'); |
fcec2383 |
229 | isa_ok($base->meta, 'Moose::Meta::Class'); |
230 | isa_ok($base->meta, 'Class::MOP::Class'); |
231 | |
232 | is($base->meta->instance_metaclass, |
8479d544 |
233 | 'Moose::POOP::Meta::Instance', |
fcec2383 |
234 | '... got the right instance metaclass name'); |
235 | |
8479d544 |
236 | isa_ok($base->meta->get_meta_instance, 'Moose::POOP::Meta::Instance'); |
fcec2383 |
237 | } |
238 | |
239 | my $article_oid; |
240 | my $article_ref; |
241 | { |
242 | my $article; |
243 | lives_ok { |
244 | $article = Newswriter::Article->new( |
245 | headline => 'Home Office Redecorated', |
246 | summary => 'The home office was recently redecorated to match the new company colors', |
247 | article => '...', |
248 | |
249 | author => Newswriter::Author->new( |
250 | first_name => 'Truman', |
251 | last_name => 'Capote' |
252 | ), |
253 | |
254 | status => 'pending' |
255 | ); |
256 | } '... created my article successfully'; |
257 | isa_ok($article, 'Newswriter::Article'); |
8479d544 |
258 | isa_ok($article, 'Moose::POOP::Object'); |
fcec2383 |
259 | |
260 | lives_ok { |
261 | $article->start_date(DateTime->new(year => 2006, month => 6, day => 10)); |
262 | $article->end_date(DateTime->new(year => 2006, month => 6, day => 17)); |
263 | } '... add the article date-time stuff'; |
264 | |
265 | ## check some meta stuff |
266 | |
8479d544 |
267 | isa_ok($article->meta, 'Moose::POOP::Meta::Class'); |
fcec2383 |
268 | isa_ok($article->meta, 'Moose::Meta::Class'); |
269 | isa_ok($article->meta, 'Class::MOP::Class'); |
270 | |
271 | is($article->meta->instance_metaclass, |
8479d544 |
272 | 'Moose::POOP::Meta::Instance', |
fcec2383 |
273 | '... got the right instance metaclass name'); |
274 | |
8479d544 |
275 | isa_ok($article->meta->get_meta_instance, 'Moose::POOP::Meta::Instance'); |
fcec2383 |
276 | |
277 | ok($article->oid, '... got a oid for the article'); |
278 | |
279 | $article_oid = $article->oid; |
280 | $article_ref = "$article"; |
281 | |
282 | is($article->headline, |
283 | 'Home Office Redecorated', |
284 | '... got the right headline'); |
285 | is($article->summary, |
286 | 'The home office was recently redecorated to match the new company colors', |
287 | '... got the right summary'); |
288 | is($article->article, '...', '... got the right article'); |
289 | |
290 | isa_ok($article->start_date, 'DateTime'); |
291 | isa_ok($article->end_date, 'DateTime'); |
292 | |
293 | isa_ok($article->author, 'Newswriter::Author'); |
294 | is($article->author->first_name, 'Truman', '... got the right author first name'); |
295 | is($article->author->last_name, 'Capote', '... got the right author last name'); |
296 | |
297 | is($article->status, 'pending', '... got the right status'); |
298 | } |
299 | |
8479d544 |
300 | Moose::POOP::Meta::Instance->_reload_db(); |
fcec2383 |
301 | |
b68e5362 |
302 | my $article2_oid; |
303 | my $article2_ref; |
fcec2383 |
304 | { |
b68e5362 |
305 | my $article2; |
306 | lives_ok { |
307 | $article2 = Newswriter::Article->new( |
308 | headline => 'Company wins Lottery', |
309 | summary => 'An email was received today that informed the company we have won the lottery', |
310 | article => 'WoW', |
311 | |
312 | author => Newswriter::Author->new( |
313 | first_name => 'Katie', |
314 | last_name => 'Couric' |
315 | ), |
316 | |
317 | status => 'posted' |
318 | ); |
319 | } '... created my article successfully'; |
320 | isa_ok($article2, 'Newswriter::Article'); |
8479d544 |
321 | isa_ok($article2, 'Moose::POOP::Object'); |
b68e5362 |
322 | |
323 | $article2_oid = $article2->oid; |
324 | $article2_ref = "$article2"; |
325 | |
326 | is($article2->headline, |
327 | 'Company wins Lottery', |
328 | '... got the right headline'); |
329 | is($article2->summary, |
330 | 'An email was received today that informed the company we have won the lottery', |
331 | '... got the right summary'); |
332 | is($article2->article, 'WoW', '... got the right article'); |
333 | |
334 | ok(!$article2->start_date, '... these two dates are unassigned'); |
335 | ok(!$article2->end_date, '... these two dates are unassigned'); |
336 | |
337 | isa_ok($article2->author, 'Newswriter::Author'); |
338 | is($article2->author->first_name, 'Katie', '... got the right author first name'); |
339 | is($article2->author->last_name, 'Couric', '... got the right author last name'); |
340 | |
341 | is($article2->status, 'posted', '... got the right status'); |
342 | |
343 | ## orig-article |
344 | |
fcec2383 |
345 | my $article; |
346 | lives_ok { |
347 | $article = Newswriter::Article->new(oid => $article_oid); |
348 | } '... (re)-created my article successfully'; |
349 | isa_ok($article, 'Newswriter::Article'); |
8479d544 |
350 | isa_ok($article, 'Moose::POOP::Object'); |
fcec2383 |
351 | |
352 | is($article->oid, $article_oid, '... got a oid for the article'); |
353 | isnt($article_ref, "$article", '... got a new article instance'); |
354 | |
355 | is($article->headline, |
356 | 'Home Office Redecorated', |
357 | '... got the right headline'); |
358 | is($article->summary, |
359 | 'The home office was recently redecorated to match the new company colors', |
360 | '... got the right summary'); |
361 | is($article->article, '...', '... got the right article'); |
362 | |
363 | isa_ok($article->start_date, 'DateTime'); |
364 | isa_ok($article->end_date, 'DateTime'); |
365 | |
366 | isa_ok($article->author, 'Newswriter::Author'); |
367 | is($article->author->first_name, 'Truman', '... got the right author first name'); |
368 | is($article->author->last_name, 'Capote', '... got the right author last name'); |
369 | |
370 | lives_ok { |
371 | $article->author->first_name('Dan'); |
372 | $article->author->last_name('Rather'); |
373 | } '... changed the value ok'; |
374 | |
375 | is($article->author->first_name, 'Dan', '... got the changed author first name'); |
376 | is($article->author->last_name, 'Rather', '... got the changed author last name'); |
377 | |
378 | is($article->status, 'pending', '... got the right status'); |
379 | } |
380 | |
8479d544 |
381 | Moose::POOP::Meta::Instance->_reload_db(); |
fcec2383 |
382 | |
383 | { |
384 | my $article; |
385 | lives_ok { |
386 | $article = Newswriter::Article->new(oid => $article_oid); |
387 | } '... (re)-created my article successfully'; |
388 | isa_ok($article, 'Newswriter::Article'); |
8479d544 |
389 | isa_ok($article, 'Moose::POOP::Object'); |
fcec2383 |
390 | |
391 | is($article->oid, $article_oid, '... got a oid for the article'); |
392 | isnt($article_ref, "$article", '... got a new article instance'); |
393 | |
394 | is($article->headline, |
395 | 'Home Office Redecorated', |
396 | '... got the right headline'); |
397 | is($article->summary, |
398 | 'The home office was recently redecorated to match the new company colors', |
399 | '... got the right summary'); |
400 | is($article->article, '...', '... got the right article'); |
401 | |
402 | isa_ok($article->start_date, 'DateTime'); |
403 | isa_ok($article->end_date, 'DateTime'); |
404 | |
405 | isa_ok($article->author, 'Newswriter::Author'); |
406 | is($article->author->first_name, 'Dan', '... got the changed author first name'); |
407 | is($article->author->last_name, 'Rather', '... got the changed author last name'); |
408 | |
409 | is($article->status, 'pending', '... got the right status'); |
b68e5362 |
410 | |
411 | my $article2; |
412 | lives_ok { |
413 | $article2 = Newswriter::Article->new(oid => $article2_oid); |
414 | } '... (re)-created my article successfully'; |
415 | isa_ok($article2, 'Newswriter::Article'); |
8479d544 |
416 | isa_ok($article2, 'Moose::POOP::Object'); |
b68e5362 |
417 | |
418 | is($article2->oid, $article2_oid, '... got a oid for the article'); |
419 | isnt($article2_ref, "$article2", '... got a new article instance'); |
420 | |
421 | is($article2->headline, |
422 | 'Company wins Lottery', |
423 | '... got the right headline'); |
424 | is($article2->summary, |
425 | 'An email was received today that informed the company we have won the lottery', |
426 | '... got the right summary'); |
427 | is($article2->article, 'WoW', '... got the right article'); |
428 | |
429 | ok(!$article2->start_date, '... these two dates are unassigned'); |
430 | ok(!$article2->end_date, '... these two dates are unassigned'); |
431 | |
432 | isa_ok($article2->author, 'Newswriter::Author'); |
433 | is($article2->author->first_name, 'Katie', '... got the right author first name'); |
434 | is($article2->author->last_name, 'Couric', '... got the right author last name'); |
435 | |
436 | is($article2->status, 'posted', '... got the right status'); |
437 | |
fcec2383 |
438 | } |
439 | |