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;
141 extends 'Newswriter::Base';
143 has 'first_name' => (is => 'rw', isa => 'Str');
144 has 'last_name' => (is => 'rw', isa => 'Str');
146 package Newswriter::Article;
150 use Moose::Util::TypeConstraints;
152 use DateTime::Format::MySQL;
154 extends 'Newswriter::Base';
158 => where { length($_) < 100 };
162 => where { length($_) < 255 };
164 subtype 'DateTimeFormatString'
166 => where { DateTime::Format::MySQL->parse_datetime($_) };
168 enum 'Status' => qw(draft posted pending archive);
170 has 'headline' => (is => 'rw', isa => 'Headline');
171 has 'summary' => (is => 'rw', isa => 'Summary');
172 has 'article' => (is => 'rw', isa => 'Str');
174 has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
175 has 'end_date' => (is => 'rw', isa => 'DateTimeFormatString');
177 has 'author' => (is => 'rw', isa => 'Newswriter::Author');
179 has 'status' => (is => 'rw', isa => 'Status');
181 around 'start_date', 'end_date' => sub {
184 $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
185 DateTime::Format::MySQL->parse_datetime($c->($self));
189 { # check the meta stuff first
190 isa_ok(Newswriter::Base->meta, 'Newswriter::Meta::Class');
191 isa_ok(Newswriter::Base->meta, 'Moose::Meta::Class');
192 isa_ok(Newswriter::Base->meta, 'Class::MOP::Class');
194 is(Newswriter::Base->meta->instance_metaclass,
195 'Newswriter::Meta::Instance',
196 '... got the right instance metaclass name');
198 isa_ok(Newswriter::Base->meta->get_meta_instance, 'Newswriter::Meta::Instance');
200 my $base = Newswriter::Base->new;
201 isa_ok($base, 'Newswriter::Base');
202 isa_ok($base, 'Moose::Object');
204 isa_ok($base->meta, 'Newswriter::Meta::Class');
205 isa_ok($base->meta, 'Moose::Meta::Class');
206 isa_ok($base->meta, 'Class::MOP::Class');
208 is($base->meta->instance_metaclass,
209 'Newswriter::Meta::Instance',
210 '... got the right instance metaclass name');
212 isa_ok($base->meta->get_meta_instance, 'Newswriter::Meta::Instance');
220 $article = Newswriter::Article->new(
221 headline => 'Home Office Redecorated',
222 summary => 'The home office was recently redecorated to match the new company colors',
225 author => Newswriter::Author->new(
226 first_name => 'Truman',
227 last_name => 'Capote'
232 } '... created my article successfully';
233 isa_ok($article, 'Newswriter::Article');
234 isa_ok($article, 'Newswriter::Base');
237 $article->start_date(DateTime->new(year => 2006, month => 6, day => 10));
238 $article->end_date(DateTime->new(year => 2006, month => 6, day => 17));
239 } '... add the article date-time stuff';
241 ## check some meta stuff
243 isa_ok($article->meta, 'Newswriter::Meta::Class');
244 isa_ok($article->meta, 'Moose::Meta::Class');
245 isa_ok($article->meta, 'Class::MOP::Class');
247 is($article->meta->instance_metaclass,
248 'Newswriter::Meta::Instance',
249 '... got the right instance metaclass name');
251 isa_ok($article->meta->get_meta_instance, 'Newswriter::Meta::Instance');
253 ok($article->oid, '... got a oid for the article');
255 $article_oid = $article->oid;
256 $article_ref = "$article";
258 is($article->headline,
259 'Home Office Redecorated',
260 '... got the right headline');
261 is($article->summary,
262 'The home office was recently redecorated to match the new company colors',
263 '... got the right summary');
264 is($article->article, '...', '... got the right article');
266 isa_ok($article->start_date, 'DateTime');
267 isa_ok($article->end_date, 'DateTime');
269 isa_ok($article->author, 'Newswriter::Author');
270 is($article->author->first_name, 'Truman', '... got the right author first name');
271 is($article->author->last_name, 'Capote', '... got the right author last name');
273 is($article->status, 'pending', '... got the right status');
276 Newswriter::Meta::Instance->_reload_db();
281 $article = Newswriter::Article->new(oid => $article_oid);
282 } '... (re)-created my article successfully';
283 isa_ok($article, 'Newswriter::Article');
284 isa_ok($article, 'Newswriter::Base');
286 is($article->oid, $article_oid, '... got a oid for the article');
287 isnt($article_ref, "$article", '... got a new article instance');
289 is($article->headline,
290 'Home Office Redecorated',
291 '... got the right headline');
292 is($article->summary,
293 'The home office was recently redecorated to match the new company colors',
294 '... got the right summary');
295 is($article->article, '...', '... got the right article');
297 isa_ok($article->start_date, 'DateTime');
298 isa_ok($article->end_date, 'DateTime');
300 isa_ok($article->author, 'Newswriter::Author');
301 is($article->author->first_name, 'Truman', '... got the right author first name');
302 is($article->author->last_name, 'Capote', '... got the right author last name');
305 $article->author->first_name('Dan');
306 $article->author->last_name('Rather');
307 } '... changed the value ok';
309 is($article->author->first_name, 'Dan', '... got the changed author first name');
310 is($article->author->last_name, 'Rather', '... got the changed author last name');
312 is($article->status, 'pending', '... got the right status');
315 Newswriter::Meta::Instance->_reload_db();
320 $article = Newswriter::Article->new(oid => $article_oid);
321 } '... (re)-created my article successfully';
322 isa_ok($article, 'Newswriter::Article');
323 isa_ok($article, 'Newswriter::Base');
325 is($article->oid, $article_oid, '... got a oid for the article');
326 isnt($article_ref, "$article", '... got a new article instance');
328 is($article->headline,
329 'Home Office Redecorated',
330 '... got the right headline');
331 is($article->summary,
332 'The home office was recently redecorated to match the new company colors',
333 '... got the right summary');
334 is($article->article, '...', '... got the right article');
336 isa_ok($article->start_date, 'DateTime');
337 isa_ok($article->end_date, 'DateTime');
339 isa_ok($article->author, 'Newswriter::Author');
340 is($article->author->first_name, 'Dan', '... got the changed author first name');
341 is($article->author->last_name, 'Rather', '... got the changed author last name');
343 is($article->status, 'pending', '... got the right status');
346 unlink('newswriter.db') if -e 'newswriter.db';