overriding new isn't necessary when using mx-nonmoose
[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   extends qw( DateTime );
27
28   has 'mayan_date' => (
29       is        => 'ro',
30       isa       => 'DateTime::Calendar::Mayan',
31       init_arg  => undef,
32       lazy      => 1,
33       builder   => '_build_mayan_date',
34       clearer   => '_clear_mayan_date',
35       predicate => 'has_mayan_date',
36   );
37
38   after 'set' => sub {
39       $_[0]->_clear_mayan_date;
40   };
41
42   sub _build_mayan_date {
43       DateTime::Calendar::Mayan->from_object( object => $_[0] );
44   }
45
46 =head1 DESCRIPTION
47
48 This recipe demonstrates how to use Moose to subclass a parent which
49 is not Moose based. This recipe only works if the parent class uses a
50 blessed hash reference for object instances. If your parent is doing
51 something funkier, you should check out L<MooseX::NonMoose::InsideOut> and L<MooseX::InsideOut>.
52
53 The meat of this recipe is contained in L<MooseX::NonMoose>, which does all
54 the grunt work for you.
55
56 =begin testing
57
58 my $dt = My::DateTime->new( year => 1970, month => 2, day => 24 );
59
60 can_ok( $dt, 'mayan_date' );
61 isa_ok( $dt->mayan_date, 'DateTime::Calendar::Mayan' );
62 is( $dt->mayan_date->date, '12.17.16.9.19', 'got expected mayan date' );
63
64 $dt->set( year => 2009 );
65 ok( ! $dt->has_mayan_date, 'mayan_date is cleared after call to ->set' );
66
67 =end testing
68
69 =cut