File Coverage

blib/lib/Perl/Metrics/File.pm
Criterion Covered Total %
statement 30 31 96.7
branch 7 10 70.0
condition n/a
subroutine 9 9 100.0
pod 2 3 66.6
total 48 53 90.5


line stmt bran cond sub pod time code
1             package Perl::Metrics::File;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Perl::Metrics::File - A local file to generate metrics for
8              
9             =head1 DESCRIPTION
10              
11             This class provides objects that link files on the local filesystem to
12             the main metrics table via their document C (see L)
13              
14             =head1 METHODS
15              
16             In addition to the general methods provided by L, this class has
17             the following additional methods.
18              
19             =cut
20              
21 6     6   40 use strict;
  6         106  
  6         253  
22 6     6   35 use Perl::Metrics ();
  6         13  
  6         89  
23 6     6   5967 use PPI::Document ();
  6         910456  
  6         180  
24 6     6   70 use base 'Perl::Metrics::CDBI';
  6         14  
  6         786  
25              
26 6     6   34 use vars qw{$VERSION};
  6         35  
  6         302  
27             BEGIN {
28 6     6   2159 $VERSION = '0.09';
29             }
30              
31              
32              
33              
34             #####################################################################
35             # Class::DBI Setup and Accessors
36              
37             =pod
38              
39             =head2 path
40              
41             The C accessor returns a string which contains the non-relative file
42             path on the local system.
43              
44             =head2 checked
45              
46             The C accessor returns the Unix epoch time for when the C
47             was last checked for this file.
48              
49             =head2 hex_id
50              
51             In the L system all documents are identified by the
52             hexidecimal MD5 value for their newline-localized contents.
53              
54             The C accessor returns this id for the file.
55              
56             =cut
57              
58             Perl::Metrics::File->table( 'files' );
59             Perl::Metrics::File->columns( Essential =>
60             'path', # Absolute local filesystem path - '/foo/bar/baz.pm'
61             'checked', # UNIX epoch time last checked - '1128495103'
62             'hex_id', # Document MD5 Identifier - 'abcdef1234567890'
63             );
64              
65             # Add custom deletion cascade
66             Perl::Metrics::File->add_trigger(
67             before_delete => sub { $_[0]->before_delete },
68             );
69             sub before_delete {
70 3     3 0 8 my $self = shift;
71              
72 3 100       70 if ( $self->search( hex_id => $self->hex_id )->count == 1 ) {
73             # We are the last file with this hex_id.
74             # Remove any metrics that were accumulated.
75 2         6598 $self->metrics->delete_all;
76             }
77              
78 3         41631 1;
79             }
80              
81             =pod
82              
83             =head2 metrics @options
84              
85             The C accessor finds and returns all C<::Metric> object
86             that match the C of the C<::File>.
87              
88             =cut
89              
90             sub metrics {
91 11     11 1 14829 my $self = shift;
92              
93             # Apply default search options to those passed
94 11         51 my @params = ( hex_id => $self->hex_id, @_ );
95 11 50       756 unless ( ref($params[-1]) eq 'HASH' ) {
96             # Add standard ordering
97 11         45 push @params, { order_by => 'package, name' };
98             }
99              
100             # Execute the search
101             return wantarray
102 11 100       107 ? Perl::Metrics::Metric->search( @params )
103             : scalar Perl::Metrics::Metric->search( @params );
104             }
105              
106             =pod
107              
108             =head2 Document
109              
110             The C method provides a convenient shortcut which will
111             load the L object for the file (while confirming the
112             C matches).
113              
114             Returns a L or dies on error.
115              
116             =cut
117              
118             sub Document {
119 7     7 1 5306 my $self = shift;
120 7         33 my $path = $self->path;
121              
122             # Load and check the Document object
123 7 50       516 my $Document = PPI::Document->new( $path )
124             or Carp::croak("Failed to load Perl document '$path'");
125 7 50       29246 unless ( $Document->hex_id eq $self->hex_id ) {
126 0         0 Carp::croak("Document at '$path' fails hex_id match check");
127             }
128              
129 7         4331 $Document;
130             }
131              
132             1;
133              
134             =pod
135              
136             =head1 SUPPORT
137              
138             Bugs should be reported via the CPAN bug tracker at
139              
140             L
141              
142             For other issues, contact the author.
143              
144             =head1 AUTHOR
145              
146             Adam Kennedy Eadamk@cpan.orgE
147              
148             =head1 SEE ALSO
149              
150             L, L
151              
152             =head1 COPYRIGHT
153              
154             Copyright 2005 - 2008 Adam Kennedy.
155              
156             This program is free software; you can redistribute
157             it and/or modify it under the same terms as Perl itself.
158              
159             The full text of the license can be found in the
160             LICENSE file included with this module.
161              
162             =cut