File Coverage

blib/lib/Algorithm/Dependency/Source.pm
Criterion Covered Total %
statement 44 51 86.3
branch 11 20 55.0
condition 7 15 46.7
subroutine 10 11 90.9
pod 5 5 100.0
total 77 102 75.5


line stmt bran cond sub pod time code
1             package Algorithm::Dependency::Source;
2              
3             =pod
4            
5             =head1 NAME
6            
7             Algorithm::Dependency::Source - Implements a source of heirachy items
8            
9             =head1 DESCRIPTION
10            
11             The Algorithm::Dependency::Source class provides an abstract parent class for
12             implementing sources for the heirachy data the algorithm will use. For an
13             example of an implementation of this, see
14             L<Algorithm::Dependency::Source::File>, which is bundled with the main
15             L<Algorithm::Dependency> package.
16            
17             =head1 METHODS
18            
19             =cut
20              
21 8     8   109 use strict;
  8         100  
  8         121  
22 8     8   185 use Algorithm::Dependency ();
  8         75  
  8         76  
23 8     8   362 use Params::Util qw{_SET};
  8         211  
  8         266  
24              
25 8     8   176 use vars qw{$VERSION};
  8         73  
  8         112  
26             BEGIN {
27 8     8   148 $VERSION = '1.102';
28             }
29              
30              
31              
32              
33              
34             #####################################################################
35             # Constructor
36              
37             =pod
38            
39             =head2 new @arguments
40            
41             Although you cannot directly use the C<new> constructor for
42             C<Algorithm::Dependency::Source>, it will work the same in all subclasses.
43            
44             The constructor takes zero or more subclass specific arguments to define the
45             location of the source of the items, and returns a new object. Alrough it
46             may check that the arguments you passed are valid, the source will usually
47             NOT actually load the items from the source, instead defering the loading
48             until you need to use the items.
49            
50             Returns a new object on success, or C<undef> on error.
51            
52             =cut
53              
54             sub new {
55 10     10 1 130 my $class = shift;
56              
57             # This can't be created directly, it must be through
58             # a SUPER::new call
59 10 50       114 if ( $class eq __PACKAGE__ ) {
60 0         0 die "Cannot directly instantiate Algorithm::Dependency::Source."
61             . " You must use a subclass";
62             }
63              
64             # Create the basic object
65 10         150 my $self = bless {
66             # Has the source been loaded
67             loaded      => 0,
68              
69             # Indexes
70             items_hash  => undef,
71             items_array => undef,
72             }, $class;
73              
74 10         141 $self;
75             }
76              
77             =pod
78            
79             =head2 load
80            
81             The C<load> method is the public method used to actually load the items from
82             their storage location into the the source object. The method will
83             automatically called, as needed, in most circumstances. You would generally
84             only want to use C<load> manually if you think there may be some uncertainty
85             that the source will load correctly, and want to check it will work.
86            
87             Returns true if the items are loaded successfully, or C<undef> on error.
88            
89             =cut
90              
91             sub load {
92 10     10 1 97 my $self = shift;
93              
94             # If this is a reload, clean up in preperation
95 10 50       147 if ( $self->{loaded} ) {
96 0         0 $self->{loaded}      = 0;
97 0         0 $self->{items_hash}  = undef;
98 0         0 $self->{items_array} = undef;
99             }
100              
101             # Pass through to the real loader
102 10         188 my $items = $self->_load_item_list;
103 10 50       181 return $items unless $items;
104 10 50       170 unless ( _SET($items, 'Algorithm::Dependency::Item') ) {
105 0         0 die( ref($self) . "::_load_item_list did not return an Algorithm::Dependency::Item set" );
106             }
107              
108             # Add the items
109 10         1133 foreach my $item ( @$items ) {
110             # Have we added this one already?
111 100         2095 my $id = $item->id;
112 100 50       1123 if ( $self->{items_hash}->{ $id } ) {
113             # Duplicate entry
114 0         0 return undef;
115             }
116              
117             # Add it
118 100         2907 push @{ $self->{items_array} }, $item;
  100         1088  
119 100         1293 $self->{items_hash}->{$id} = $item;
120             }
121              
122 10         195 $self->{loaded} = 1;
123             }
124              
125             =pod
126            
127             =head2 item $name
128            
129             The C<item> method fetches and returns the item object specified by the
130             name argument.
131            
132             Returns an L<Algorithm::Dependency::Item> object on success, or C<undef> if
133             the named item does not exist in the source.
134            
135             =cut
136              
137             sub item {
138 1037     1037 1 14514 my $self = shift;
139 1037 50 33     20620 my $id = (defined $_[0] and ! ref $_[0] and $_[0] ne '') ? shift : return undef;
      33        
140 1037 50 33     19760 $self->{loaded} or $self->load or return undef;
141              
142             # Return the item (or undef)
143 1037         19570 $self->{items_hash}->{$id};
144             }
145              
146             =pod
147            
148             =head2 items
149            
150             The C<items> method returns, as a list of objects, all of the items
151             contained in the source. The item objects will be returned in the same order
152             as that in the storage location.
153            
154             Returns a list of L<Algorithm::Dependency::Item> objects on success, or
155             C<undef> on error.
156            
157             =cut
158              
159             sub items {
160 14     14 1 137 my $self = shift;
161 14 50 66     187 $self->{loaded} or $self->load or return undef;
162 14         120 @{ $self->{items_array} };
  14         273  
163             }
164              
165             =pod
166            
167             =head2 missing_dependencies
168            
169             By default, we are leniant with missing dependencies if the item is neved
170             used. For systems where having a missing dependency can be very bad, the
171             C<missing_dependencies> method checks all Items to make sure their
172             dependencies exist.
173            
174             If there are any missing dependencies, returns a reference to an array of
175             their ids. If there are no missing dependencies, returns 0. Returns
176             C<undef> on error.
177            
178             =cut
179              
180             sub missing_dependencies {
181 5     5 1 51 my $self = shift;
182 5 50 66     112 $self->{loaded} or $self->load or return undef;
183            
184             # Merged the depends of all the items, and see if
185             # any are missing.
186 4         49 my %missing = map { $_ => 1 }
  34         326  
187 40         484 grep { ! $self->item($_) }
188 5         74 map  { $_->depends }
189             $self->items;
190 5 100       272 %missing ? [ sort keys %missing ] : 0;
191             }
192              
193              
194              
195              
196              
197             #####################################################################
198             # Catch unimplemented methods in subclasses
199              
200             sub _load_item_list {
201 0     0     die "Class $_[0] failed to define the method _load_item_list";
202             }
203              
204             1;
205              
206             =pod
207            
208             =head1 EXTENDING
209            
210             C<Algorithm::Dependency::Source> itself is a fairly thin module, and it
211             is intended that you will probably need to extend it to be able to
212             extract item data from whatever location you have stored them.
213            
214             This is usually a fairly simple two step process.
215            
216             =over 4
217            
218             =item Overload the C<new> method.
219            
220             Assuming your subclass takes some form or argument on creation, you will
221             need to overload the C<new> method to accept the arguments, validate them,
222             and store them in the source object.
223            
224             =item Define the method C<_load_item_list>.
225            
226             Leaving our parent's C<load> method to take care of conflict, errors, and
227             whatever, the C<_load_item_list> method is used to simply create a list of
228             L<Algorithm::Dependency::Item> objects from wherever you store the item,
229             and return them as a list.
230            
231             =back
232            
233             Having completed these two things, your subclass should be completed. For
234             an example of the code, have a look at the source for the simple subclass
235             L<Algorithm::Dependency::Source::File>.
236            
237             =head1 SUPPORT
238            
239             For general comments, contact the author.
240            
241             To file a bug against this module, in a way you can keep track of, see the
242             CPAN bug tracking system.
243            
244             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Dependency>
245            
246             =head1 AUTHOR
247            
248             Adam Kennedy E<lt>cpan@ali.asE<gt>, L<http://ali.as/>
249            
250             =head1 SEE ALSO
251            
252             L<Algorithm::Dependency>, L<Algorithm::Dependency::Source::File>
253            
254             =head1 COPYRIGHT
255            
256             Copyright (c) 2003 - 2005 Adam Kennedy. All rights reserved.
257            
258             This program is free software; you can redistribute
259             it and/or modify it under the same terms as Perl itself.
260            
261             The full text of the license can be found in the
262             LICENSE file included with this module.
263            
264             =cut
265