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 $@;
24 This example creates a very basic Object Database which
25 links in the instances created with a backend store
26 (a DBM::Deep hash). It is by no means to be taken seriously
27 as a real-world ODB, but is a proof of concept of the flexibility
28 of the ::Instance protocol.
34 package Moose::POOP::Meta::Instance;
39 extends 'Moose::Meta::Instance';
42 my %INSTANCE_COUNTERS;
44 my $db = DBM::Deep->new({
45 file => "newswriter.db",
54 $db = DBM::Deep->new({
55 file => "newswriter.db",
63 my $class = $self->associated_metaclass->name;
64 my $oid = ++$INSTANCE_COUNTERS{$class};
66 $db->{$class}->[($oid - 1)] = {};
68 $self->bless_instance_structure({
70 instance => $db->{$class}->[($oid - 1)]
75 my ($self, $oid) = @_;
76 my $instance = $db->{$self->associated_metaclass->name}->[($oid - 1)];
77 $self->bless_instance_structure({
84 my ($self, $instance) = @_;
86 my $class = $self->{meta}->name;
87 my $oid = ++$INSTANCE_COUNTERS{$class};
89 my $clone = tied($instance)->clone;
91 $self->bless_instance_structure({
98 sub get_instance_oid {
99 my ($self, $instance) = @_;
104 my ($self, $instance, $slot_name) = @_;
105 return $instance->{instance}->{$slot_name};
109 my ($self, $instance, $slot_name, $value) = @_;
110 $instance->{instance}->{$slot_name} = $value;
113 sub is_slot_initialized {
114 my ($self, $instance, $slot_name, $value) = @_;
115 exists $instance->{instance}->{$slot_name} ? 1 : 0;
118 sub weaken_slot_value {
119 confess "Not sure how well DBM::Deep plays with weak refs, Rob says 'Write a test'";
122 sub inline_slot_access {
123 my ($self, $instance, $slot_name) = @_;
124 sprintf "%s->{instance}->{%s}", $instance, $slot_name;
127 package Moose::POOP::Meta::Class;
130 extends 'Moose::Meta::Class';
132 override 'construct_instance' => sub {
133 my ($class, %params) = @_;
134 return $class->get_meta_instance->find_instance($params{oid})
141 package Moose::POOP::Object;
142 use metaclass 'Moose::POOP::Meta::Class' => (
143 instance_metaclass => 'Moose::POOP::Meta::Instance'
151 ->get_instance_oid($self);
156 package Newswriter::Author;
159 extends 'Moose::POOP::Object';
161 has 'first_name' => (is => 'rw', isa => 'Str');
162 has 'last_name' => (is => 'rw', isa => 'Str');
164 package Newswriter::Article;
166 use Moose::Util::TypeConstraints;
168 use DateTime::Format::MySQL;
170 extends 'Moose::POOP::Object';
174 => where { length($_) < 100 };
178 => where { length($_) < 255 };
180 subtype 'DateTimeFormatString'
182 => where { DateTime::Format::MySQL->parse_datetime($_) };
184 enum 'Status' => qw(draft posted pending archive);
186 has 'headline' => (is => 'rw', isa => 'Headline');
187 has 'summary' => (is => 'rw', isa => 'Summary');
188 has 'article' => (is => 'rw', isa => 'Str');
190 has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
191 has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString');
193 has 'author' => (is => 'rw', isa => 'Newswriter::Author');
195 has 'status' => (is => 'rw', isa => 'Status');
197 around 'start_date', 'end_date' => sub {
200 $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
201 DateTime::Format::MySQL->parse_datetime($c->($self) || return undef);
205 { # check the meta stuff first
206 isa_ok(Moose::POOP::Object->meta, 'Moose::POOP::Meta::Class');
207 isa_ok(Moose::POOP::Object->meta, 'Moose::Meta::Class');
208 isa_ok(Moose::POOP::Object->meta, 'Class::MOP::Class');
210 is(Moose::POOP::Object->meta->instance_metaclass,
211 'Moose::POOP::Meta::Instance',
212 '... got the right instance metaclass name');
214 isa_ok(Moose::POOP::Object->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
216 my $base = Moose::POOP::Object->new;
217 isa_ok($base, 'Moose::POOP::Object');
218 isa_ok($base, 'Moose::Object');
220 isa_ok($base->meta, 'Moose::POOP::Meta::Class');
221 isa_ok($base->meta, 'Moose::Meta::Class');
222 isa_ok($base->meta, 'Class::MOP::Class');
224 is($base->meta->instance_metaclass,
225 'Moose::POOP::Meta::Instance',
226 '... got the right instance metaclass name');
228 isa_ok($base->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
236 $article = Newswriter::Article->new(
237 headline => 'Home Office Redecorated',
238 summary => 'The home office was recently redecorated to match the new company colors',
241 author => Newswriter::Author->new(
242 first_name => 'Truman',
243 last_name => 'Capote'
248 } '... created my article successfully';
249 isa_ok($article, 'Newswriter::Article');
250 isa_ok($article, 'Moose::POOP::Object');
253 $article->start_date(DateTime->new(year => 2006, month => 6, day => 10));
254 $article->end_date(DateTime->new(year => 2006, month => 6, day => 17));
255 } '... add the article date-time stuff';
257 ## check some meta stuff
259 isa_ok($article->meta, 'Moose::POOP::Meta::Class');
260 isa_ok($article->meta, 'Moose::Meta::Class');
261 isa_ok($article->meta, 'Class::MOP::Class');
263 is($article->meta->instance_metaclass,
264 'Moose::POOP::Meta::Instance',
265 '... got the right instance metaclass name');
267 isa_ok($article->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
269 ok($article->oid, '... got a oid for the article');
271 $article_oid = $article->oid;
272 $article_ref = "$article";
274 is($article->headline,
275 'Home Office Redecorated',
276 '... got the right headline');
277 is($article->summary,
278 'The home office was recently redecorated to match the new company colors',
279 '... got the right summary');
280 is($article->article, '...', '... got the right article');
282 isa_ok($article->start_date, 'DateTime');
283 isa_ok($article->end_date, 'DateTime');
285 isa_ok($article->author, 'Newswriter::Author');
286 is($article->author->first_name, 'Truman', '... got the right author first name');
287 is($article->author->last_name, 'Capote', '... got the right author last name');
289 is($article->status, 'pending', '... got the right status');
292 Moose::POOP::Meta::Instance->_reload_db();
299 $article2 = Newswriter::Article->new(
300 headline => 'Company wins Lottery',
301 summary => 'An email was received today that informed the company we have won the lottery',
304 author => Newswriter::Author->new(
305 first_name => 'Katie',
306 last_name => 'Couric'
311 } '... created my article successfully';
312 isa_ok($article2, 'Newswriter::Article');
313 isa_ok($article2, 'Moose::POOP::Object');
315 $article2_oid = $article2->oid;
316 $article2_ref = "$article2";
318 is($article2->headline,
319 'Company wins Lottery',
320 '... got the right headline');
321 is($article2->summary,
322 'An email was received today that informed the company we have won the lottery',
323 '... got the right summary');
324 is($article2->article, 'WoW', '... got the right article');
326 ok(!$article2->start_date, '... these two dates are unassigned');
327 ok(!$article2->end_date, '... these two dates are unassigned');
329 isa_ok($article2->author, 'Newswriter::Author');
330 is($article2->author->first_name, 'Katie', '... got the right author first name');
331 is($article2->author->last_name, 'Couric', '... got the right author last name');
333 is($article2->status, 'posted', '... got the right status');
339 $article = Newswriter::Article->new(oid => $article_oid);
340 } '... (re)-created my article successfully';
341 isa_ok($article, 'Newswriter::Article');
342 isa_ok($article, 'Moose::POOP::Object');
344 is($article->oid, $article_oid, '... got a oid for the article');
345 isnt($article_ref, "$article", '... got a new article instance');
347 is($article->headline,
348 'Home Office Redecorated',
349 '... got the right headline');
350 is($article->summary,
351 'The home office was recently redecorated to match the new company colors',
352 '... got the right summary');
353 is($article->article, '...', '... got the right article');
355 isa_ok($article->start_date, 'DateTime');
356 isa_ok($article->end_date, 'DateTime');
358 isa_ok($article->author, 'Newswriter::Author');
359 is($article->author->first_name, 'Truman', '... got the right author first name');
360 is($article->author->last_name, 'Capote', '... got the right author last name');
363 $article->author->first_name('Dan');
364 $article->author->last_name('Rather');
365 } '... changed the value ok';
367 is($article->author->first_name, 'Dan', '... got the changed author first name');
368 is($article->author->last_name, 'Rather', '... got the changed author last name');
370 is($article->status, 'pending', '... got the right status');
373 Moose::POOP::Meta::Instance->_reload_db();
378 $article = Newswriter::Article->new(oid => $article_oid);
379 } '... (re)-created my article successfully';
380 isa_ok($article, 'Newswriter::Article');
381 isa_ok($article, 'Moose::POOP::Object');
383 is($article->oid, $article_oid, '... got a oid for the article');
384 isnt($article_ref, "$article", '... got a new article instance');
386 is($article->headline,
387 'Home Office Redecorated',
388 '... got the right headline');
389 is($article->summary,
390 'The home office was recently redecorated to match the new company colors',
391 '... got the right summary');
392 is($article->article, '...', '... got the right article');
394 isa_ok($article->start_date, 'DateTime');
395 isa_ok($article->end_date, 'DateTime');
397 isa_ok($article->author, 'Newswriter::Author');
398 is($article->author->first_name, 'Dan', '... got the changed author first name');
399 is($article->author->last_name, 'Rather', '... got the changed author last name');
401 is($article->status, 'pending', '... got the right status');
405 $article2 = Newswriter::Article->new(oid => $article2_oid);
406 } '... (re)-created my article successfully';
407 isa_ok($article2, 'Newswriter::Article');
408 isa_ok($article2, 'Moose::POOP::Object');
410 is($article2->oid, $article2_oid, '... got a oid for the article');
411 isnt($article2_ref, "$article2", '... got a new article instance');
413 is($article2->headline,
414 'Company wins Lottery',
415 '... got the right headline');
416 is($article2->summary,
417 'An email was received today that informed the company we have won the lottery',
418 '... got the right summary');
419 is($article2->article, 'WoW', '... got the right article');
421 ok(!$article2->start_date, '... these two dates are unassigned');
422 ok(!$article2->end_date, '... these two dates are unassigned');
424 isa_ok($article2->author, 'Newswriter::Author');
425 is($article2->author->first_name, 'Katie', '... got the right author first name');
426 is($article2->author->last_name, 'Couric', '... got the right author last name');
428 is($article2->status, 'posted', '... got the right status');
432 unlink('newswriter.db') if -e 'newswriter.db';