blib/lib/Graphics/Primitive/ComponentList.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 70.8% 17 / 24 Conditioncond 58.3% 7 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 85.5% 112 / 131 SCAR 29.4
CC
18
Cov
86%
remove_component
7.3
find_component
6.7
find
6.0
CRAP 18.9
« lib/Graphics/Primitive/Driver.pm ↑ Summary blib/lib/Graphics/Primitive/Brush.pm »
1 package Graphics::Primitive::ComponentList;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use MooseX::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 with Storage (format => 'JSON', io => 'File');
6  
7 has 'components' => (
8 traits => ['Array'],
9 is => 'rw',
10 isa => 'ArrayRef',
11 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 4 handles => {
13 'component_count' => 'count',
14 'get_component' => 'get',
15 'push_components' => 'push',
16 'set_component' => 'set',
17 },
18 );
19  
20 has 'constraints' => (
21 traits => ['Array'],
22 is => 'rw',
23 isa => 'ArrayRef',
24 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 4 handles => {
26 'constraint_count' => 'count',
27 'get_constraint' => 'get',
28 'push_constraints' => 'push',
29 'set_constraint' => 'set',
30 },
31 );
32  
33  
34 sub add_component {
35 22 ▶ my ($self, $component, $constraint) = @_;
Subroutine add_component: called CC=1 SCAR=0.0
Pod documented
36  
37 22 push(@{ $self->components }, $component);
38 22 push(@{ $self->constraints }, $constraint);
39 }
40  
41 sub clear {
42 1 ▶ my ($self) = @_;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
43  
44 1 $self->components([]);
45 1 $self->constraints([]);
46 }
47  
48 sub each {
49 2 ▶ my ($self, $functor) = @_;
Subroutine each: called CC=3 SCAR=11.0
Pod documented
50  
51 2 ▶ for(my $i = 0; $i < scalar(@{ $self->components }); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->components;}) 4 2 6
52 4 my $component = $self->get_component($i);
53 4 my $constraint = $self->get_constraint($i);
54  
55 4 ▶ next unless defined($component);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $component 0 4 4
56  
57 4 $functor->($component, $constraint);
58 }
59 }
60  
61 sub find {
62 5 ▶ my ($self, $predicate) = @_;
Subroutine find: called CC=6 SCAR=17.9
Pod documented
63  
64 5 my $newlist = Graphics::Primitive::ComponentList->new;
65 5 ▶ for(my $i = 0; $i < scalar(@{ $self->components }); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->components;}) 14 5 19
66  
67 14 my $component = $self->get_component($i);
68 14 my $constraint = $self->get_constraint($i);
69  
70 14 ▶ next unless defined($component);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $component 0 14 14
71  
72 14 ▶ if($component->can('component_list')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component->can("component_list")) 2 12 14
73 2 my $list = $component->find($predicate);
74  
75 2 ▶ next unless(scalar(@{ $list->components }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$list->components;} 0 2 2
76  
77 2 $newlist->push_components(@{ $list->components });
78 2 $newlist->push_constraints(@{ $list->constraints });
79 }
80  
81 14 ▶ if($predicate->($component, $constraint)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$predicate($component, $constraint)) 6 8 14
82 6 $newlist->add_component($component, $constraint);
83 }
84 }
85  
86 5 return $newlist;
87 }
88  
89 sub find_component {
90 2 ▶ my ($self, $name) = @_;
Subroutine find_component: called CC=5 SCAR=19.1
Pod documented
91  
92 2 ▶ for(my $i = 0; $i <= scalar(@{ $self->components }); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= scalar @{$self->components;}) 2 0 2
93 2 my $comp = $self->get_component($i);
94  
95 2 ▶ if(defined($comp) && defined($comp->name) && $comp->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp and defined $comp->name and $comp->name eq $name) 2 0 2
Condition33%2/6
defined $comp and defined $comp->name and $comp->name eq $name
!ll&&!rl&&r
002
defined $comp and defined $comp->name
!ll&&!rl&&r
002
MC/DC0%0/3
defined $comp and defined $comp->name and $comp->name eq $name
defined $comp defined $comp->name $comp->name eq $name
Truth table25%1/4
A and B and C
A= defined $compB= defined $comp->nameC= $comp->name eq $name
ABCresult
0XX0
10X0
1100
1111
96  
97 2 return $i;
98 }
99 }
100  
101 0 return undef;
102 }
103  
104 sub remove_component {
105 1 ▶ my ($self, $component) = @_;
Subroutine remove_component: called CC=7 SCAR=19.8
Pod documented
106  
107 1 my $name;
108  
109 # Handle either a component object or a scalar name
110 1 ▶ if(ref($component)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $component) { } 1 0 1
111 1 ▶ if($component->can('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component->can('name')) { } 1 0 1
112 1 $name = $component->name();
113 } else {
114 0 die('Must supply a Component or a scalar name.');
115 }
116 } else {
117 0 $name = $component;
118 }
119  
120 1 my $count = 0;
121 1 my @dels = ();
122 1 foreach my $comp (@{ $self->components }) {
123  
124 3 ▶ if(defined($comp) && defined($comp->name) && $comp->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp and defined $comp->name and $comp->name eq $name) 1 2 3
Condition83%5/6
defined $comp and defined $comp->name and $comp->name eq $name
!ll&&!rl&&r
111
defined $comp and defined $comp->name
!ll&&!rl&&r
012
MC/DC66%2/3
defined $comp and defined $comp->name and $comp->name eq $name
defined $comp defined $comp->name $comp->name eq $name
Truth table75%3/4
A and B and C
A= defined $compB= defined $comp->nameC= $comp->name eq $name
ABCresult
0XX0
10X0
1100
1111
125  
126 1 push(@dels, $self->components->[$count]);
127 1 delete($self->components->[$count]);
128 1 delete($self->constraints->[$count]);
129 }
130 3 $count++;
131 }
132  
133 1 return \@dels;
134 }
135  
136 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 4 1;
138  
139 =head1 NAME
140  
141 Graphics::Primitive::ComponentList - List of Components
142  
143 =head1 DESCRIPTION
144  
145 Maintains a list of components and their constraints. This is implemented
146 as a class to provide functionality above and beyond a simple array.
147  
148 =head1 SYNOPSIS
149  
150 my $c = Graphics::Primitive::ComponentList->new;
151 $c->add_component($comp, $constraint);
152  
153 my $cindex = $c->find_component($comp->name);
154  
155 =head1 METHODS
156  
157 =head2 Constructor
158  
159 =over 4
160  
161 =item I<new>
162  
163 Creates a new Container.
164  
165 =back
166  
167 =head2 Instance Methods
168  
169 =over 4
170  
171 =item I<add_component ($component, $constraint)>
172  
173 Add a component to the list. Returns a true value if the component
174 was added successfully. A second argument may be required, please consult the
175 POD for your specific layout manager implementation.
176  
177 Before the component is added, it is passed to the validate_component method.
178 If validate_component does not return a true value, then the component is not
179 added.
180  
181 =item I<clear>
182  
183 Reset components and constraints to empty arrayrefs.
184  
185 =item I<component_count>
186  
187 Returns the number of components in this list.
188  
189 =item I<constraint_count>
190  
191 Returns the number of constraints in this list.
192  
193 =item I<each>
194  
195 Calls the supplied CODEREF for each component in this list, passing the
196 component and it's constraints as arguments.
197  
198 my $flist = $list->each(
199 sub{
200 my ($component, $constraint) = @_; $comp->class('foo)
201 }
202 );
203  
204  
205 =item I<find>
206  
207 Returns a new ComponentList containing only the components for which the
208 supplied CODEREF returns true. The coderef is called for each component and
209 is passed the component and it's constraints. Undefined components (the ones
210 left around after a remove_component) are automatically skipped.
211  
212 my $flist = $list->find(
213 sub{
214 my ($component, $constraint) = @_; return $comp->class eq 'foo'
215 }
216 );
217  
218 If no matching components are found then a new list is returned so that simple
219 calls liked $container->find(...)->each(...) don't explode.
220  
221 =item I<find_component>
222  
223 Returns the index of the first component with the supplied name. Returns
224 undef if no component with that name is found.
225  
226 =item I<get_component>
227  
228 Get the component at the specified index.
229  
230 =item I<get_constraint>
231  
232 Get the constraint at the specified index.
233  
234 =item I<remove_component>
235  
236 Removes a component and it's constraint. B<Components must have names to be
237 removed.> Returns an arrayref of Components that were removed.
238  
239 =back
240  
241 =head1 AUTHOR
242  
243 Copyright 2008-2009 by Cory G Watson.
244  
245 =head1 BUGS
246  
247 Please report any bugs or feature requests to C<bug-geometry-primitive at rt.cpan.org>, or through
248 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geometry-Primitive>. I will be notified, and then you'll
249 automatically be notified of progress on your bug as I make changes.
250  
251 =head1 COPYRIGHT & LICENSE
252  
253 Copyright 2008-2009 by Cory G Watson.
254  
255 This program is free software; you can redistribute it and/or modify it
256 under the same terms as Perl itself.
« lib/Graphics/Primitive/Driver.pm ↑ Summary blib/lib/Graphics/Primitive/Brush.pm »