Commit | Line | Data |
22883e5b |
1 | package Moose::Cookbook::Basics::DateTime_ExtendingNonMooseParent; |
daa0fd7d |
2 | |
22883e5b |
3 | # ABSTRACT: Extending a non-Moose parent class |
daa0fd7d |
4 | |
5 | __END__ |
6 | |
04d80e2a |
7 | |
8 | =pod |
9 | |
1f476b5f |
10 | =begin testing-SETUP |
11 | |
0adca353 |
12 | use Test::Requires { |
13 | 'DateTime' => '0', |
14 | 'DateTime::Calendar::Mayan' => '0', |
f14f6809 |
15 | 'MooseX::NonMoose' => '0', |
0adca353 |
16 | }; |
1f476b5f |
17 | |
18 | =end testing-SETUP |
19 | |
04d80e2a |
20 | =head1 SYNOPSIS |
21 | |
1f476b5f |
22 | package My::DateTime; |
04d80e2a |
23 | |
1f476b5f |
24 | use Moose; |
f14f6809 |
25 | use MooseX::NonMoose; |
639b87e2 |
26 | use DateTime::Calendar::Mayan; |
f14f6809 |
27 | extends qw( DateTime ); |
04d80e2a |
28 | |
1f476b5f |
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', |
04d80e2a |
37 | ); |
38 | |
1f476b5f |
39 | after 'set' => sub { |
40 | $_[0]->_clear_mayan_date; |
41 | }; |
04d80e2a |
42 | |
1f476b5f |
43 | sub _build_mayan_date { |
44 | DateTime::Calendar::Mayan->from_object( object => $_[0] ); |
04d80e2a |
45 | } |
46 | |
47 | =head1 DESCRIPTION |
48 | |
1f476b5f |
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 |
a68c47da |
52 | something funkier, you should check out L<MooseX::NonMoose::InsideOut> and L<MooseX::InsideOut>. |
04d80e2a |
53 | |
f14f6809 |
54 | The meat of this recipe is contained in L<MooseX::NonMoose>, which does all |
1f476b5f |
55 | the grunt work for you. |
04d80e2a |
56 | |
1f476b5f |
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 | |
04d80e2a |
70 | =cut |