10 plan skip_all => "DBM::Deep required for this test" if $@;
22 package Newswriter::Meta::Instance;
29 extends 'Moose::Meta::Instance';
32 my $instance_counter = -1;
34 my $db = DBM::Deep->new({
35 file => "newswriter.db",
39 $db->{root} = [] unless exists $db->{root};
43 $db = DBM::Deep->new({
44 file => "newswriter.db",
53 $db->{root}->[$instance_counter] = {};
55 $self->bless_instance_structure({
56 oid => $instance_counter,
57 instance => $db->{root}->[$instance_counter]
62 my ($self, $oid) = @_;
63 my $instance_struct = $db->{root}->[$oid];
65 $self->bless_instance_structure({
67 instance => $instance_struct
72 sub get_instance_oid {
73 my ($self, $instance) = @_;
78 confess "&clone_instance is left as an exercise for the user";
82 my ($self, $instance, $slot_name) = @_;
83 return $instance->{instance}->{$slot_name};
87 my ($self, $instance, $slot_name, $value) = @_;
88 $instance->{instance}->{$slot_name} = $value;
91 sub is_slot_initialized {
92 my ($self, $instance, $slot_name, $value) = @_;
93 exists $instance->{instance}->{$slot_name} ? 1 : 0;
96 sub weaken_slot_value {
97 confess "Not sure how well DBM::Deep plays with weak refs, Rob says 'Writer a test'";
100 sub inline_slot_access {
101 my ($self, $instance, $slot_name) = @_;
102 sprintf "%s->{instance}->{%s}", $instance, $slot_name;
105 package Newswriter::Meta::Class;
110 extends 'Moose::Meta::Class';
112 override 'construct_instance' => sub {
113 my ($class, %params) = @_;
114 return $class->get_meta_instance->find_instance($params{oid})
121 package Newswriter::Base;
124 use metaclass 'Newswriter::Meta::Class' => (
125 ':instance_metaclass' => 'Newswriter::Meta::Instance'
133 ->get_instance_oid($self);
136 package Newswriter::Author;
139 use metaclass 'Newswriter::Meta::Class' => (
140 ':instance_metaclass' => 'Newswriter::Meta::Instance'
144 extends 'Newswriter::Base';
146 has 'first_name' => (is => 'rw', isa => 'Str');
147 has 'last_name' => (is => 'rw', isa => 'Str');
149 package Newswriter::Article;
152 use metaclass 'Newswriter::Meta::Class' => (
153 ':instance_metaclass' => 'Newswriter::Meta::Instance'
156 use Moose::Util::TypeConstraints;
158 use DateTime::Format::MySQL;
160 extends 'Newswriter::Base';
164 => where { length($_) < 100 };
168 => where { length($_) < 255 };
170 subtype 'DateTimeFormatString'
172 => where { DateTime::Format::MySQL->parse_datetime($_) };
174 enum 'Status' => qw(draft posted pending archive);
176 has 'headline' => (is => 'rw', isa => 'Headline');
177 has 'summary' => (is => 'rw', isa => 'Summary');
178 has 'article' => (is => 'rw', isa => 'Str');
180 has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
181 has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString');
183 has 'author' => (is => 'rw', isa => 'Newswriter::Author');
185 has 'status' => (is => 'rw', isa => 'Status');
187 around 'start_date', 'end_date' => sub {
190 $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
191 DateTime::Format::MySQL->parse_datetime($c->($self));
195 { # check the meta stuff first
196 isa_ok(Newswriter::Base->meta, 'Newswriter::Meta::Class');
197 isa_ok(Newswriter::Base->meta, 'Moose::Meta::Class');
198 isa_ok(Newswriter::Base->meta, 'Class::MOP::Class');
200 is(Newswriter::Base->meta->instance_metaclass,
201 'Newswriter::Meta::Instance',
202 '... got the right instance metaclass name');
204 isa_ok(Newswriter::Base->meta->get_meta_instance, 'Newswriter::Meta::Instance');
206 my $base = Newswriter::Base->new;
207 isa_ok($base, 'Newswriter::Base');
208 isa_ok($base, 'Moose::Object');
210 isa_ok($base->meta, 'Newswriter::Meta::Class');
211 isa_ok($base->meta, 'Moose::Meta::Class');
212 isa_ok($base->meta, 'Class::MOP::Class');
214 is($base->meta->instance_metaclass,
215 'Newswriter::Meta::Instance',
216 '... got the right instance metaclass name');
218 isa_ok($base->meta->get_meta_instance, 'Newswriter::Meta::Instance');
226 $article = Newswriter::Article->new(
227 headline => 'Home Office Redecorated',
228 summary => 'The home office was recently redecorated to match the new company colors',
231 author => Newswriter::Author->new(
232 first_name => 'Truman',
233 last_name => 'Capote'
238 } '... created my article successfully';
239 isa_ok($article, 'Newswriter::Article');
240 isa_ok($article, 'Newswriter::Base');
243 $article->start_date(DateTime->new(year => 2006, month => 6, day => 10));
244 $article->end_date(DateTime->new(year => 2006, month => 6, day => 17));
245 } '... add the article date-time stuff';
247 ## check some meta stuff
249 isa_ok($article->meta, 'Newswriter::Meta::Class');
250 isa_ok($article->meta, 'Moose::Meta::Class');
251 isa_ok($article->meta, 'Class::MOP::Class');
253 is($article->meta->instance_metaclass,
254 'Newswriter::Meta::Instance',
255 '... got the right instance metaclass name');
257 isa_ok($article->meta->get_meta_instance, 'Newswriter::Meta::Instance');
259 ok($article->oid, '... got a oid for the article');
261 $article_oid = $article->oid;
262 $article_ref = "$article";
264 is($article->headline,
265 'Home Office Redecorated',
266 '... got the right headline');
267 is($article->summary,
268 'The home office was recently redecorated to match the new company colors',
269 '... got the right summary');
270 is($article->article, '...', '... got the right article');
272 isa_ok($article->start_date, 'DateTime');
273 isa_ok($article->end_date, 'DateTime');
275 isa_ok($article->author, 'Newswriter::Author');
276 is($article->author->first_name, 'Truman', '... got the right author first name');
277 is($article->author->last_name, 'Capote', '... got the right author last name');
279 is($article->status, 'pending', '... got the right status');
282 Newswriter::Meta::Instance->_reload_db();
287 $article = Newswriter::Article->new(oid => $article_oid);
288 } '... (re)-created my article successfully';
289 isa_ok($article, 'Newswriter::Article');
290 isa_ok($article, 'Newswriter::Base');
292 is($article->oid, $article_oid, '... got a oid for the article');
293 isnt($article_ref, "$article", '... got a new article instance');
295 is($article->headline,
296 'Home Office Redecorated',
297 '... got the right headline');
298 is($article->summary,
299 'The home office was recently redecorated to match the new company colors',
300 '... got the right summary');
301 is($article->article, '...', '... got the right article');
303 isa_ok($article->start_date, 'DateTime');
304 isa_ok($article->end_date, 'DateTime');
306 isa_ok($article->author, 'Newswriter::Author');
307 is($article->author->first_name, 'Truman', '... got the right author first name');
308 is($article->author->last_name, 'Capote', '... got the right author last name');
311 $article->author->first_name('Dan');
312 $article->author->last_name('Rather');
313 } '... changed the value ok';
315 is($article->author->first_name, 'Dan', '... got the changed author first name');
316 is($article->author->last_name, 'Rather', '... got the changed author last name');
318 is($article->status, 'pending', '... got the right status');
321 Newswriter::Meta::Instance->_reload_db();
326 $article = Newswriter::Article->new(oid => $article_oid);
327 } '... (re)-created my article successfully';
328 isa_ok($article, 'Newswriter::Article');
329 isa_ok($article, 'Newswriter::Base');
331 is($article->oid, $article_oid, '... got a oid for the article');
332 isnt($article_ref, "$article", '... got a new article instance');
334 is($article->headline,
335 'Home Office Redecorated',
336 '... got the right headline');
337 is($article->summary,
338 'The home office was recently redecorated to match the new company colors',
339 '... got the right summary');
340 is($article->article, '...', '... got the right article');
342 isa_ok($article->start_date, 'DateTime');
343 isa_ok($article->end_date, 'DateTime');
345 isa_ok($article->author, 'Newswriter::Author');
346 is($article->author->first_name, 'Dan', '... got the changed author first name');
347 is($article->author->last_name, 'Rather', '... got the changed author last name');
349 is($article->status, 'pending', '... got the right status');
352 unlink('newswriter.db') if -e 'newswriter.db';