blib/lib/AudioFile/Find.pm

filter: Statementstmt 84.6% 44 / 52 Branchbran 30.0% 6 / 20 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 71.2% 67 / 94 CC 14 SCAR 24.5
CC
14
Cov
69%
pass
32.1
search
6.1
extensions
2.3
CRAP 11.5
↑ Summary
1 package AudioFile::Find;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use AudioFile::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::MoreUtils qw( zip );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use YAML 'LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 AudioFile::Find - Finds audio files located on your system and maps them to L<AudioFile::Info> objects.
14  
15 =cut
16  
17 1 our $VERSION = '0.03';
18 1 $VERSION = eval $VERSION;
19  
20 =head1 SYNOPSIS
21  
22 use AudioFile::Find;
23  
24 my $finder = AudioFile::Find->new( 'some/dir' );
25
26 # find everything
27 my @audiofiles = $finder->search();
28
29 # specify a search directory
30 my @audiofiles = $finder->search( 'some/other/dir' );
31
32 #same for genre, title, track, artist and album
33 my @audiofiles = $finder->search( artist => 'Seeed' );
34
35 #search using a regex
36 my @audiofiles = $finder->search( 'some/other/dir', title => qr/Ding/ );
37
38 # anonymous subroutine that returns true or false
39 my @audiofiles = $finder->search( 'some/other/dir', track => sub { return shift > 10; } );
40  
41 =head1 METHODS
42  
43 =head2 new
44  
45 Creates an object of this class. Takes an optional single argument which is the directory to search in.
46  
47 =cut
48  
49 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 3 my ($class, $dir) = @_;
51 3 return bless { dir => $dir }, $class;
52 }
53  
54 =head2 new
55  
56 Sets and returns the directory to search.
57  
58 =cut
59  
60 1 ▶sub dir {
Subroutine dir: called CC=2 SCAR=7.1
Pod undocumented
61 1 my ($self, $dir) = @_;
62 1 ▶ $self->{dir} = $dir if defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dir 0 1 1
63 1 return $self->{dir};
64 }
65  
66 =head2 search
67  
68 Starts the search and returns a hash of filenames as keys and AudioFile::Info-Objects as values.
69 You may specify a search directory as the first argument
70 and also pass a hash with search criteria. See the synopsis for details.
71  
72 =cut
73  
74 2 ▶sub search {
Subroutine search: called CC=6 SCAR=18.2
Pod documented
75 2 my $self = shift;
76 2 ▶ my $dir = @_ % 2 == 0 ? '' : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 1 1 2
77 2 my $args = {@_};
78 2 my %audio;
79
80 2 my @patterns = map { "*.$_" } $self->extensions;
81 2 ▶ for ( File::Find::Rule->file()->name( @patterns )->in( $dir || $self->dir || '.' ) )
Condition60%3/5
$dir || $self->dir || '.'
l!l
20
$dir or $self->dir
l!l&&r!l&&!r
110
MC/DC0%0/2
$dir || $self->dir || '.'
$dir $self->dir
Truth table66%2/3
A or B
A= $dirB= $self->dir
ABresult
1X1
011
000
82 {
83 2 my $info = AudioFile::Info->new($_);
84
85 2 ▶ $audio{$_} = $info
Branch: 1 branch
BranchTrueFalseTotal
if $self->pass($info, $args) 2 0 2
86 if $self->pass( $info, $args);
87 }
88
89 2 return %audio;
90 }
91  
92 =head2 pass
93  
94 Checks whether a given L<AudioFile::Info> object meets given criteria.
95 First argument is the L<AudioFile::Info> object, second argument is a reference to the criteria hash.
96  
97 =cut
98  
99 sub pass
100 2 ▶{
Subroutine pass: called CC=7 SCAR=34.7
Pod documented
101 2 my ($self, $file, $criteria) = @_;
102
103 2 ▶ while ( my ($key, $criterium) = each %$criteria )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$criteria) 0 2 2
104 {
105 0 my $value = $file->$key;
106
107 0 ▶ if ( ref($criterium) eq "Regexp" )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $criterium eq 'Regexp') { } 0 0 0
elsif (ref $criterium eq 'CODE') { } 0 0 0
108 {
109 0 ▶ return unless $value =~ $criterium;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$criterium/ 0 0 0
110 }
111 elsif ( ref($criterium) eq "CODE" )
112 {
113 0 ▶ return unless $criterium->( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless &$criterium($value) 0 0 0
114 }
115 else
116 {
117 0 ▶ return unless $value eq $criterium;
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $criterium 0 0 0
118 }
119 }
120
121 2 return 1;
122 }
123  
124 =head2 extensions
125  
126 Discovers the extensions that are supported by the installed L<AudioFile::Info> plugins.
127  
128 =cut
129  
130 1 ▶sub extensions {
Subroutine extensions: called CC=2 SCAR=8.1
131 1 my ($self) = @_;
132 1 my $path = $INC{'AudioFile/Info.pm'};
133 1 $path =~ s/Info.pm$/plugins.yaml/;
134 1 ▶ my $config = eval { LoadFile($path) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $config = eval { do { LoadFile($path) } } or return 0 1 1
135 0 my @ext = keys %{ $config->{default} };
136 0 return @ext;
137 }
138  
139 1 1;
140  
141 =head1 AUTHORS
142  
143 =over
144  
145 =item Markus, C<< <holli.holzer at googlemail.com> >>
146  
147 =item Joel Berger C<joel.a.berger@gmail.com>
148  
149 =back
150  
151 =head1 SOURCE REPOSITORY
152  
153 L<http://github.com/jberger/AudioFile-Find>
154  
155 =head1 BUGS
156  
157 Bugs may be reported to:
158  
159 =over
160  
161 =item L<http://github.com/jberger/AudioFile-Find/issues>
162  
163 =item L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AudioFile-Find>
164  
165 =back
166  
167 =head1 COPYRIGHT & LICENSE
168  
169 Copyright 2008-2014 by Authors listed above, all rights reserved.
170  
171 This program is free software; you can redistribute it and/or modify it
172 under the same terms as Perl itself.
173  
174 =cut
175  
↑ Summary