DataObjects/Directives.pm

filter: Statementstmt 57.8% 66 / 114 Branchbran 29.4% 10 / 34 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 58.3% 14 / 24 Podpod 61.5% 8 / 13 totaltotal 49.0% 100 / 204 CC 25 SCAR 18.7
CC
25
Cov
49%
set_directive_at_idx
20.0
get_directive_at_idx
12.0
delete_directive_at_idx
12.0
CRAP 6.5
« DataObjects/Game.pm ↑ Summary DataObjects.pm »
1 #===============================================================================
2 #
3 # FILE: Games::Go::AGA::DataObjects::Directives.pm
4 #
5 # USAGE: use Games::Go::AGA::DataObjects::Directives;
6 #
7 # PODNAME: Games::Go::AGA::DataObjects::Directives
8 # ABSTRACT: model directives information from an AGA register.tde file
9 #
10 # AUTHOR: Reid Augustin (REID), <reid@hellosix.com>
11 # CREATED: 11/19/2010 03:13:05 PM PST
12 #===============================================================================
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 package Games::Go::AGA::DataObjects::Directives;
18 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use Games::Go::AGA::DataObjects::Types qw( is_Rank is_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Games::Go::AGA::Parse::Util qw( Rank_to_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Games::Go::AGA::DataObjects::Types qw( isa_CodeRef isa_HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 our $VERSION = '0.152'; # VERSION
26  
27 has booleans => (
28 isa => \&isa_HashRef,
29 is => 'lazy',
30 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 {
32 1 FORCE_ROUND_ROBIN => 1,
33 NO_ROUND_ROBIN => 1,
34 MCMAHON => 1,
35 AGA_RATED => 1,
36 TEST => 1,
37 }
38 },
39 3 );
40 has change_callback => (
41 isa => \&isa_CodeRef,
42 is => 'rw',
43 lazy => 1,
44 0 ▶ default => sub { sub { } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 3 );
46  
47 3 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
48 3 my ($self) = @_;
49 3 $self->{keys} = []; # empty arrays
50 3 $self->{values} = [];
51 }
52  
53 0 ▶sub changed {
Subroutine changed: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my ($self) = @_;
55  
56 0 &{$self->change_callback}(@_);
57 }
58  
59 0 ▶sub directives {
Subroutine directives: not called CC=2 SCAR=17.9
Pod documented
60 0 my ($self) = @_;
61  
62 return wantarray
63 0 ? @{$self->{keys}}
64 0 ▶ : scalar @{$self->{keys}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
65 }
66  
67 19 ▶sub is_boolean {
Subroutine is_boolean: called CC=1 SCAR=0.0
Pod undocumented
68 19 my ($self, $key) = @_;
69  
70 19 return $self->booleans->{uc $key};
71 }
72  
73 0 ▶sub get_directive_at_idx {
Subroutine get_directive_at_idx: not called CC=3 SCAR=24.8
Pod documented
74 0 my ($self, $idx) = @_;
75  
76 croak("$idx out of range") if (($idx < 0) or
77 0 ▶ ($idx >= @{$self->{keys}}));
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 or $idx >= @{$self->{"keys"};} 0 0 0
Condition0%0/3
$idx < 0 or $idx >= @{$self->{"keys"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx < 0 or $idx >= @{$self->{"keys"};}
$idx < 0 $idx >= @{$self->{"keys"};}
Truth table0%0/3
A or B
A= $idx < 0B= $idx >= @{$self->{"keys"};}
ABresult
1X1
011
000
78 0 return ($self->{keys}[$idx], $self->{values}[$idx]);
79 }
80  
81 0 ▶sub set_directive_at_idx {
Subroutine set_directive_at_idx: not called CC=4 SCAR=30.0
Pod documented
82 0 my ($self, $idx, $key, $value) = @_;
83  
84 croak("$idx out of range") if (($idx < 0) or
85 0 ▶ ($idx >= @{$self->{keys}}));
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 or $idx >= @{$self->{"keys"};} 0 0 0
Condition0%0/3
$idx < 0 or $idx >= @{$self->{"keys"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx < 0 or $idx >= @{$self->{"keys"};}
$idx < 0 $idx >= @{$self->{"keys"};}
Truth table0%0/3
A or B
A= $idx < 0B= $idx >= @{$self->{"keys"};}
ABresult
1X1
011
000
86 0 ▶ $value = $self->_munge_BAND_BREAKS($value) if (uc $key eq 'BAND_BREAKS');
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq "BAND_BREAKS" 0 0 0
87 0 $self->{keys}[$idx] = $key;
88 0 $self->{values}[$idx] = $value;
89 0 $self->changed;
90 0 return $self;
91 }
92  
93 0 ▶sub delete_directive_at_idx {
Subroutine delete_directive_at_idx: not called CC=3 SCAR=24.8
Pod documented
94 0 my ($self, $idx) = @_;
95  
96 0 ▶ $idx = 0 if ($idx < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 0 0
97 0 ▶ $idx = $#{$self->{keys}} if ($idx > $#{$self->{keys}});
Branch: 1 branch
BranchTrueFalseTotal
if $idx > $#{$$self{"keys"};} 0 0 0
98  
99 0 splice(@{$self->{keys}} , $idx, 1);
100 0 splice(@{$self->{values}}, $idx, 1);
101 0 $self->changed;
102 0 return $self;
103 }
104  
105 1 ▶sub _munge_BAND_BREAKS {
Subroutine _munge_BAND_BREAKS: called CC=4 SCAR=14.3
106 1 my ($self, $value) = @_;
107  
108 1 ▶ return '' if ($value eq ''); #special case to mark empty breaks
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 1 1
109 return join ' ', # read from the bottom up:
110 1 sort { $b <=> $a } # sort stronger bands higher
111 2 grep { $_ } # defined and truthy
112 2 ▶ map { is_Rank($_) # is it a valid Rank?
Branch: 2 branches
BranchTrueFalseTotal
is_Rank($_) ? : 0 2 2
is_Rating($_) ? : 2 0 2
113 ? int Rank_to_Rating($_) # if so, convert to a Rating and integerize
114 : is_Rating($_) # is it a Rating?
115 ? $_ # pass through untouched
116 : undef } # filter out everything else
117 1 grep { $_ } # defined and truthy
118 split(/[^\ddDkK\.\-]+/, $value); # split into ranks/ratings
119 }
120  
121 5 ▶sub insert_directive_above {
Subroutine insert_directive_above: called CC=5 SCAR=17.1
Pod documented
122 5 my ($self, $idx, $key, $value) = @_;
123  
124 5 ▶ if (not defined $idx or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $idx or $idx < 0 or $idx >= @{$self->{"keys"};}) 5 0 5
Condition33%2/6
not defined $idx or $idx < 0 or $idx >= @{$self->{"keys"};}
l!l&&r!l&&!r
500
not defined $idx or $idx < 0
l!l&&r!l&&!r
050
MC/DC0%0/3
not defined $idx or $idx < 0 or $idx >= @{$self->{"keys"};}
not defined $idx $idx < 0 $idx >= @{$self->{"keys"};}
Truth table25%1/4
not(A) or B or C
A= not defined $idxB= $idx < 0C= $idx >= @{$self->{"keys"};}
ABCresult
1XX1
01X1
0011
0000
125 $idx < 0 or
126 0 $idx >= @{$self->{keys}}) {
127 5 $idx = @{$self->{keys}}; # add to end
128 }
129  
130 5 ▶ $value = $self->_munge_BAND_BREAKS($value) if (uc $key eq 'BAND_BREAKS');
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq "BAND_BREAKS" 1 4 5
131 5 splice(@{$self->{keys}} , $idx, 0, $key);
132 5 splice(@{$self->{values}}, $idx, 0, $value);
133 5 $self->changed;
134 5 return $self;
135 }
136  
137 0 ▶sub delete_directive {
Subroutine delete_directive: not called CC=3 SCAR=24.8
Pod documented
138 0 my ($self, $key) = @_;
139  
140 0 my $keys = $self->{keys};
141  
142 0 ▶ for (my $ii = $#{$keys}; $ii >= 0; $ii--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii >= 0) 0 0 0
143 0 ▶ if (uc $key eq uc $keys->[$ii]) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $key eq uc $keys->[$ii]) 0 0 0
144 0 $self->delete_directive_at_idx($ii);
145 0 $self->changed;
146 0 last;
147 }
148 }
149 0 return $self;
150 }
151  
152 0 ▶sub get_directive_values { # for backwards compatibility
Subroutine get_directive_values: not called CC=1 SCAR=6.9
Pod undocumented
153 0 shift->get_directive_value(@_);
154 }
155  
156 20 ▶sub get_directive_value {
Subroutine get_directive_value: called CC=4 SCAR=13.9
Pod documented
157 20 my ($self, $key) = @_;
158  
159 20 my $keys = $self->{keys};
160 20 $key = uc ($key);
161 20 foreach my $ii (0 .. $#{$keys}) {
162 98 ▶ if (uc($keys->[$ii]) eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $keys->[$ii] eq $key) 19 79 98
163 19 my $val = $self->{values}[$ii];
164 19 ▶ $val = 1 if ($self->is_boolean($key)); # booleans (key but no defined value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_boolean($key) 0 19 19
165 19 return $val;
166 }
167 }
168 1 return; # undef
169 }
170  
171 5 ▶sub set_directive_value {
Subroutine set_directive_value: called CC=4 SCAR=15.9
Pod documented
172 5 my ($self, $key, $val) = @_;
173  
174 5 my $keys = $self->{keys};
175 5 my $uc_key = uc ($key);
176 5 foreach my $ii (0 .. $#{$keys}) {
177 10 ▶ if (uc($keys->[$ii]) eq $uc_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $keys->[$ii] eq $uc_key) 0 10 10
178 0 ▶ $val = '' if ($self->directive_is_boolean($key)); # presence is sufficient
Branch: 1 branch
BranchTrueFalseTotal
if $self->directive_is_boolean($key) 0 0 0
179 0 return $self->set_directive_at_idx ($ii, $key, $val);
180 }
181 }
182 # not found? add to the end
183 5 return $self->insert_directive_above (-1, $key, $val);
184 }
185  
186 0 ▶sub fprint {
Subroutine fprint: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my ($self, $fh) = @_;
188  
189 0 for my $ii (0 .. $#{$self->{keys}}) {
190 0 $fh->print("## $self->{keys}[$ii] $self->{values}[$ii]\n");
191 }
192 }
193  
194 3 1;
195  
196 __END__
« DataObjects/Game.pm ↑ Summary DataObjects.pm »