lib/Mac/iTunes/Library/Playlist.pm

filter: Statementstmt 92.6% 88 / 95 Branchbran 68.7% 22 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.3% 14 / 17 Podpod 91.6% 11 / 12 totaltotal 86.5% 135 / 156 CC 17 SCAR 12.6
CC
17
Cov
87%
new
8.6
allItems
3.0
addItems
2.0
CRAP 3.5
« blib/lib/Mac/iTunes/Library/Item.pm ↑ Summary lib/Mac/iTunes/Library/XML.pm »
1 package Mac::iTunes::Library::Playlist;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 require Exporter;
9 3 our @ISA = qw(Exporter);
10 3 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
11 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
12 3 our @EXPORT = qw( );
13  
14 3 our $VERSION = '1.0';
15  
16 =head1 NAME
17  
18 Mac::iTunes::Library::Playlist - Perl extension for representing a playlist
19 (list of items by Track ID) within an iTunes library.
20  
21 =head1 SYNOPSIS
22  
23 use Mac::iTunes::Library::Playlist;
24  
25 # Let's create a few simple items
26 my @items = (
27 Mac::iTunes::Library::Item->new('Track ID' => 3),
28 Mac::iTunes::Library::Item->new('Track ID' => 4),
29 );
30 # Create a playlist
31 my $smartInfo =<< 'EOF';
32 AQEAAwAAAAIAAAAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
33 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
34 AAAAAA==
35 EOF
36  
37 my $smartCriteria =<< 'EOF';
38 U0xzdAABAAEAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
39 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
40 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkAAAABAAAAAAAAAAAAAAAAAAAAAAAA
41 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAGQAAAAAAAAAAAAAAAAAAAAB
42 AAAAAAAAAGQAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAA=
43 EOF
44 my $playlist = Mac::iTunes::Library::Playlist->new(
45 'Name' => '5 Stars',
46 'Playlist ID' => '10073',
47 'Playlist Persistent ID' => '2E2D1396AF1DED73',
48 'All Items' => 'true',
49 'Smart Info' => $smartInfo,
50 'Smart Criteria' => $smartCriteria,
51 'Playlist Items' => @items,
52 );
53  
54 # Add another item to that playlist
55 my $item = Mac::iTunes::Library::Item->new( 'Track ID' => 7 );
56 $playlist->addItem( $item );
57  
58 # Get all of the items in the playlist
59 my @items = $playlist->items();
60  
61 # Get an item by it's ID
62 $item = $playlist->item(3);
63  
64 =head1 DESCRIPTION
65  
66 A data structure for representing a playlist within an iTunes
67 library. Use this along with Mac::iTunes::Library to create an iTunes library
68 from which other information can be gleaned.
69  
70 =head1 EXPORT
71  
72 None by default.
73  
74 =head1 METHODS
75  
76 =head2 new()
77  
78 Creates a new Mac::iTunes::Playlist object that can store all of the data of an
79 iTunes library playlist.
80  
81 my $playlist = Mac::iTunes::Playlist->new();
82  
83 The constructor can also be called with any number of attributes defined in
84 a hash:
85  
86 my $smartInfo =<< 'EOF';
87 AQEAAwAAAAIAAAAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
88 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
89 AAAAAA==
90 EOF
91  
92 my $smartCriteria =<< 'EOF';
93 U0xzdAABAAEAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
94 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
95 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkAAAABAAAAAAAAAAAAAAAAAAAAAAAA
96 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAGQAAAAAAAAAAAAAAAAAAAAB
97 AAAAAAAAAGQAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAA=
98 EOF
99  
100 # A few simple items
101 my @items = (
102 Mac::iTunes::Library::Item->new('Track ID' => 2),
103 Mac::iTunes::Library::Item->new('Track ID' => 3),
104 Mac::iTunes::Library::Item->new('Track ID' => 4),
105 );
106  
107 my $playlist = Mac::iTunes::Playlist->new(
108 'Name' => '5 Stars',
109 'Playlist ID' => '10073',
110 'Playlist Persistent ID' => '2E2D1396AF1DED73',
111 'All Items' => 'true',
112 'Smart Info' => $smartInfo,
113 'Smart Criteria' => $smartCriteria,
114 'Playlist Items' => @items,
115 );
116  
117 =cut
118  
119 1 ▶sub new {
Subroutine new: called CC=8 SCAR=21.5
Pod documented
120 1 my $class = shift;
121 1 my %params = @_;
122  
123 1 my $self = {
124 'Name' => undef,
125 'Playlist ID' => undef,
126 'Playlist Persistent ID' => undef,
127 'All Items' => undef,
128 'Smart Info' => undef,
129 'Smart Criteria' => undef,
130 'Playlist Items' => [],
131 };
132  
133 1 bless $self, $class;
134  
135 # Deal with parameters
136 1 ▶ if ( exists( $params{'Name'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Name"}) 1 0 1
137 1 name( $self, $params{'Name'} );
138 1 ▶ } if ( exists( $params{'Playlist ID'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Playlist ID"}) 1 0 1
139 1 playlistID( $self, $params{'Playlist ID'} );
140 1 ▶ } if ( exists( $params{'Playlist Persistent ID'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Playlist Persistent ID"}) 1 0 1
141 1 playlistPersistentID( $self, $params{'Playlist Persistent ID'} );
142 1 ▶ } if ( exists( $params{'All Items'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"All Items"}) 1 0 1
143 1 allItems( $self, $params{'All Items'} );
144 1 ▶ } if ( exists( $params{'Smart Info'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Smart Info"}) 1 0 1
145 1 smartInfo( $self, $params{'Smart Info'} );
146 1 ▶ } if ( exists( $params{'Smart Criteria'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Smart Criteria"}) 1 0 1
147 1 smartCriteria( $self, $params{'Smart Criteria'} );
148 1 ▶ } if ( exists( $params{'Playlist Items'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Playlist Items"}) 1 0 1
149 1 addItems( $self, $params{'Playlist Items'} );
150 }
151  
152 1 return $self;
153 } #new
154  
155 # Clean up
156 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
157 # Nothing to do.
158 } #DESTROY
159  
160 =head2 name( name )
161  
162 Get/set the name attribute for this playlist.
163  
164 =cut
165  
166 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
167 2 my $self = shift;
168  
169 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
170 1 my $name = shift;
171 1 $self->{'Name'} = $name;
172 }
173  
174 2 return $self->{'Name'};
175 } #name
176  
177 =head2 playlistID( id )
178  
179 Get/set the Playlist ID attribute for this playlist.
180  
181 =cut
182  
183 2 ▶sub playlistID {
Subroutine playlistID: called CC=2 SCAR=6.9
Pod documented
184 2 my $self = shift;
185  
186 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
187 1 my $id = shift;
188 1 $self->{'Playlist ID'} = $id;
189 }
190  
191 2 return $self->{'Playlist ID'};
192 } #playlistID
193  
194 =head2 playlistPersistenID( id )
195  
196 Get/set the Playlist Persistent ID attribute for this playlist.
197  
198 =cut
199  
200 2 ▶sub playlistPersistentID {
Subroutine playlistPersistentID: called CC=2 SCAR=6.9
Pod undocumented
201 2 my $self = shift;
202  
203 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
204 1 my $id = shift;
205 1 $self->{'Playlist Persistent ID'} = $id;
206 }
207  
208 2 return $self->{'Playlist Persistent ID'};
209 } #playlistPersistentID
210  
211 =head2 allItems( 0|1 )
212  
213 Get/set the All Items attribute for this playlist.
214  
215 =cut
216  
217 2 ▶sub allItems {
Subroutine allItems: called CC=3 SCAR=11.0
Pod documented
218 2 my $self = shift;
219  
220 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
221 1 my $allItems = shift;
222 1 ▶ return carp "All items must be 0 or 1." unless ($allItems =~ /^[01]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $allItems =~ /^[01]$/ 0 1 1
223 1 $self->{'All Items'} = $allItems;
224 }
225  
226 2 return $self->{'All Items'};
227 } #allItems
228  
229 =head2 smartInfo( smartInfo )
230  
231 Get/set the Smart Info attribute for this playlist.
232  
233 =cut
234  
235 2 ▶sub smartInfo {
Subroutine smartInfo: called CC=2 SCAR=6.9
Pod documented
236 2 my $self = shift;
237  
238 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
239 1 my $smartInfo = shift;
240 1 $self->{'Smart Info'} = $smartInfo;
241 }
242  
243 2 return $self->{'Smart Info'};
244 } #smartInfo
245  
246 =head2 smartCriteria( smartInfo )
247  
248 Get/set the Smart Criteria attribute for this playlist.
249  
250 =cut
251  
252 2 ▶sub smartCriteria {
Subroutine smartCriteria: called CC=2 SCAR=6.9
Pod documented
253 2 my $self = shift;
254  
255 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
256 1 my $smartCriteria = shift;
257 1 $self->{'Smart Criteria'} = $smartCriteria;
258 }
259  
260 2 return $self->{'Smart Criteria'};
261 } #smartCriteria
262  
263 =head2 num()
264  
265 Get the number of elements in this playlist.
266  
267 =cut
268  
269 0 ▶sub num {
Subroutine num: not called CC=1 SCAR=6.9
Pod documented
270 0 my $self = shift;
271  
272 0 return scalar(@{$self->{'items'}});
273 } #num
274  
275 =head2 addItem( Mac::iTunes::Library::Item )
276  
277 Add an item to this playlist; duplicates are allowed
278  
279 =cut
280  
281 1 ▶sub addItem {
Subroutine addItem: called CC=2 SCAR=7.0
Pod documented
282 1 my $self = shift;
283 1 my $item = shift;
284  
285 1 ▶ return carp "Need a Mac::iTunes::Library::Item object."
Branch: 1 branch
BranchTrueFalseTotal
unless $item->isa("Mac::iTunes::Library::Item") 0 1 1
286 unless ($item->isa('Mac::iTunes::Library::Item'));
287  
288 1 push @{$self->{'items'}}, $item;
289 # TODO: shouldn't this be referencing a track already in the lib?
290 } #addItem
291  
292 =head2 addItems( Mac::iTunes::Library::Item )
293  
294 Add an array of items to this playlist; duplicates are allowed
295  
296 =cut
297  
298 1 ▶sub addItems {
Subroutine addItems: called CC=2 SCAR=7.1
Pod documented
299 1 my $self = shift;
300 1 my $items = shift;
301  
302 # Complain if there are any non-item objects
303 1 ▶ unless (grep {$_->isa('Mac::iTunes::Library::Item')} @{$items}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->isa("Mac::iTunes::Library::Item");} @{$items;}) 0 1 1
304 0 return carp "Given an array containig items that are not all " .
305 "Mac::iTunes::Library::Item objects.";
306 }
307  
308 1 push @{$self->{'items'}}, @{$items};
309 } #addItems
310  
311 =head2 items()
312  
313 Get an array of the items in the playlist.
314  
315 =cut
316  
317 0 ▶sub items {
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
318 0 my $self = shift;
319  
320 0 return @{$self->{'items'}};
321 } #items
322  
323 =head2 item( trackID )
324  
325 Get an item by it's trackID
326  
327 =cut
328  
329 1 ▶sub item {
Subroutine item: called CC=1 SCAR=0.0
Pod documented
330 1 my $self = shift;
331 1 my $id = shift;
332  
333 # There may be duplicates
334 1 my @items = grep {$_->trackID() == $id} @{$self->{'items'}};
335  
336 1 return $items[0];
337 } #item
338  
339 3 1;
340  
341 =head1 SEE ALSO
342  
343 L<Mac::iTunes::Library>, L<Mac::iTunes::Library::Item>
344  
345 =head1 AUTHOR
346  
347 Drew Stephens <drew@dinomite.net>, http://dinomite.net
348  
349 =head1 CONTRIBUTORS
350  
351 =over 4
352  
353 =item *
354  
355 Mark Grimes <mgrimes@cpan.org>, L<http://www.peculiarities.com>
356  
357 =back
358  
359 =head1 SOURCE REPOSITORY
360  
361 http://mac-itunes.googlecode.com
362  
363 =head1 SVN INFO
364  
365 $Revision: 90 $
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 Copyright (C) 2007-2008 by Drew Stephens
370  
371 This library is free software; you can redistribute it and/or modify
372 it under the same terms as Perl itself, either Perl version 5.8.8 or,
373 at your option, any later version of Perl 5 you may have available.
374  
375 =cut
376 __END__
« blib/lib/Mac/iTunes/Library/Item.pm ↑ Summary lib/Mac/iTunes/Library/XML.pm »