EO/Hierarchy.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 66.6% 8 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 33.3% 1 / 3 totaltotal 79.1% 57 / 72 CC 9 SCAR 17.2
CC
9
Cov
82%
at
9.9
init
2.1
BEGIN
1.0
CRAP 5.6
« EO/Data.pm ↑ Summary EO/Error.pm »
1 package EO::Hierarchy;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use EO::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use EO::delegate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base qw( EO );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = 0.96;
11  
12 1 EO::Hierarchy->mk_accessors( qw( parent ) );
13  
14 1 exception EO::Hierarchy::Error::NoParent;
15 1 exception EO::Hierarchy::Error::InvalidState;
16  
17 2 ▶sub init {
Subroutine init: called CC=2 SCAR=7.4
Pod undocumented
18 2 my $self = shift;
19 2 ▶ if ($self->SUPER::init( @_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SUPER::init(@_)) 2 0 2
20 2 $self->delegate( EO::Hash->new );
21 2 return 1;
22 }
23 0 return 0;
24 }
25  
26 1 ▶sub add_child {
Subroutine add_child: called CC=1 SCAR=0.0
Pod documented
27 1 my $self = shift;
28 1 my $name = shift;
29 ## we want to share the delegated class, which makes this look
30 ## sort of ugly, but not bad enough to make me want a 'class' method.
31 ## on everything.
32 1 my $child = ref($self)->new
33 ->delegate( ref($self->delegate)->new )
34 ->parent( $self );
35 1 $self->at($name, $child);
36 }
37  
38  
39 ## this took me a while to work out, so I'd better document it.
40 5 ▶sub at {
Subroutine at: called CC=8 SCAR=22.9
Pod undocumented
41 5 my $self = shift;
42 5 my $key = shift;
43  
44 5 ▶ throw EO::Error::InvalidParameters text => 'no key specified' unless defined( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 5 5
45  
46 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 3 5
47 ## if its a set, then we do just that, no
48 ## need to worry about it.
49 2 return $self->delegate->at( $key, @_ );
50 }
51  
52 ## right, its a get, here is the fun.
53 3 ▶ if ( !defined( $self->delegate->at( $key ) ) && $self->parent ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $self->delegate->at($key) and $self->parent) { } 1 2 3
elsif (not defined $self->delegate->at($key) || $self->parent) { } 0 2 2
elsif (defined $self->delegate->at($key)) { } 2 0 2
Condition66%2/3
not defined $self->delegate->at($key) and $self->parent
!ll&&!rl&&r
201
MC/DC50%1/2
not defined $self->delegate->at($key) and $self->parent
not defined $self->delegate->at($key) $self->parent
Truth table66%2/3
not(A) and B
A= not defined $self->delegate->at($key)B= $self->parent
ABresult
0X0
100
111
54 ## if we don't have a defined value at the key, but
55 ## we do have a parent then we ask the parent.
56 1 return $self->parent->at( $key );
57 ▶ } elsif (!defined( $self->delegate->at($key) ) && !$self->parent) {
Condition33%1/3
defined $self->delegate->at($key) || $self->parent
l!l&&r!l&&!r
200
MC/DC0%0/2
defined $self->delegate->at($key) || $self->parent
defined $self->delegate->at($key) $self->parent
Truth table33%1/3
A || B
A= defined $self->delegate->at($key)B= $self->parent
ABresult
1X1
011
000
58 ## if we don't have a defined value at the key, and
59 ## we have no parent, then we throw an Exception.
60 0 throw EO::Hierarchy::Error::NoParent
61 text => 'have gone as far as we can';
62 } elsif (defined( $self->delegate->at( $key ) )) {
63 ## if we have a key, then we return it
64 2 return $self->delegate->at( $key );
65 } else {
66 ## we should absolutely never get here. I don't know
67 ## how we would get here, but it all seems hairy enough
68 ## to warrant an exception.
69 0 throw EO::Hierarchy::Error::InvalidState
70 text => 'how the heck did we get here?'
71 }
72 }
73  
74 1 1;
75  
76 __END__
« EO/Data.pm ↑ Summary EO/Error.pm »