blib/lib/Zoidberg/PluginHash.pm

filter: Statementstmt 66.9% 89 / 133 Branchbran 51.6% 31 / 60 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 3 totaltotal 61.0% 130 / 213 CC 44 SCAR 28.7
CC
44
Cov
62%
STORE
32.3
hash
17.1
DELETE
12.0
CRAP 17.6
« blib/script/zoid ↑ Summary blib/lib/Zoidberg/Utils/FileSystem.pm »
1 package Zoidberg::PluginHash;
2  
3 64 our $VERSION = '0.981';
4  
5 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 64 ▶use Zoidberg::Utils qw/:default read_file merge_hash list_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # $self->[0] = plugin objects hash
9 # $self->[1] = plugin meta data hash
10 # $self->[2] = parent zoid
11  
12 61 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
13 61 my ($class, $zoid) = @_;
14 61 my $self = [{}, {}, $zoid];
15 61 bless $self, $class;
16 61 $self->hash;
17 61 return $self;
18 }
19  
20 46 ▶sub FETCH {
Subroutine FETCH: called CC=4 SCAR=15.5
21 46 my ($self, $key) = @_;
22  
23 46 ▶ return $self->[0]{$key} if exists $self->[0]{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[0]{$key} 30 16 46
24  
25 16 ▶ unless ($self->[1]{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[1]{$key}) 0 16 16
26 0 my @caller = caller;
27 0 error "No such object \'$key\' as requested by $caller[1] line $caller[2]";
28 }
29  
30 0 ▶ $self->load($key) or return sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
$self->load($key) or return sub { undef; } 16 0 16
31 16 return $self->[0]{$key};
32 }
33  
34 80 ▶sub STORE {
Subroutine STORE: called CC=20 SCAR=34.8
35 80 my ($self, $name, $ding) = @_;
36 80 ▶ my $data = ref($ding) ? $ding : { config_file => $ding, %{read_file($ding)} } ;
Branch: 1 branch
BranchTrueFalseTotal
ref $ding ? : 16 64 80
37  
38 80 ▶ if (exists $$data{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"object"}) 0 80 80
39 $$data{object}{zoidname} = $name
40 0 ▶ if eval{ $$data{object}->isa( 'Zoidberg::Fish' ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $data->{"object"}->isa("Zoidberg::Fish") } } 0 0 0
41 $self->[0]{$name} = $$data{object}
42 0 }
43  
44 # settings && aliases
45 80 for my $t (qw/settings aliases/) {
46 160 $$self[2]{$t}{$_} = $$data{$t}{$_} for keys %{$$data{$t}};
47 160 delete $$data{$t};
48 }
49  
50 # config
51 $self->[2]{settings}{$name} = merge_hash(
52 $$data{config},
53 80 ▶ $self->[2]{settings}{$name}
Condition50%1/2
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name}) || {}
l!l
800
MC/DC0%0/1
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name}) || {}
merge_hash($data->{'config'}, $self->[2]{'settings'}{$name})
Truth table50%1/2
A
A= merge_hash($data->{'config'}, $self->[2]{'settings'}{$name})
Aresult
11
00
54 ) || {};
55 80 delete $$data{config};
56
57 # commands
58 80 for (keys %{$$data{commands}}) {
59 $$data{commands}{$_} =~ s/^(\w)/->$name->$1/
60 32 ▶ unless ref $$data{commands}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"commands"}{$_} 32 0 32
61 }
62 80 ▶ if (exists $$data{export}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"export"}) 64 16 80
63 $$data{commands}{$_} = "->$name->$_"
64 64 for @{$$data{export}};
65 64 delete $$data{export};
66 }
67 80 my ($c, $s);
68 80 ▶ while( ($c, $s) = each %{$$data{commands}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$data{"commands"};}) 624 80 704
69 624 $self->[2]{commands}{$c} = [$s, $name];
70 }
71 80 delete $$data{commands};
72  
73 # events
74 80 for (keys %{$$data{events}}) {
75 $$data{events}{$_} =~ s/^(\w)/->$name->$1/
76 16 ▶ unless ref $$data{events}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"events"}{$_} 16 0 16
77 }
78 80 ▶ if (exists $$data{import}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"import"}) 0 80 80
79 $$data{events}{$_} = "->$name->$_"
80 0 for @{$$data{import}};
81 0 delete $$data{import};
82 }
83 80 ▶ while( ($c, $s) = each %{$$data{events}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$data{"events"};}) 16 80 96
84 16 $self->[2]{events}{$c} = [$s, $name];
85 }
86 80 delete $$data{events};
87  
88 # parser
89 80 ▶ if (exists $$data{parser}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"parser"}) 0 80 80
90 0 require Zoidberg::Fish;
91 0 ▶ my @c = (ref($$data{parser}) eq 'ARRAY') ? (@{$$data{parser}}) : ($$data{parser});
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'parser'} eq 'ARRAY' ? : 0 0 0
92 0 Zoidberg::Fish::add_context({zoidname => $name, shell => $$self[2]}, $_) for @c;
93 0 delete $$data{parser};
94 }
95  
96 80 $self->[1]{$name} = $data;
97 }
98  
99 64 our @_keys;
100  
101 0 ▶sub FIRSTKEY { @_keys = keys %{$_[0][1]}; shift @_keys }
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
102  
103 0 ▶sub NEXTKEY { shift @_keys }
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
104  
105 137 ▶sub EXISTS { exists $_[0][1]->{$_[1]} }
Subroutine EXISTS: called CC=1 SCAR=0.0
106  
107 0 ▶sub DELETE { # leaves config intact
Subroutine DELETE: not called CC=3 SCAR=24.8
108 0 my ($self, $key) = @_;
109 0 ▶ $$self[0]{$key}->round_up() if eval { $self->[0]{$key}->isa( 'Zoidberg::Fish' ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->[0]{$key}->isa("Zoidberg::Fish") } } 0 0 0
110 0 delete $$self[0]{$key};
111 0 $$self[2]{$_}->wipe($key) for qw/events commands/; # wipe DispatchTable stacks
112 0 $$self[2]->broadcast('unplug_'.$key);
113 0 return $$self[1]{$key};
114 }
115  
116 0 ▶sub CLEAR { $_[0]->DELETE($_) for keys %{$_[0][1]} }
Subroutine CLEAR: not called CC=2 SCAR=17.9
117  
118 61 ▶sub hash {
Subroutine hash: called CC=11 SCAR=28.4
Pod undocumented
119 61 my $self = shift;
120  
121 # TODO how about an ignore list for users who disagree with there admin ?
122  
123 61 $self->[1] = {};
124 61 for my $dir (map "$_/plugins", @{$self->[2]{settings}{data_dirs}}) {
125 61 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 45 16 61
126 16 for (list_dir($dir)) {
127 80 ▶ /^(\w+)/ || next;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)/ or next 80 0 80
128 80 my ($name, $ding) = ($1, "$dir/$_");
129 80 ▶ next if exists $$self[1]{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[1]{$name} 0 80 80
130 80 ▶ if (-d "$dir/$_") {
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$dir/$_") { } 0 80 80
elsif (/.pm$/) { } 16 64 80
131 0 my ($conf) = grep /^PluginConf/, list_dir("$dir/$_");
132 0 ▶ next unless $conf;
Branch: 1 branch
BranchTrueFalseTotal
unless $conf 0 0 0
133 0 unshift @INC, "$dir/$_";
134 0 ▶ unshift @{$self->[2]{settings}{data_dirs}}, "$dir/$_/data"
Branch: 1 branch
BranchTrueFalseTotal
if -d "$dir/$_/data" 0 0 0
135 if -d "$dir/$_/data";
136 0 $ding = "$dir/$_/$conf";
137 }
138 elsif (/.pm$/) {
139 16 my $class = $_;
140 16 $class =~ s/.pm$//;
141 16 $ding = {module => $class, pmfile => "$dir/$_"};
142 }
143 80 eval { $self->STORE($name, $ding) };
144 80 ▶ complain if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 80 80
145 }
146 }
147 }
148  
149 16 ▶sub load {
Subroutine load: called CC=7 SCAR=24.7
Pod undocumented
150 16 my ($self, $zoidname, @args) = @_;
151 16 my $class = $$self[1]{$zoidname}{module};
152 16 ▶ unless ($class) { # FIXME is this allright and does it belong in this package ?
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 16 16
153 $self->[0]{$zoidname} = {
154 shell => $self->[2],
155 zoidname => $zoidname,
156 settings => $self->[2]->{settings},
157 0 config => $self->[2]->{settings}{$zoidname},
158 };
159 0 debug "Loaded stub plugin $zoidname";
160 0 $$self[2]->broadcast('plug_'.$zoidname);
161 0 return $self->[0]{$zoidname};
162 }
163  
164 16 my $req = $class;
165 16 ▶ $req = '\''.$$self[1]{$zoidname}{pmfile}.'\'' if exists $$self[1]{$zoidname}{pmfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[1]{$zoidname}{"pmfile"} 0 16 16
166 16 debug "Going to load plugin $zoidname of class $class, requiring $req";
167 16 eval "require $req";
168 16 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 16 0 16
169 16 ▶ if (eval{ $class->isa( 'Zoidberg::Fish' ) }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $class->isa('Zoidberg::Fish') } }) { } 16 0 16
elsif ($class->can('new')) { } 0 0 0
170 16 $self->[0]{$zoidname} = $class->new($self->[2], $zoidname);
171 16 $self->[0]{$zoidname}->init(@args);
172 }
173 0 elsif ($class->can('new')) { $self->[0]{$zoidname} = $class->new(@args) }
174 0 else { error "Module $class doesn't seem to be Object Oriented" }
175 } unless $@;
176 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 16 16
177 0 $@ =~ s/\n$/ /;
178 0 complain "Failed to load class: $class ($@)\nDisabling plugin: $zoidname";
179 0 $self->DELETE($zoidname);
180 0 delete $$self[1]{$zoidname};
181 0 return undef;
182 }
183 else {
184 16 debug "Loaded plugin $zoidname";
185 16 $$self[2]->broadcast('plug_'.$zoidname);
186 16 return $self->[0]{$zoidname};
187 }
188 }
189  
190 3 ▶sub round_up {
Subroutine round_up: called CC=3 SCAR=11.1
Pod undocumented
191 3 my $self = shift;
192 3 for (keys %{$$self[0]}) {
193 $$self[0]{$_}->round_up(@_)
194 2 ▶ if eval{ $$self[0]{$_}->isa( 'Zoidberg::Fish' ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->[0]{$_}->isa("Zoidberg::Fish") } } 2 0 2
195 }
196 }
197  
198 64 1;
199  
200 __END__
« blib/script/zoid ↑ Summary blib/lib/Zoidberg/Utils/FileSystem.pm »