Rename Basics::Recipe10 to Basics::Person_BUILDARGSAndBUILD
[gitmo/Moose.git] / lib / Moose / Cookbook / Basics / Recipe11.pod
1 package Moose::Cookbook::Basics::Recipe11;
2
3 # ABSTRACT: Extending a non-Moose base class
4
5 __END__
6
7
8 =pod
9
10 =begin testing-SETUP
11
12 use Test::Requires {
13     'DateTime'                  => '0',
14     'DateTime::Calendar::Mayan' => '0',
15     'MooseX::NonMoose'          => '0',
16 };
17
18 =end testing-SETUP
19
20 =head1 SYNOPSIS
21
22   package My::DateTime;
23
24   use Moose;
25   use MooseX::NonMoose;
26   use DateTime::Calendar::Mayan;
27   extends qw( DateTime );
28
29   has 'mayan_date' => (
30       is        => 'ro',
31       isa       => 'DateTime::Calendar::Mayan',
32       init_arg  => undef,
33       lazy      => 1,
34       builder   => '_build_mayan_date',
35       clearer   => '_clear_mayan_date',
36       predicate => 'has_mayan_date',
37   );
38
39   after 'set' => sub {
40       $_[0]->_clear_mayan_date;
41   };
42
43   sub _build_mayan_date {
44       DateTime::Calendar::Mayan->from_object( object => $_[0] );
45   }
46
47 =head1 DESCRIPTION
48
49 This recipe demonstrates how to use Moose to subclass a parent which
50 is not Moose based. This recipe only works if the parent class uses a
51 blessed hash reference for object instances. If your parent is doing
52 something funkier, you should check out L<MooseX::NonMoose::InsideOut> and L<MooseX::InsideOut>.
53
54 The meat of this recipe is contained in L<MooseX::NonMoose>, which does all
55 the grunt work for you.
56
57 =begin testing
58
59 my $dt = My::DateTime->new( year => 1970, month => 2, day => 24 );
60
61 can_ok( $dt, 'mayan_date' );
62 isa_ok( $dt->mayan_date, 'DateTime::Calendar::Mayan' );
63 is( $dt->mayan_date->date, '12.17.16.9.19', 'got expected mayan date' );
64
65 $dt->set( year => 2009 );
66 ok( ! $dt->has_mayan_date, 'mayan_date is cleared after call to ->set' );
67
68 =end testing
69
70 =cut