File: 002_example_Moose_POOP.t

package info (click to toggle)
libmoose-perl 1.09-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,004 kB
  • ctags: 1,472
  • sloc: perl: 25,387; makefile: 2
file content (439 lines) | stat: -rw-r--r-- 13,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

BEGIN {
    eval "use DBM::Deep 1.0003;";
    plan skip_all => "DBM::Deep 1.0003 (or greater) is required for this test" if $@;
    eval "use DateTime::Format::MySQL;";
    plan skip_all => "DateTime::Format::MySQL is required for this test" if $@;
}

use Test::Exception;

BEGIN {
    # in case there are leftovers
    unlink('newswriter.db') if -e 'newswriter.db';
}

END {
    unlink('newswriter.db') if -e 'newswriter.db';
}


=pod

This example creates a very basic Object Database which
links in the instances created with a backend store
(a DBM::Deep hash). It is by no means to be taken seriously
as a real-world ODB, but is a proof of concept of the flexibility
of the ::Instance protocol.

=cut

BEGIN {

    package Moose::POOP::Meta::Instance;
    use Moose;

    use DBM::Deep;

    extends 'Moose::Meta::Instance';

    {
        my %INSTANCE_COUNTERS;

        my $db = DBM::Deep->new({
            file      => "newswriter.db",
            autobless => 1,
            locking   => 1,
        });

        sub _reload_db {
            #use Data::Dumper;
            #warn Dumper $db;
            $db = undef;
            $db = DBM::Deep->new({
                file      => "newswriter.db",
                autobless => 1,
                locking   => 1,
            });
        }

        sub create_instance {
            my $self  = shift;
            my $class = $self->associated_metaclass->name;
            my $oid   = ++$INSTANCE_COUNTERS{$class};

            $db->{$class}->[($oid - 1)] = {};

            bless {
                oid      => $oid,
                instance => $db->{$class}->[($oid - 1)]
            }, $class;
        }

        sub find_instance {
            my ($self, $oid) = @_;
            my $instance = $db->{$self->associated_metaclass->name}->[($oid - 1)];

            bless {
                oid      => $oid,
                instance => $instance,
            }, $self->associated_metaclass->name;
        }

        sub clone_instance {
            my ($self, $instance) = @_;

            my $class = $self->{meta}->name;
            my $oid   = ++$INSTANCE_COUNTERS{$class};

            my $clone = tied($instance)->clone;

            bless {
                oid      => $oid,
                instance => $clone,
            }, $class;
        }
    }

    sub get_instance_oid {
        my ($self, $instance) = @_;
        $instance->{oid};
    }

    sub get_slot_value {
        my ($self, $instance, $slot_name) = @_;
        return $instance->{instance}->{$slot_name};
    }

    sub set_slot_value {
        my ($self, $instance, $slot_name, $value) = @_;
        $instance->{instance}->{$slot_name} = $value;
    }

    sub is_slot_initialized {
        my ($self, $instance, $slot_name, $value) = @_;
        exists $instance->{instance}->{$slot_name} ? 1 : 0;
    }

    sub weaken_slot_value {
        confess "Not sure how well DBM::Deep plays with weak refs, Rob says 'Write a test'";
    }

    sub inline_slot_access {
        my ($self, $instance, $slot_name) = @_;
        sprintf "%s->{instance}->{%s}", $instance, $slot_name;
    }

    package Moose::POOP::Meta::Class;
    use Moose;

    extends 'Moose::Meta::Class';

    override '_construct_instance' => sub {
        my $class = shift;
        my $params = @_ == 1 ? $_[0] : {@_};
        return $class->get_meta_instance->find_instance($params->{oid})
            if $params->{oid};
        super();
    };

}
{
    package Moose::POOP::Object;
    use metaclass 'Moose::POOP::Meta::Class' => (
        instance_metaclass => 'Moose::POOP::Meta::Instance'
    );
    use Moose;

    sub oid {
        my $self = shift;
        $self->meta
             ->get_meta_instance
             ->get_instance_oid($self);
    }

}
{
    package Newswriter::Author;
    use Moose;

    extends 'Moose::POOP::Object';

    has 'first_name' => (is => 'rw', isa => 'Str');
    has 'last_name'  => (is => 'rw', isa => 'Str');

    package Newswriter::Article;
    use Moose;
    use Moose::Util::TypeConstraints;

    use DateTime::Format::MySQL;

    extends 'Moose::POOP::Object';

    subtype 'Headline'
        => as 'Str'
        => where { length($_) < 100 };

    subtype 'Summary'
        => as 'Str'
        => where { length($_) < 255 };

    subtype 'DateTimeFormatString'
        => as 'Str'
        => where { DateTime::Format::MySQL->parse_datetime($_) };

    enum 'Status' => qw(draft posted pending archive);

    has 'headline' => (is => 'rw', isa => 'Headline');
    has 'summary'  => (is => 'rw', isa => 'Summary');
    has 'article'  => (is => 'rw', isa => 'Str');

    has 'start_date' => (is => 'rw', isa => 'DateTimeFormatString');
    has 'end_date'   => (is => 'rw', isa => 'DateTimeFormatString');

    has 'author' => (is => 'rw', isa => 'Newswriter::Author');

    has 'status' => (is => 'rw', isa => 'Status');

    around 'start_date', 'end_date' => sub {
        my $c    = shift;
        my $self = shift;
        $c->($self, DateTime::Format::MySQL->format_datetime($_[0])) if @_;
        DateTime::Format::MySQL->parse_datetime($c->($self) || return undef);
    };
}

{ # check the meta stuff first
    isa_ok(Moose::POOP::Object->meta, 'Moose::POOP::Meta::Class');
    isa_ok(Moose::POOP::Object->meta, 'Moose::Meta::Class');
    isa_ok(Moose::POOP::Object->meta, 'Class::MOP::Class');

    is(Moose::POOP::Object->meta->instance_metaclass,
      'Moose::POOP::Meta::Instance',
      '... got the right instance metaclass name');

    isa_ok(Moose::POOP::Object->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');

    my $base = Moose::POOP::Object->new;
    isa_ok($base, 'Moose::POOP::Object');
    isa_ok($base, 'Moose::Object');

    isa_ok($base->meta, 'Moose::POOP::Meta::Class');
    isa_ok($base->meta, 'Moose::Meta::Class');
    isa_ok($base->meta, 'Class::MOP::Class');

    is($base->meta->instance_metaclass,
      'Moose::POOP::Meta::Instance',
      '... got the right instance metaclass name');

    isa_ok($base->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');
}

my $article_oid;
my $article_ref;
{
    my $article;
    lives_ok {
        $article = Newswriter::Article->new(
            headline => 'Home Office Redecorated',
            summary  => 'The home office was recently redecorated to match the new company colors',
            article  => '...',

            author => Newswriter::Author->new(
                first_name => 'Truman',
                last_name  => 'Capote'
            ),

            status => 'pending'
        );
    } '... created my article successfully';
    isa_ok($article, 'Newswriter::Article');
    isa_ok($article, 'Moose::POOP::Object');

    lives_ok {
        $article->start_date(DateTime->new(year => 2006, month => 6, day => 10));
        $article->end_date(DateTime->new(year => 2006, month => 6, day => 17));
    } '... add the article date-time stuff';

    ## check some meta stuff

    isa_ok($article->meta, 'Moose::POOP::Meta::Class');
    isa_ok($article->meta, 'Moose::Meta::Class');
    isa_ok($article->meta, 'Class::MOP::Class');

    is($article->meta->instance_metaclass,
      'Moose::POOP::Meta::Instance',
      '... got the right instance metaclass name');

    isa_ok($article->meta->get_meta_instance, 'Moose::POOP::Meta::Instance');

    ok($article->oid, '... got a oid for the article');

    $article_oid = $article->oid;
    $article_ref = "$article";

    is($article->headline,
       'Home Office Redecorated',
       '... got the right headline');
    is($article->summary,
       'The home office was recently redecorated to match the new company colors',
       '... got the right summary');
    is($article->article, '...', '... got the right article');

    isa_ok($article->start_date, 'DateTime');
    isa_ok($article->end_date,   'DateTime');

    isa_ok($article->author, 'Newswriter::Author');
    is($article->author->first_name, 'Truman', '... got the right author first name');
    is($article->author->last_name, 'Capote', '... got the right author last name');

    is($article->status, 'pending', '... got the right status');
}

Moose::POOP::Meta::Instance->_reload_db();

my $article2_oid;
my $article2_ref;
{
    my $article2;
    lives_ok {
        $article2 = Newswriter::Article->new(
            headline => 'Company wins Lottery',
            summary  => 'An email was received today that informed the company we have won the lottery',
            article  => 'WoW',

            author => Newswriter::Author->new(
                first_name => 'Katie',
                last_name  => 'Couric'
            ),

            status => 'posted'
        );
    } '... created my article successfully';
    isa_ok($article2, 'Newswriter::Article');
    isa_ok($article2, 'Moose::POOP::Object');

    $article2_oid = $article2->oid;
    $article2_ref = "$article2";

    is($article2->headline,
       'Company wins Lottery',
       '... got the right headline');
    is($article2->summary,
       'An email was received today that informed the company we have won the lottery',
       '... got the right summary');
    is($article2->article, 'WoW', '... got the right article');

    ok(!$article2->start_date, '... these two dates are unassigned');
    ok(!$article2->end_date,   '... these two dates are unassigned');

    isa_ok($article2->author, 'Newswriter::Author');
    is($article2->author->first_name, 'Katie', '... got the right author first name');
    is($article2->author->last_name, 'Couric', '... got the right author last name');

    is($article2->status, 'posted', '... got the right status');

    ## orig-article

    my $article;
    lives_ok {
        $article = Newswriter::Article->new(oid => $article_oid);
    } '... (re)-created my article successfully';
    isa_ok($article, 'Newswriter::Article');
    isa_ok($article, 'Moose::POOP::Object');

    is($article->oid, $article_oid, '... got a oid for the article');
    isnt($article_ref, "$article", '... got a new article instance');

    is($article->headline,
       'Home Office Redecorated',
       '... got the right headline');
    is($article->summary,
       'The home office was recently redecorated to match the new company colors',
       '... got the right summary');
    is($article->article, '...', '... got the right article');

    isa_ok($article->start_date, 'DateTime');
    isa_ok($article->end_date,   'DateTime');

    isa_ok($article->author, 'Newswriter::Author');
    is($article->author->first_name, 'Truman', '... got the right author first name');
    is($article->author->last_name, 'Capote', '... got the right author last name');

    lives_ok {
        $article->author->first_name('Dan');
        $article->author->last_name('Rather');
    } '... changed the value ok';

    is($article->author->first_name, 'Dan', '... got the changed author first name');
    is($article->author->last_name, 'Rather', '... got the changed author last name');

    is($article->status, 'pending', '... got the right status');
}

Moose::POOP::Meta::Instance->_reload_db();

{
    my $article;
    lives_ok {
        $article = Newswriter::Article->new(oid => $article_oid);
    } '... (re)-created my article successfully';
    isa_ok($article, 'Newswriter::Article');
    isa_ok($article, 'Moose::POOP::Object');

    is($article->oid, $article_oid, '... got a oid for the article');
    isnt($article_ref, "$article", '... got a new article instance');

    is($article->headline,
       'Home Office Redecorated',
       '... got the right headline');
    is($article->summary,
       'The home office was recently redecorated to match the new company colors',
       '... got the right summary');
    is($article->article, '...', '... got the right article');

    isa_ok($article->start_date, 'DateTime');
    isa_ok($article->end_date,   'DateTime');

    isa_ok($article->author, 'Newswriter::Author');
    is($article->author->first_name, 'Dan', '... got the changed author first name');
    is($article->author->last_name, 'Rather', '... got the changed author last name');

    is($article->status, 'pending', '... got the right status');

    my $article2;
    lives_ok {
        $article2 = Newswriter::Article->new(oid => $article2_oid);
    } '... (re)-created my article successfully';
    isa_ok($article2, 'Newswriter::Article');
    isa_ok($article2, 'Moose::POOP::Object');

    is($article2->oid, $article2_oid, '... got a oid for the article');
    isnt($article2_ref, "$article2", '... got a new article instance');

    is($article2->headline,
       'Company wins Lottery',
       '... got the right headline');
    is($article2->summary,
       'An email was received today that informed the company we have won the lottery',
       '... got the right summary');
    is($article2->article, 'WoW', '... got the right article');

    ok(!$article2->start_date, '... these two dates are unassigned');
    ok(!$article2->end_date,   '... these two dates are unassigned');

    isa_ok($article2->author, 'Newswriter::Author');
    is($article2->author->first_name, 'Katie', '... got the right author first name');
    is($article2->author->last_name, 'Couric', '... got the right author last name');

    is($article2->status, 'posted', '... got the right status');

}

done_testing;