3D/Template.pm

filter: Statementstmt 82.3% 107 / 130 Branchbran 63.2% 43 / 68 Conditioncond 42.8% 6 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 92.8% 13 / 14 Podpod 91.6% 11 / 12 totaltotal 73.8% 181 / 245 CC 46 SCAR 26.3
CC
46
Cov
74%
from_string
27.4
as_string
16.5
init_thing
15.5
CRAP 13.9
↑ Summary 3D/Link.pm »
1  
2 # Template - describe an object class and it's keys/settings
3  
4 package Games::3D::Template;
5  
6 # (C) by Tels <http://bloodgate.com/>
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 require Exporter;
11 3 ▶use vars qw/@ISA $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 @ISA = qw/Exporter/;
13  
14 3 $VERSION = '0.02';
15  
16 ##############################################################################
17 # protected vars
18  
19 # Templates have their own unique IDs
20  
21 {
22 3 my $id = 1;
23 12 ▶ sub ID { return $id++;}
Subroutine ID: called CC=1 SCAR=0.0
Pod undocumented
24 }
25  
26 ##############################################################################
27 # methods
28  
29 sub new
30 3 ▶ {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
31 # create a new Template
32 12 my $class = shift;
33  
34 12 my $args = $_[0];
35 12 ▶ $args = { @_ } unless ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 12 0 12
36  
37 12 my $self = { id => ID() };
38 12 bless $self, $class;
39  
40 $self->{valid} = {
41 12 name => 'STR=',
42 id => 'INT=',
43 state => 'INT=0',
44 state_0 => 'ARRAY=1',
45 state_1 => 'ARRAY=1',
46 visible => 'BOOL=false',
47 active => 'BOOL=true',
48 think_time => 'INT=0',
49 next_think => 'INT=0',
50 inputs => 'ARRAY=0',
51 outputs => 'ARRAY=0',
52 state_endtime => 'INT=',
53 state_target => 'INT=',
54 class => 'STR=',
55 info => 'STR=',
56 };
57 12 ▶ $self->{class} = $args->{class} || 'Games::3D::Thingy';
Condition100%2/2
$args->{'class'} || 'Games::3D::Thingy'
l!l
111
MC/DC100%1/1
$args->{'class'} || 'Games::3D::Thingy'
$args->{'class'}
Truth table100%2/2
A
A= $args->{'class'}
Aresult
11
00
58 12 $self;
59 }
60  
61 sub class
62 0 ▶ {
Subroutine class: not called CC=1 SCAR=6.9
Pod documented
63 0 my $self = shift;
64 0 $self->{class};
65 }
66  
67 sub id
68 1 ▶ {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
69 1 my $self = shift;
70  
71 1 $self->{id};
72 }
73  
74 sub create_thing
75 3 ▶ {
Subroutine create_thing: called CC=5 SCAR=16.1
Pod documented
76 # take your own blueprint and create a thing
77 3 my $self = shift;
78  
79 3 ▶ my $base = $self->{base} || 'Games::3D::Thingy';
Condition50%1/2
$self->{'base'} || 'Games::3D::Thingy'
l!l
03
MC/DC0%0/1
$self->{'base'} || 'Games::3D::Thingy'
$self->{'base'}
Truth table50%1/2
A
A= $self->{'base'}
Aresult
11
00
80
81 3 ▶ if (exists $self->{valid}->{base})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"valid"}{"base"}) 1 2 3
82 {
83 1 $base = $self->{valid}->{base};
84 }
85  
86 3 my $base_pm = $base; $base_pm =~ s/::/\//g; $base_pm .= '.pm';
87 3 require $base_pm;
88 3 my $object = $base->new();
89  
90 # rebless, from 'Games::3D::Thingy' into 'Games::3D::Thingy::Physical...'
91 3 $object->{class} = $self->{class};
92  
93 # Foo::Bar::Baz inherits from Foo::Bar and Foo, so check all of them
94 # TODO: we might just store the inherited stuff as to not always have
95 # to check overriden settings
96 3 my @classes = split /::/, $object->{class};
97  
98 3 ▶ while (@classes > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@classes > 0) 6 3 9
99 {
100 6 my $class = join('::', @classes);
101 6 my $tpl = $self->{_world}->find_template($class);
102 6 ▶ $tpl->init_thing($object) if $tpl;
Branch: 1 branch
BranchTrueFalseTotal
if $tpl 6 0 6
103 6 pop @classes;
104 }
105 3 $object;
106 }
107  
108 sub init_thing
109 7 ▶ {
Subroutine init_thing: called CC=11 SCAR=27.4
Pod documented
110 # init all fields in a thing from the blueprint and return the thing
111 7 my ($self,$thing) = @_;
112  
113 7 foreach my $key (keys %{$self->{valid}})
114 {
115 117 ▶ next if exists $thing->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $thing->{$key} 101 16 117
116 16 my ($type,$default) = split /=/, $self->{valid}->{$key};
117 16 ▶ ($type,$default) = ('STR', $type) unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 3 13 16
118  
119 16 ▶ if ($type eq 'ARRAY')
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 16 16
elsif ($type eq 'BOOL') { } 0 16 16
elsif ($type eq 'CODE') { } 0 16 16
elsif ($type eq 'SIG') { } 0 16 16
elsif ($type eq 'FRACT') { } 8 8 16
120 {
121 0 $thing->{$key} = [ split /,/, $default ];
122 }
123 elsif ($type eq 'BOOL')
124 {
125 0 ▶ $thing->{$key} = $default =~ /^(false|off|no)$/i ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$default =~ /^(false|off|no)$/i ? : 0 0 0
126 }
127 elsif ($type eq 'CODE')
128 {
129 0 $thing->{$key} = $default;
130 }
131 elsif ($type eq 'SIG')
132 {
133 0 $thing->{$key} = signal_by_name($default);
134 }
135 elsif ($type eq 'FRACT')
136 {
137 8 $thing->{$key} = abs($default);
138 8 ▶ $thing->{$key} = 1 if $thing->{$key} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $thing->{$key} > 1 0 8 8
139 }
140 else
141 {
142 8 $thing->{$key} = $default;
143 }
144 }
145 7 $thing;
146 }
147  
148 sub validate
149 4 ▶ {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
150 # check whether a given key is allowed, and whether his data confirms to the
151 # blueprint
152 4 my $self = shift;
153 4 my $obj = shift;
154  
155 4 my $class = $self->{class};
156  
157 return
158 4 ▶ "Object class '". ref($obj)."' does not match template class '".$class."'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq ref $obj 1 3 4
159 unless $class eq ref($obj);
160  
161 3 ▶ return $self->validate_key($obj, $_[0]) if (@_ > 0);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 0 0 3 3
162  
163 3 foreach my $key (keys %$obj)
164 {
165 43 ▶ next if $key =~ /^_/; # skip internals
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^_/ 3 40 43
166 40 my $rc = $self->validate_key($obj, $key);
167 40 ▶ return $rc if defined $rc; # error?
Branch: 1 branch
BranchTrueFalseTotal
if defined $rc 1 39 40
168 }
169 2 return; # okay
170 }
171  
172 sub validate_key
173 40 ▶ {
Subroutine validate_key: called CC=3 SCAR=11.1
Pod documented
174 40 my ($self,$obj,$key) = @_;
175
176 return "Invalid key '$key' on object " . ref($obj) . " #" . ($obj->{id}||-1)
177 40 ▶ unless exists $self->{valid}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"valid"}{$key} 1 39 40
Condition50%1/2
$obj->{'id'} || -1
l!l
10
MC/DC0%0/1
$obj->{'id'} || -1
$obj->{'id'}
Truth table50%1/2
A
A= $obj->{'id'}
Aresult
11
00
178  
179 39 return; # okay
180 }
181  
182 sub from_string
183 2 ▶ {
Subroutine from_string: called CC=15 SCAR=33.1
Pod documented
184 2 my ($str) = shift;
185  
186 2 my @lines = split /\n/, $str;
187
188 2 my ($name,@objects,$line);
189 2 my $linenr = 0;
190 2 ▶ while (@lines > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@lines > 0) 16 2 18
191 {
192 16 $line = shift @lines; $linenr++;
193 16 ▶ next if $line =~ /^\s*(#|$)/; # skip comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(#|$)/ 6 10 16
194  
195 10 ▶ return ("Invalid format in string: '$line' in line $linenr")
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*([\w:]+)\s*\{/ 0 10 10
196 unless ($line =~ /\s*([\w:]+)\s*\{/); # declaration: Class {
197  
198 10 my $class = $1;
199 10 ▶ return "Undefined class in line $linenr" if ($class || '') eq '';
Branch: 1 branch
BranchTrueFalseTotal
if ($class || "") eq "" 0 10 10
Condition50%1/2
$class || ''
l!l
100
MC/DC0%0/1
$class || ''
$class
Truth table50%1/2
A
A= $class
Aresult
11
00
200  
201 10 my $self = __PACKAGE__->new(); # emulate ->new();
202 10 $self->{class} = $class;
203  
204 10 $line = shift @lines; $linenr++;
205 10 my $s = $self->{valid};
206  
207 10 ▶ while ($line !~ /^\s*\}/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*\}/) 37 10 47
208 {
209 37 ▶ if ( $line =~ m/\s*([\w-]+)\s*=>?\s*\{\s*$/) # "hash => {"
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s*([\w-]+)\s*=>?\s*\{\s*$/) { } 0 37 37
210 {
211 0 ▶ $name = $1 || return ("Empty hash name in line $linenr\n");
Condition0%0/2
$1 || (return "Empty hash name in line $linenr\n")
l!l
00
MC/DC0%0/1
$1 || (return "Empty hash name in line $linenr\n")
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
212 0 $s->{$name} = {};
213 0 $line = shift @lines; $linenr++;
214 0 ▶ while ($line !~ /^\s*\}/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*\}/) 0 0 0
215 {
216 # var => val, var = val
217 0 ▶ return "Invalid line format in line $linenr\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*([\w-]+)\s*=>?\s*(['\"])?(.*?)\2?\s*$/ 0 0 0
218 $line =~
219 m/\s*([\w-]+)\s*=>?\s*(['\"])?(.*?)\2?\s*$/;
220 0 ▶ my $n = $1 || return ("Empty name in line $linenr\n");
Condition0%0/2
$1 || (return "Empty name in line $linenr\n")
l!l
00
MC/DC0%0/1
$1 || (return "Empty name in line $linenr\n")
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
221 # return ("Field '$n' already defined in hash '$name' in '$class' in line $linenr")
222 # if exists $s->{$name}->{$n};
223 0 $s->{$name}->{$n} = $3;
224 0 $line = shift @lines; $linenr++;
225 }
226 }
227 else
228 {
229 37 ▶ return "Invalid line format in line $linenr\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*([\w-]+)\s*=>?\s*(['\"])?(.*?)\2?\s*$/ 0 37 37
230 $line =~
231 m/\s*([\w-]+)\s*=>?\s*(['\"])?(.*?)\2?\s*$/; # var => val, var = val
232 37 ▶ $name = $1 || return ("Empty name in line $linenr\n");
Condition50%1/2
$1 || (return "Empty name in line $linenr\n")
l!l
370
MC/DC0%0/1
$1 || (return "Empty name in line $linenr\n")
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
233 # return ("Field '$name' already defined in '$class' in line $linenr")
234 # if exists $s->{$name};
235 37 $s->{$name} = $3;
236 37 $line = shift @lines; $linenr++;
237 }
238 }
239 # one object done
240 10 push @objects, $self;
241 }
242 2 ▶ wantarray ? @objects : $objects[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
243 }
244  
245 sub as_string
246 4 ▶ {
Subroutine as_string: called CC=9 SCAR=28.0
Pod documented
247 4 my $self = shift;
248  
249 4 my $txt = $self->{class} . " {\n";
250 4 my $s = $self->{valid};
251 4 foreach my $k (sort keys %$s)
252 {
253 66 ▶ next if $k =~ /^_/; # skip internal keys
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^_/ 0 66 66
254 66 my $v = $s->{$k}; # get key
255 66 ▶ next if !defined $v; # skip empty
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 66 66
256 66 ▶ if (ref($v) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 0 66 66
257 {
258 0 $v = "{\n";
259 0 foreach my $key (sort keys %{$s->{$k}})
260 {
261 0 my $vi = $s->{$k}->{$key};
262 0 ▶ $vi = $vi->as_string() if ref($v);
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 0 0
263 0 $v .= " $key = $vi\n";
264 }
265 0 $v .= " }";
266 }
267 else
268 {
269 66 ▶ $v = '"'.$v.'"' if $v =~ /[^a-zA-Z0-9_\.,'"+-=]/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /[^a-zA-Z0-9_\.,'"+-=]/ 0 66 66
270 66 ▶ next if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 0 66 66
271 }
272 66 $txt .= " $k = $v\n";
273 }
274 4 $txt .= "}\n";
275 }
276  
277 sub add_key
278 2 ▶ {
Subroutine add_key: called CC=1 SCAR=0.0
Pod documented
279 2 my ($self,$key,$data) = @_;
280  
281 2 $self->{valid}->{$key} = $data;
282 2 $self;
283 }
284  
285 sub keys
286 3 ▶ {
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
287 3 my ($self) = @_;
288  
289 3 scalar keys %{$self->{valid}};
290 }
291  
292 3 1;
293  
294 __END__
↑ Summary 3D/Link.pm »