Dependency/Source.pm

filter: Statementstmt 86.2% 44 / 51 Branchbran 55.0% 11 / 20 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 69.0% 76 / 110 CC 17 SCAR 16.9
CC
17
Cov
72%
item
9.8
load
7.7
missing_dependencies
4.0
CRAP 5.4
« Dependency/Ordered.pm ↑ Summary Dependency/Source/File.pm »
1 package Algorithm::Dependency::Source;
2 # ABSTRACT: Implements a source of hierarchy items
3  
4 #pod =pod
5 #pod
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod The Algorithm::Dependency::Source class provides an abstract parent class for
9 #pod implementing sources for the hierarchy data the algorithm will use. For an
10 #pod example of an implementation of this, see
11 #pod L<Algorithm::Dependency::Source::File>, which is bundled with the main
12 #pod L<Algorithm::Dependency> package.
13 #pod
14 #pod =head1 METHODS
15 #pod
16 #pod =cut
17  
18 8 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Algorithm::Dependency ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Params::Util qw{_SET};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 our $VERSION = '1.112';
24  
25  
26 #####################################################################
27 # Constructor
28  
29 #pod =pod
30 #pod
31 #pod =head2 new @arguments
32 #pod
33 #pod Although you cannot directly use the C<new> constructor for
34 #pod C<Algorithm::Dependency::Source>, it will work the same in all subclasses.
35 #pod
36 #pod The constructor takes zero or more subclass specific arguments to define the
37 #pod location of the source of the items, and returns a new object. Although it
38 #pod may check that the arguments you passed are valid, the source will usually
39 #pod NOT actually load the items from the source, instead deferring the loading
40 #pod until you need to use the items.
41 #pod
42 #pod Returns a new object on success, or C<undef> on error.
43 #pod
44 #pod =cut
45  
46 10 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
47 10 my $class = shift;
48  
49 # This can't be created directly, it must be through
50 # a SUPER::new call
51 10 ▶ if ( $class eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 0 10 10
52 0 die "Cannot directly instantiate Algorithm::Dependency::Source."
53 . " You must use a subclass";
54 }
55  
56 # Create the basic object
57 10 my $self = bless {
58 # Has the source been loaded
59 loaded => 0,
60  
61 # Indexes
62 items_hash => undef,
63 items_array => undef,
64 }, $class;
65  
66 10 $self;
67 }
68  
69 #pod =pod
70 #pod
71 #pod =head2 load
72 #pod
73 #pod The C<load> method is the public method used to actually load the items from
74 #pod their storage location into the the source object. The method will
75 #pod automatically called, as needed, in most circumstances. You would generally
76 #pod only want to use C<load> manually if you think there may be some uncertainty
77 #pod that the source will load correctly, and want to check it will work.
78 #pod
79 #pod Returns true if the items are loaded successfully, or C<undef> on error.
80 #pod
81 #pod =cut
82  
83 10 ▶sub load {
Subroutine load: called CC=6 SCAR=20.4
Pod documented
84 10 my $self = shift;
85  
86 # If this is a reload, clean up in preperation
87 10 ▶ if ( $self->{loaded} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"loaded"}) 0 10 10
88 0 $self->{loaded} = 0;
89 0 $self->{items_hash} = undef;
90 0 $self->{items_array} = undef;
91 }
92  
93 # Pass through to the real loader
94 10 my $items = $self->_load_item_list;
95 10 ▶ return $items unless $items;
Branch: 1 branch
BranchTrueFalseTotal
unless $items 0 10 10
96 10 ▶ unless ( _SET($items, 'Algorithm::Dependency::Item') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_SET($items, "Algorithm::Dependency::Item")) 0 10 10
97 0 die( ref($self) . "::_load_item_list did not return an Algorithm::Dependency::Item set" );
98 }
99  
100 # Add the items
101 10 foreach my $item ( @$items ) {
102 # Have we added this one already?
103 100 my $id = $item->id;
104 100 ▶ if ( $self->{items_hash}->{ $id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"items_hash"}{$id}) 0 100 100
105 # Duplicate entry
106 0 return undef;
107 }
108  
109 # Add it
110 100 push @{ $self->{items_array} }, $item;
111 100 $self->{items_hash}->{$id} = $item;
112 }
113  
114 10 $self->{loaded} = 1;
115 }
116  
117 #pod =pod
118 #pod
119 #pod =head2 item $name
120 #pod
121 #pod The C<item> method fetches and returns the item object specified by the
122 #pod name argument.
123 #pod
124 #pod Returns an L<Algorithm::Dependency::Item> object on success, or C<undef> if
125 #pod the named item does not exist in the source.
126 #pod
127 #pod =cut
128  
129 1037 ▶sub item {
Subroutine item: called CC=6 SCAR=22.8
Pod documented
130 1037 my $self = shift;
131 1037 ▶ my $id = (defined $_[0] and ! ref $_[0] and $_[0] ne '') ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) && $_[0] ne '' ? : 1037 0 1037
Condition33%2/6
defined $_[0] and not ref $_[0] and $_[0] ne ""
!ll&&!rl&&r
001037
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
001037
MC/DC0%0/3
defined $_[0] and not ref $_[0] and $_[0] ne ""
defined $_[0] not ref $_[0] $_[0] ne ""
Truth table25%1/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= $_[0] ne ""
ABCresult
0XX0
10X0
1100
1111
132 1037 ▶ $self->{loaded} or $self->load or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"loaded"} or $self->load or return undef 1037 0 1037
Condition33%1/3
$self->{"loaded"} or $self->load
l!l&&r!l&&!r
103700
MC/DC0%0/2
$self->{"loaded"} or $self->load
$self->{"loaded"} $self->load
Truth table33%1/3
A or B
A= $self->{"loaded"}B= $self->load
ABresult
1X1
011
000
133  
134 # Return the item (or undef)
135 1037 $self->{items_hash}->{$id};
136 }
137  
138 #pod =pod
139 #pod
140 #pod =head2 items
141 #pod
142 #pod The C<items> method returns, as a list of objects, all of the items
143 #pod contained in the source. The item objects will be returned in the same order
144 #pod as that in the storage location.
145 #pod
146 #pod Returns a list of L<Algorithm::Dependency::Item> objects on success, or
147 #pod C<undef> on error.
148 #pod
149 #pod =cut
150  
151 14 ▶sub items {
Subroutine items: called CC=3 SCAR=11.3
Pod documented
152 14 my $self = shift;
153 14 ▶ $self->{loaded} or $self->load or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"loaded"} or $self->load or return undef 14 0 14
Condition66%2/3
$self->{"loaded"} or $self->load
l!l&&r!l&&!r
1130
MC/DC0%0/2
$self->{"loaded"} or $self->load
$self->{"loaded"} $self->load
Truth table66%2/3
A or B
A= $self->{"loaded"}B= $self->load
ABresult
1X1
011
000
154 14 @{ $self->{items_array} };
155 }
156  
157 #pod =pod
158 #pod
159 #pod =head2 missing_dependencies
160 #pod
161 #pod By default, we are lenient with missing dependencies if the item is never
162 #pod used. For systems where having a missing dependency can be very bad, the
163 #pod C<missing_dependencies> method checks all Items to make sure their
164 #pod dependencies exist.
165 #pod
166 #pod If there are any missing dependencies, returns a reference to an array of
167 #pod their ids. If there are no missing dependencies, returns 0. Returns
168 #pod C<undef> on error.
169 #pod
170 #pod =cut
171  
172 5 ▶sub missing_dependencies {
Subroutine missing_dependencies: called CC=4 SCAR=14.0
Pod documented
173 5 my $self = shift;
174 5 ▶ $self->{loaded} or $self->load or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"loaded"} or $self->load or return undef 5 0 5
Condition66%2/3
$self->{"loaded"} or $self->load
l!l&&r!l&&!r
410
MC/DC0%0/2
$self->{"loaded"} or $self->load
$self->{"loaded"} $self->load
Truth table66%2/3
A or B
A= $self->{"loaded"}B= $self->load
ABresult
1X1
011
000
175
176 # Merged the depends of all the items, and see if
177 # any are missing.
178 4 my %missing = map { $_ => 1 }
179 34 grep { ! $self->item($_) }
180 5 map { $_->depends }
181 $self->items;
182 5 ▶ %missing ? [ sort keys %missing ] : 0;
Branch: 1 branch
BranchTrueFalseTotal
%missing ? : 2 3 5
183 }
184  
185  
186  
187  
188  
189 #####################################################################
190 # Catch unimplemented methods in subclasses
191  
192 0 ▶sub _load_item_list {
Subroutine _load_item_list: not called CC=1 SCAR=6.9
193 0 die "Class $_[0] failed to define the method _load_item_list";
194 }
195  
196 8 1;
197  
198 __END__
« Dependency/Ordered.pm ↑ Summary Dependency/Source/File.pm »