Commit | Line | Data |
daa0fd7d |
1 | package Moose::Cookbook::Basics::Recipe11; |
2 | |
3 | # ABSTRACT: Extending a non-Moose base class |
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; |
26 | extends qw( DateTime ); |
04d80e2a |
27 | |
1f476b5f |
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', |
04d80e2a |
36 | ); |
37 | |
1f476b5f |
38 | sub new { |
04d80e2a |
39 | my $class = shift; |
40 | |
1f476b5f |
41 | my $obj = $class->SUPER::new(@_); |
42 | |
43 | return $class->meta->new_object( |
44 | __INSTANCE__ => $obj, |
45 | @_, |
46 | ); |
04d80e2a |
47 | } |
48 | |
1f476b5f |
49 | after 'set' => sub { |
50 | $_[0]->_clear_mayan_date; |
51 | }; |
04d80e2a |
52 | |
1f476b5f |
53 | sub _build_mayan_date { |
54 | DateTime::Calendar::Mayan->from_object( object => $_[0] ); |
04d80e2a |
55 | } |
56 | |
57 | =head1 DESCRIPTION |
58 | |
1f476b5f |
59 | This recipe demonstrates how to use Moose to subclass a parent which |
60 | is not Moose based. This recipe only works if the parent class uses a |
61 | blessed hash reference for object instances. If your parent is doing |
a68c47da |
62 | something funkier, you should check out L<MooseX::NonMoose::InsideOut> and L<MooseX::InsideOut>. |
04d80e2a |
63 | |
f14f6809 |
64 | The meat of this recipe is contained in L<MooseX::NonMoose>, which does all |
1f476b5f |
65 | the grunt work for you. |
04d80e2a |
66 | |
1f476b5f |
67 | =begin testing |
68 | |
69 | my $dt = My::DateTime->new( year => 1970, month => 2, day => 24 ); |
70 | |
71 | can_ok( $dt, 'mayan_date' ); |
72 | isa_ok( $dt->mayan_date, 'DateTime::Calendar::Mayan' ); |
73 | is( $dt->mayan_date->date, '12.17.16.9.19', 'got expected mayan date' ); |
74 | |
75 | $dt->set( year => 2009 ); |
76 | ok( ! $dt->has_mayan_date, 'mayan_date is cleared after call to ->set' ); |
77 | |
78 | =end testing |
79 | |
04d80e2a |
80 | =cut |