9 eval "use DBM::Deep 1.0003;";
10 plan skip_all => "DBM::Deep 1.0003 (or greater) is required for this test" if $@;
11 eval "use DateTime::Format::MySQL;";
12 plan skip_all => "DateTime::Format::MySQL is required for this test" if $@;
19 # in case there are leftovers
20 unlink('newswriter.db') if -e 'newswriter.db';
24 unlink('newswriter.db') if -e 'newswriter.db';
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
34 as a real-world ODB, but is a proof of concept of the flexibility
35 of the ::Instance protocol.
41 package Moose::POOP::Meta::Instance;
46 extends 'Moose::Meta::Instance';
49 my %INSTANCE_COUNTERS;
51 my $db = DBM::Deep->new({
52 file => "newswriter.db",
61 $db = DBM::Deep->new({
62 file => "newswriter.db",
70 my $class = $self->associated_metaclass->name;
71 my $oid = ++$INSTANCE_COUNTERS{$class};
73 $db->{$class}->[($oid - 1)] = {};
75 $self->bless_instance_structure({
77 instance => $db->{$class}->[($oid - 1)]
82 my ($self, $oid) = @_;
83 my $instance = $db->{$self->associated_metaclass->name}->[($oid - 1)];
84 $self->bless_instance_structure({
91 my ($self, $instance) = @_;
93 my $class = $self->{meta}->name;
94 my $oid = ++$INSTANCE_COUNTERS{$class};
96 my $clone = tied($instance)->clone;
98 $self->bless_instance_structure({
105 sub get_instance_oid {
106 my ($self, $instance) = @_;
111 my ($self, $instance, $slot_name) = @_;
112 return $instance->{instance}->{$slot_name};
116 my ($self, $instance, $slot_name, $value) = @_;
117 $instance->{instance}->{$slot_name} = $value;
120 sub is_slot_initialized {
121 my ($self, $instance, $slot_name, $value) = @_;
122 exists $instance->{instance}->{$slot_name} ? 1 : 0;
125 sub weaken_slot_value {
126 confess "Not sure how well DBM::Deep plays with weak refs, Rob says 'Write a test'";
129 sub inline_slot_access {
130 my ($self, $instance, $slot_name) = @_;
131 sprintf "%s->{instance}->{%s}", $instance, $slot_name;
134 package Moose::POOP::Meta::Class;
137 extends 'Moose::Meta::Class';
139 override 'construct_instance' => sub {
141 my $params = @_ == 1 ? $_[0] : {@_};
142 return $class->get_meta_instance->find_instance($params->{oid})
149 package Moose::POOP::Object;
150 use metaclass 'Moose::POOP::Meta::Class' => (
151 instance_metaclass => 'Moose::POOP::Meta::Instance'
159 ->get_instance_oid($self);
164 package Newswriter::Author;
167 extends 'Moose::POOP::Object';
169 has 'first_name' => (is => 'rw', isa => 'Str');
170 has 'last_name' => (is => 'rw', isa => 'Str');
172 package Newswriter::Article;
174 use Moose::Util::TypeConstraints;
176 use DateTime::Format::MySQL;
178 extends 'Moose::POOP::Object';
182 => where { length($_) < 100 };
186 => where { length($_) < 255 };
188 subtype 'DateTimeFormatString'
190 => where { DateTime::Format::MySQL->parse_datetime($_) };
192 enum 'Status' => qw(draft posted pending archive);
194 has 'headline' => (is => 'rw', isa => 'Headline');
195 has 'summary' => (is => 'rw', isa => 'Summary');
196 has 'article' => (is => 'rw', isa => 'Str');
198 has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
199 has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString');
201 has 'author' => (is => 'rw', isa => 'Newswriter::Author');
203 has 'status' => (is => 'rw', isa => 'Status');
205 around 'start_date', 'end_date' => sub {
208 $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
209 DateTime::Format::MySQL->parse_datetime($c->($self) || return undef);
213 { # check the meta stuff first
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');
218 is(Moose::POOP::Object->meta->instance_metaclass,
219 'Moose::POOP::Meta::Instance',
220 '... got the right instance metaclass name');
222 isa_ok(Moose::POOP::Object->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
224 my $base = Moose::POOP::Object->new;
225 isa_ok($base, 'Moose::POOP::Object');
226 isa_ok($base, 'Moose::Object');
228 isa_ok($base->meta, 'Moose::POOP::Meta::Class');
229 isa_ok($base->meta, 'Moose::Meta::Class');
230 isa_ok($base->meta, 'Class::MOP::Class');
232 is($base->meta->instance_metaclass,
233 'Moose::POOP::Meta::Instance',
234 '... got the right instance metaclass name');
236 isa_ok($base->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
244 $article = Newswriter::Article->new(
245 headline => 'Home Office Redecorated',
246 summary => 'The home office was recently redecorated to match the new company colors',
249 author => Newswriter::Author->new(
250 first_name => 'Truman',
251 last_name => 'Capote'
256 } '... created my article successfully';
257 isa_ok($article, 'Newswriter::Article');
258 isa_ok($article, 'Moose::POOP::Object');
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';
265 ## check some meta stuff
267 isa_ok($article->meta, 'Moose::POOP::Meta::Class');
268 isa_ok($article->meta, 'Moose::Meta::Class');
269 isa_ok($article->meta, 'Class::MOP::Class');
271 is($article->meta->instance_metaclass,
272 'Moose::POOP::Meta::Instance',
273 '... got the right instance metaclass name');
275 isa_ok($article->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
277 ok($article->oid, '... got a oid for the article');
279 $article_oid = $article->oid;
280 $article_ref = "$article";
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');
290 isa_ok($article->start_date, 'DateTime');
291 isa_ok($article->end_date, 'DateTime');
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');
297 is($article->status, 'pending', '... got the right status');
300 Moose::POOP::Meta::Instance->_reload_db();
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',
312 author => Newswriter::Author->new(
313 first_name => 'Katie',
314 last_name => 'Couric'
319 } '... created my article successfully';
320 isa_ok($article2, 'Newswriter::Article');
321 isa_ok($article2, 'Moose::POOP::Object');
323 $article2_oid = $article2->oid;
324 $article2_ref = "$article2";
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');
334 ok(!$article2->start_date, '... these two dates are unassigned');
335 ok(!$article2->end_date, '... these two dates are unassigned');
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');
341 is($article2->status, 'posted', '... got the right status');
347 $article = Newswriter::Article->new(oid => $article_oid);
348 } '... (re)-created my article successfully';
349 isa_ok($article, 'Newswriter::Article');
350 isa_ok($article, 'Moose::POOP::Object');
352 is($article->oid, $article_oid, '... got a oid for the article');
353 isnt($article_ref, "$article", '... got a new article instance');
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');
363 isa_ok($article->start_date, 'DateTime');
364 isa_ok($article->end_date, 'DateTime');
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');
371 $article->author->first_name('Dan');
372 $article->author->last_name('Rather');
373 } '... changed the value ok';
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');
378 is($article->status, 'pending', '... got the right status');
381 Moose::POOP::Meta::Instance->_reload_db();
386 $article = Newswriter::Article->new(oid => $article_oid);
387 } '... (re)-created my article successfully';
388 isa_ok($article, 'Newswriter::Article');
389 isa_ok($article, 'Moose::POOP::Object');
391 is($article->oid, $article_oid, '... got a oid for the article');
392 isnt($article_ref, "$article", '... got a new article instance');
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');
402 isa_ok($article->start_date, 'DateTime');
403 isa_ok($article->end_date, 'DateTime');
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');
409 is($article->status, 'pending', '... got the right status');
413 $article2 = Newswriter::Article->new(oid => $article2_oid);
414 } '... (re)-created my article successfully';
415 isa_ok($article2, 'Newswriter::Article');
416 isa_ok($article2, 'Moose::POOP::Object');
418 is($article2->oid, $article2_oid, '... got a oid for the article');
419 isnt($article2_ref, "$article2", '... got a new article instance');
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');
429 ok(!$article2->start_date, '... these two dates are unassigned');
430 ok(!$article2->end_date, '... these two dates are unassigned');
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');
436 is($article2->status, 'posted', '... got the right status');