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