Moose now warns when you try to load it from the main package. Added a
[gitmo/Moose.git] / t / 200_examples / 002_example_Moose_POOP.t
CommitLineData
fcec2383 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
047248f9 6use Test::More;
7
8BEGIN {
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 16use Test::Exception;
17
18BEGIN {
0f051d58 19 # in case there are leftovers
20 unlink('newswriter.db') if -e 'newswriter.db';
21}
22
23END {
24 unlink('newswriter.db') if -e 'newswriter.db';
25}
26
7ff56534 27
fcec2383 28
b68e5362 29=pod
30
31This example creates a very basic Object Database which
32links in the instances created with a backend store
33(a DBM::Deep hash). It is by no means to be taken seriously
77a18c28 34as a real-world ODB, but is a proof of concept of the flexibility
35of the ::Instance protocol.
b68e5362 36
37=cut
38
fcec2383 39BEGIN {
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 {
140 my ($class, %params) = @_;
141 return $class->get_meta_instance->find_instance($params{oid})
142 if $params{oid};
143 super();
144 };
fcec2383 145
8479d544 146}
fcec2383 147{
8479d544 148 package Moose::POOP::Object;
8479d544 149 use metaclass 'Moose::POOP::Meta::Class' => (
5cf3dbcf 150 instance_metaclass => 'Moose::POOP::Meta::Instance'
fcec2383 151 );
152 use Moose;
153
154 sub oid {
155 my $self = shift;
156 $self->meta
157 ->get_meta_instance
158 ->get_instance_oid($self);
159 }
8479d544 160
161}
162{
fcec2383 163 package Newswriter::Author;
fcec2383 164 use Moose;
165
8479d544 166 extends 'Moose::POOP::Object';
fcec2383 167
168 has 'first_name' => (is => 'rw', isa => 'Str');
169 has 'last_name' => (is => 'rw', isa => 'Str');
170
171 package Newswriter::Article;
fcec2383 172 use Moose;
173 use Moose::Util::TypeConstraints;
174
175 use DateTime::Format::MySQL;
176
8479d544 177 extends 'Moose::POOP::Object';
fcec2383 178
179 subtype 'Headline'
180 => as 'Str'
181 => where { length($_) < 100 };
182
183 subtype 'Summary'
184 => as 'Str'
185 => where { length($_) < 255 };
186
187 subtype 'DateTimeFormatString'
188 => as 'Str'
189 => where { DateTime::Format::MySQL->parse_datetime($_) };
190
191 enum 'Status' => qw(draft posted pending archive);
192
193 has 'headline' => (is => 'rw', isa => 'Headline');
194 has 'summary' => (is => 'rw', isa => 'Summary');
195 has 'article' => (is => 'rw', isa => 'Str');
196
197 has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
198 has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString');
199
200 has 'author' => (is => 'rw', isa => 'Newswriter::Author');
201
202 has 'status' => (is => 'rw', isa => 'Status');
203
204 around 'start_date', 'end_date' => sub {
205 my $c = shift;
206 my $self = shift;
207 $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
b68e5362 208 DateTime::Format::MySQL->parse_datetime($c->($self) || return undef);
fcec2383 209 };
210}
211
212{ # check the meta stuff first
8479d544 213 isa_ok(Moose::POOP::Object->meta, 'Moose::POOP::Meta::Class');
214 isa_ok(Moose::POOP::Object->meta, 'Moose::Meta::Class');
215 isa_ok(Moose::POOP::Object->meta, 'Class::MOP::Class');
fcec2383 216
8479d544 217 is(Moose::POOP::Object->meta->instance_metaclass,
218 'Moose::POOP::Meta::Instance',
fcec2383 219 '... got the right instance metaclass name');
220
8479d544 221 isa_ok(Moose::POOP::Object->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
fcec2383 222
8479d544 223 my $base = Moose::POOP::Object->new;
224 isa_ok($base, 'Moose::POOP::Object');
fcec2383 225 isa_ok($base, 'Moose::Object');
226
8479d544 227 isa_ok($base->meta, 'Moose::POOP::Meta::Class');
fcec2383 228 isa_ok($base->meta, 'Moose::Meta::Class');
229 isa_ok($base->meta, 'Class::MOP::Class');
230
231 is($base->meta->instance_metaclass,
8479d544 232 'Moose::POOP::Meta::Instance',
fcec2383 233 '... got the right instance metaclass name');
234
8479d544 235 isa_ok($base->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
fcec2383 236}
237
238my $article_oid;
239my $article_ref;
240{
241 my $article;
242 lives_ok {
243 $article = Newswriter::Article->new(
244 headline => 'Home Office Redecorated',
245 summary => 'The home office was recently redecorated to match the new company colors',
246 article => '...',
247
248 author => Newswriter::Author->new(
249 first_name => 'Truman',
250 last_name => 'Capote'
251 ),
252
253 status => 'pending'
254 );
255 } '... created my article successfully';
256 isa_ok($article, 'Newswriter::Article');
8479d544 257 isa_ok($article, 'Moose::POOP::Object');
fcec2383 258
259 lives_ok {
260 $article->start_date(DateTime->new(year => 2006, month => 6, day => 10));
261 $article->end_date(DateTime->new(year => 2006, month => 6, day => 17));
262 } '... add the article date-time stuff';
263
264 ## check some meta stuff
265
8479d544 266 isa_ok($article->meta, 'Moose::POOP::Meta::Class');
fcec2383 267 isa_ok($article->meta, 'Moose::Meta::Class');
268 isa_ok($article->meta, 'Class::MOP::Class');
269
270 is($article->meta->instance_metaclass,
8479d544 271 'Moose::POOP::Meta::Instance',
fcec2383 272 '... got the right instance metaclass name');
273
8479d544 274 isa_ok($article->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
fcec2383 275
276 ok($article->oid, '... got a oid for the article');
277
278 $article_oid = $article->oid;
279 $article_ref = "$article";
280
281 is($article->headline,
282 'Home Office Redecorated',
283 '... got the right headline');
284 is($article->summary,
285 'The home office was recently redecorated to match the new company colors',
286 '... got the right summary');
287 is($article->article, '...', '... got the right article');
288
289 isa_ok($article->start_date, 'DateTime');
290 isa_ok($article->end_date, 'DateTime');
291
292 isa_ok($article->author, 'Newswriter::Author');
293 is($article->author->first_name, 'Truman', '... got the right author first name');
294 is($article->author->last_name, 'Capote', '... got the right author last name');
295
296 is($article->status, 'pending', '... got the right status');
297}
298
8479d544 299Moose::POOP::Meta::Instance->_reload_db();
fcec2383 300
b68e5362 301my $article2_oid;
302my $article2_ref;
fcec2383 303{
b68e5362 304 my $article2;
305 lives_ok {
306 $article2 = Newswriter::Article->new(
307 headline => 'Company wins Lottery',
308 summary => 'An email was received today that informed the company we have won the lottery',
309 article => 'WoW',
310
311 author => Newswriter::Author->new(
312 first_name => 'Katie',
313 last_name => 'Couric'
314 ),
315
316 status => 'posted'
317 );
318 } '... created my article successfully';
319 isa_ok($article2, 'Newswriter::Article');
8479d544 320 isa_ok($article2, 'Moose::POOP::Object');
b68e5362 321
322 $article2_oid = $article2->oid;
323 $article2_ref = "$article2";
324
325 is($article2->headline,
326 'Company wins Lottery',
327 '... got the right headline');
328 is($article2->summary,
329 'An email was received today that informed the company we have won the lottery',
330 '... got the right summary');
331 is($article2->article, 'WoW', '... got the right article');
332
333 ok(!$article2->start_date, '... these two dates are unassigned');
334 ok(!$article2->end_date, '... these two dates are unassigned');
335
336 isa_ok($article2->author, 'Newswriter::Author');
337 is($article2->author->first_name, 'Katie', '... got the right author first name');
338 is($article2->author->last_name, 'Couric', '... got the right author last name');
339
340 is($article2->status, 'posted', '... got the right status');
341
342 ## orig-article
343
fcec2383 344 my $article;
345 lives_ok {
346 $article = Newswriter::Article->new(oid => $article_oid);
347 } '... (re)-created my article successfully';
348 isa_ok($article, 'Newswriter::Article');
8479d544 349 isa_ok($article, 'Moose::POOP::Object');
fcec2383 350
351 is($article->oid, $article_oid, '... got a oid for the article');
352 isnt($article_ref, "$article", '... got a new article instance');
353
354 is($article->headline,
355 'Home Office Redecorated',
356 '... got the right headline');
357 is($article->summary,
358 'The home office was recently redecorated to match the new company colors',
359 '... got the right summary');
360 is($article->article, '...', '... got the right article');
361
362 isa_ok($article->start_date, 'DateTime');
363 isa_ok($article->end_date, 'DateTime');
364
365 isa_ok($article->author, 'Newswriter::Author');
366 is($article->author->first_name, 'Truman', '... got the right author first name');
367 is($article->author->last_name, 'Capote', '... got the right author last name');
368
369 lives_ok {
370 $article->author->first_name('Dan');
371 $article->author->last_name('Rather');
372 } '... changed the value ok';
373
374 is($article->author->first_name, 'Dan', '... got the changed author first name');
375 is($article->author->last_name, 'Rather', '... got the changed author last name');
376
377 is($article->status, 'pending', '... got the right status');
378}
379
8479d544 380Moose::POOP::Meta::Instance->_reload_db();
fcec2383 381
382{
383 my $article;
384 lives_ok {
385 $article = Newswriter::Article->new(oid => $article_oid);
386 } '... (re)-created my article successfully';
387 isa_ok($article, 'Newswriter::Article');
8479d544 388 isa_ok($article, 'Moose::POOP::Object');
fcec2383 389
390 is($article->oid, $article_oid, '... got a oid for the article');
391 isnt($article_ref, "$article", '... got a new article instance');
392
393 is($article->headline,
394 'Home Office Redecorated',
395 '... got the right headline');
396 is($article->summary,
397 'The home office was recently redecorated to match the new company colors',
398 '... got the right summary');
399 is($article->article, '...', '... got the right article');
400
401 isa_ok($article->start_date, 'DateTime');
402 isa_ok($article->end_date, 'DateTime');
403
404 isa_ok($article->author, 'Newswriter::Author');
405 is($article->author->first_name, 'Dan', '... got the changed author first name');
406 is($article->author->last_name, 'Rather', '... got the changed author last name');
407
408 is($article->status, 'pending', '... got the right status');
b68e5362 409
410 my $article2;
411 lives_ok {
412 $article2 = Newswriter::Article->new(oid => $article2_oid);
413 } '... (re)-created my article successfully';
414 isa_ok($article2, 'Newswriter::Article');
8479d544 415 isa_ok($article2, 'Moose::POOP::Object');
b68e5362 416
417 is($article2->oid, $article2_oid, '... got a oid for the article');
418 isnt($article2_ref, "$article2", '... got a new article instance');
419
420 is($article2->headline,
421 'Company wins Lottery',
422 '... got the right headline');
423 is($article2->summary,
424 'An email was received today that informed the company we have won the lottery',
425 '... got the right summary');
426 is($article2->article, 'WoW', '... got the right article');
427
428 ok(!$article2->start_date, '... these two dates are unassigned');
429 ok(!$article2->end_date, '... these two dates are unassigned');
430
431 isa_ok($article2->author, 'Newswriter::Author');
432 is($article2->author->first_name, 'Katie', '... got the right author first name');
433 is($article2->author->last_name, 'Couric', '... got the right author last name');
434
435 is($article2->status, 'posted', '... got the right status');
436
fcec2383 437}
438