Remember/Class.pm

filter: Statementstmt 78.9% 90 / 114 Branchbran 58.3% 21 / 36 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 88.8% 16 / 18 Podpod 100% 9 / 9 totaltotal 75.9% 148 / 195 CC 24 SCAR 15.8
CC
24
Cov
74%
forget_when
9.1
recall_each
8.9
__ANON__
6.0
CRAP 4.9
« Remember/Util.pm ↑ Summary Remember/Memory.pm »
1 package Data::Remember::Class;
2 {
3 11 $Data::Remember::Class::VERSION = '0.140490';
4 }
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: remember complex information without giving yourself a headache, now with POOP!
9  
10 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Data::Remember::Util
14 11 ▶ process_que => { -as => '_process_que' },
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 init_brain => { -as => '_init_brain' };
16  
17  
18 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
19 11 my $class = shift;
20 11 ▶ my $brain = shift || 'Memory';
Condition50%1/2
shift() || 'Memory'
l!l
110
MC/DC0%0/1
shift() || 'Memory'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
21  
22 11 my $caller = caller;
23  
24 11 my $gray_matter = _init_brain($brain, @_);
25  
26 11 return bless { brain => $gray_matter }, $class;
27 }
28  
29  
30 154 ▶sub remember {
Subroutine remember: called CC=2 SCAR=7.6
Pod documented
31 154 my ($self, $que, $fact) = @_;
32  
33 154 my $clean_que = _process_que($que);;
34  
35 154 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 154 154
36 0 carp "Undefined que element found in call to remember().";
37 0 return;
38 }
39  
40 154 $self->{brain}->remember($clean_que, $fact);
41  
42 154 return;
43 }
44  
45  
46 15 ▶sub remember_these {
Subroutine remember_these: called CC=4 SCAR=14.3
Pod documented
47 15 my ($self, $que, $fact) = @_;
48  
49 15 my $clean_que = _process_que($que);;
50  
51 15 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 15 15
52 0 carp "Undefined que element found in call to remember_these().";
53 0 return;
54 }
55  
56 15 my $brain = $self->{brain};
57  
58 15 my $fact_list = $brain->recall($clean_que);
59  
60 15 ▶ if (defined reftype $fact_list and reftype $fact_list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::reftype($fact_list) and builtin::reftype($fact_list) eq 'ARRAY') { } 10 5 15
Condition66%2/3
defined builtin::reftype($fact_list) and builtin::reftype($fact_list) eq "ARRAY"
!ll&&!rl&&r
5010
MC/DC50%1/2
defined builtin::reftype($fact_list) and builtin::reftype($fact_list) eq "ARRAY"
defined builtin::reftype($fact_list) builtin::reftype($fact_list) eq "ARRAY"
Truth table66%2/3
A and B
A= defined builtin::reftype($fact_list)B= builtin::reftype($fact_list) eq "ARRAY"
ABresult
0X0
100
111
61 10 push @$fact_list, $fact;
62 }
63  
64 else {
65 5 $brain->remember($clean_que, [ $fact ]);
66 }
67  
68 15 return;
69 }
70  
71  
72 288 ▶sub recall {
Subroutine recall: called CC=2 SCAR=7.9
Pod documented
73 288 my ($self, $que) = @_;
74  
75 288 my $clean_que = _process_que($que);
76  
77 288 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 288 288
78 0 carp "Undefined que element used in call to recall().";
79 0 return;
80 }
81  
82 288 return scalar $self->{brain}->recall($clean_que);
83 }
84  
85  
86 10 ▶sub recall_each {
Subroutine recall_each: called CC=5 SCAR=21.9
Pod documented
87 10 my ($self, $que) = @_;
88 10 my $brain = $self->{brain};
89  
90 10 my $clean_que = _process_que($que);
91  
92 10 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 10 10
93 0 carp "Undefined que element used in call to recall_each().";
94 0 return;
95 }
96  
97 10 my $value = $brain->recall($clean_que);
98  
99 10 my $value_ref_type = reftype($value);
100 10 ▶ $value_ref_type = '' unless defined $value_ref_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_ref_type 0 10 10
101  
102 10 ▶ if ($value_ref_type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value_ref_type eq 'HASH') { } 10 0 10
elsif ($value_ref_type eq 'ARRAY') { } 0 0 0
103 10 my @keys = keys %$value;
104 40 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
105 40 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 10 30 40
106 30 my $k = shift @keys;
107 30 return ($k, $value->{$k});
108 10 };
109 }
110  
111 elsif ($value_ref_type eq 'ARRAY') {
112 0 my @indexes = 0 .. $#$value;
113 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 ▶ return unless @indexes;
Branch: 1 branch
BranchTrueFalseTotal
unless @indexes 0 0 0
115 0 my $i = shift @indexes;
116 0 return ($i, $value->[$i]);
117 0 };
118 }
119  
120 else {
121 0 my @values = ($value);
122 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
123 0 ▶ return unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 0 0
124 0 my $v = shift @values;
125 0 return (undef, $v);
126 0 };
127 }
128 }
129  
130  
131 5 ▶sub recall_and_update {
Subroutine recall_and_update: called CC=2 SCAR=7.2
Pod documented
132 5 my ($self, $code, $que) = @_;
133  
134 5 my $clean_que = _process_que($que);
135  
136 5 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 5 5
137 0 carp "Undefined que element used in call to recall_and_update().";
138 0 return;
139 }
140  
141 5 my $brain = $self->{brain};
142  
143 # Recall and modify $_
144 5 local $_ = $brain->recall($clean_que);
145 5 my $result = $code->();
146  
147 # Store that value back
148 5 $brain->remember($clean_que, $_);
149  
150 # Return the result
151 5 return $result;
152 }
153  
154  
155 26 ▶sub forget {
Subroutine forget: called CC=2 SCAR=7.6
Pod documented
156 26 my ($self, $que) = @_;
157  
158 26 my $clean_que = _process_que($que);
159  
160 26 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 26 26
161 0 carp "Undefined que element used in call to forget().";
162 0 return;
163 }
164  
165 26 $self->{brain}->forget($clean_que);
166  
167 26 return;
168 }
169  
170  
171 40 ▶sub forget_when {
Subroutine forget_when: called CC=9 SCAR=22.1
Pod documented
172 40 my ($self, $code, $que) = @_;
173  
174 40 my $clean_que = _process_que($que);
175  
176 40 ▶ unless (defined $clean_que) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clean_que) 0 40 40
177 0 carp "Undefined que element used in call to forget_when().";
178 0 return;
179 }
180  
181 40 my $brain = $self->{brain};
182 40 my $fact = $brain->recall($clean_que);
183  
184 40 ▶ if (ref $fact and reftype $fact eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fact and builtin::reftype($fact) eq 'HASH') { } 10 30 40
elsif (ref $fact and builtin::reftype($fact) eq 'ARRAY') { } 10 20 30
Condition100%3/3
ref $fact and builtin::reftype($fact) eq "HASH"
!ll&&!rl&&r
201010
MC/DC100%2/2
ref $fact and builtin::reftype($fact) eq "HASH"
ref $fact builtin::reftype($fact) eq "HASH"
Truth table100%3/3
A and B
A= ref $factB= builtin::reftype($fact) eq "HASH"
ABresult
0X0
100
111
185 10 for my $key (keys %$fact) {
186 30 my $value = $fact->{ $key };
187 30 local $_ = $value;
188 30 ▶ delete $fact->{ $key } if $code->($key, $value);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($key, $value) 15 15 30
189 }
190 }
191  
192 ▶ elsif (ref $fact and reftype $fact eq 'ARRAY') {
Condition66%2/3
ref $fact and builtin::reftype($fact) eq "ARRAY"
!ll&&!rl&&r
20010
MC/DC50%1/2
ref $fact and builtin::reftype($fact) eq "ARRAY"
ref $fact builtin::reftype($fact) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $factB= builtin::reftype($fact) eq "ARRAY"
ABresult
0X0
100
111
193 10 my $index = 0;
194 my @new_fact
195 10 = grep { my $value = $_; not $code->($index++, $value) } @$fact;
196 10 $brain->remember($clean_que, \@new_fact);
197 }
198  
199 else {
200 20 local $_ = $fact;
201 20 ▶ $brain->forget($clean_que) if $code->(undef, $fact);
Branch: 1 branch
BranchTrueFalseTotal
if &$code(undef, $fact) 10 10 20
202 }
203  
204 40 return;
205 }
206  
207  
208 22 ▶sub brain {
Subroutine brain: called CC=1 SCAR=0.0
Pod documented
209 22 my $self = shift;
210 22 return $self->{brain};
211 }
212  
213  
214 11 1;
215  
216 __END__
« Remember/Util.pm ↑ Summary Remember/Memory.pm »