6 use Test::More tests => 23;
10 Some examples of triggers and how they can
11 be used to manage parent-child relationships.
26 # if the parents last-name changes
27 # then so do all the childrens
28 foreach my $child ( @{ $self->children } ) {
29 $child->last_name( $self->last_name );
35 ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
49 # if the parent is changed,..
51 $self->last_name( $self->parent->last_name );
59 default => sub { (shift)->parent->last_name }
64 my $parent = Parent->new( last_name => 'Smith' );
65 isa_ok( $parent, 'Parent' );
67 is( $parent->last_name, 'Smith',
68 '... the parent has the last name we expected' );
70 $parent->children( [ map { Child->new( parent => $parent ) } ( 0 .. 3 ) ] );
72 foreach my $child ( @{ $parent->children } ) {
73 is( $child->last_name, $parent->last_name,
74 '... parent and child have the same last name ('
79 $parent->last_name('Jones');
80 is( $parent->last_name, 'Jones', '... the parent has the new last name' );
82 foreach my $child ( @{ $parent->children } ) {
83 is( $child->last_name, $parent->last_name,
84 '... parent and child have the same last name ('
91 my $parent2 = Parent->new( last_name => 'Brown' );
92 isa_ok( $parent2, 'Parent' );
96 my $orphan = pop @{ $parent->children };
98 # and then the new parent adopts it
100 $orphan->parent($parent2);
102 foreach my $child ( @{ $parent->children } ) {
103 is( $child->last_name, $parent->last_name,
104 '... parent and child have the same last name ('
109 isnt( $orphan->last_name, $parent->last_name,
110 '... the orphan child does not have the same last name anymore ('
111 . $parent2->last_name
113 is( $orphan->last_name, $parent2->last_name,
114 '... parent2 and orphan child have the same last name ('
115 . $parent2->last_name
118 # make sure that changes still will not propagate
120 $parent->last_name('Miller');
121 is( $parent->last_name, 'Miller',
122 '... the parent has the new last name (again)' );
124 foreach my $child ( @{ $parent->children } ) {
125 is( $child->last_name, $parent->last_name,
126 '... parent and child have the same last name ('
131 isnt( $orphan->last_name, $parent->last_name,
132 '... the orphan child is not affected by changes in the parent anymore' );
133 is( $orphan->last_name, $parent2->last_name,
134 '... parent2 and orphan child have the same last name ('
135 . $parent2->last_name