6 use Test::More tests => 23;
10 Some examples of triggers and how they can
11 be used to manage parent-child relationships.
24 # if the parents last-name changes
25 # then so do all the childrens
26 foreach my $child (@{$self->children}) {
27 $child->last_name($self->last_name);
32 has 'children' => (is => 'rw', isa => 'ArrayRef', default => sub {[]});
44 # if the parent is changed,..
46 $self->last_name($self->parent->last_name);
54 default => sub { (shift)->parent->last_name }
59 my $parent = Parent->new(last_name => 'Smith');
60 isa_ok($parent, 'Parent');
62 is($parent->last_name, 'Smith', '... the parent has the last name we expected');
65 map { Child->new(parent => $parent) } (0 .. 3)
68 foreach my $child (@{$parent->children}) {
69 is($child->last_name, $parent->last_name, '... parent and child have the same last name (' . $parent->last_name . ')');
72 $parent->last_name('Jones');
73 is($parent->last_name, 'Jones', '... the parent has the new last name');
75 foreach my $child (@{$parent->children}) {
76 is($child->last_name, $parent->last_name, '... parent and child have the same last name (' . $parent->last_name . ')');
81 my $parent2 = Parent->new(last_name => 'Brown');
82 isa_ok($parent2, 'Parent');
86 my $orphan = pop @{$parent->children};
88 # and then the new parent adopts it
90 $orphan->parent($parent2);
92 foreach my $child (@{$parent->children}) {
93 is($child->last_name, $parent->last_name, '... parent and child have the same last name (' . $parent->last_name . ')');
96 isnt($orphan->last_name, $parent->last_name, '... the orphan child does not have the same last name anymore (' . $parent2->last_name . ')');
97 is($orphan->last_name, $parent2->last_name, '... parent2 and orphan child have the same last name (' . $parent2->last_name . ')');
99 # make sure that changes still will not propagate
101 $parent->last_name('Miller');
102 is($parent->last_name, 'Miller', '... the parent has the new last name (again)');
104 foreach my $child (@{$parent->children}) {
105 is($child->last_name, $parent->last_name, '... parent and child have the same last name (' . $parent->last_name . ')');
108 isnt($orphan->last_name, $parent->last_name, '... the orphan child is not affected by changes in the parent anymore');
109 is($orphan->last_name, $parent2->last_name, '... parent2 and orphan child have the same last name (' . $parent2->last_name . ')');