File Coverage

blib/lib/Spike/Tree.pm
Criterion Covered Total %
statement 23 32 71.8
branch 1 6 16.6
condition 5 12 41.6
subroutine 7 8 87.5
pod 0 4 0.0
total 36 62 58.0


line stmt bran cond sub pod time code
1             package Spike::Tree;
2              
3 2     2   9 use strict;
  2         3  
  2         44  
4 2     2   7 use warnings;
  2         2  
  2         43  
5              
6 2     2   6 use base qw(Spike::Object);
  2         2  
  2         461  
7              
8 2     2   8 use Scalar::Util qw(weaken);
  2         2  
  2         566  
9              
10             sub new {
11 15     15 0 24 my ($proto, $name) = @_;
12 15   66     26 my $class = ref $proto || $proto;
13              
14 15         30 return $class->SUPER::new(name => $name);
15             }
16              
17 142   100 142 0 75 sub childs { @{shift->{childs} ||= []} }
  142         389  
18              
19             sub add_child {
20 14     14 0 11 my ($self, $child) = @_;
21              
22 14 50       22 if ($child->parent) {
23 0 0       0 return if !$child->parent->rm_child($child);
24             }
25              
26 14   50     11 push @{$self->{childs} ||= []}, $child;
  14         23  
27 14         27 weaken($child->{parent} = $self);
28              
29 14         17 return $self;
30             }
31              
32             sub rm_child {
33 0     0 0   my ($self, $child) = @_;
34              
35 0 0 0       return if !$child->parent || $child->parent != $self;
36              
37 0           $child->{parent} = undef;
38 0   0       @{$self->{childs}} = grep { $_ != $child } @{$self->{childs} ||= []};
  0            
  0            
  0            
39              
40 0           return $self;
41             }
42              
43             __PACKAGE__->mk_accessors(qw(name));
44             __PACKAGE__->mk_ro_accessors(qw(parent));
45              
46             1;