converted to moose
[audio-mpd-common.git] / lib / Audio / MPD / Common / Item / Song.pm
blob9c23d700bcddc59184ccb6f50383e1c2a38fc8b8
1 use strict;
2 use warnings;
4 package Audio::MPD::Common::Item::Song;
5 # ABSTRACT: a song object with some audio tags
7 use Moose;
8 use Readonly;
10 use base qw{ Audio::MPD::Common::Item };
11 use overload '""' => \&as_string;
13 Readonly my $SEP => ' = ';
16 # -- attributes
18 =attr $song->album;
20 Album of the song.
22 =attr $song->artist;
24 Artist of the song.
26 =attr $song->date;
28 Last modification date of the song.
30 =attr $song->file;
32 Path to the song. Only attribute which will always be defined.
34 =attr $song->genre;
36 Genre of the song.
38 =attr $song->id;
40 Id of the song in MPD's database.
42 =attr $song->pos;
44 Position of the song in the playlist.
46 =attr $song->title;
48 Title of the song.
50 =attr $song->track;
52 Track number of the song.
54 =attr $song->time;
56 Length of the song in seconds.
58 =cut
60 has album => ( is=>'ro', isa=>'Str' );
61 has artist => ( is=>'ro', isa=>'Str' );
62 has date => ( is=>'ro', isa=>'Int' );
63 has file => ( is=>'ro', isa=>'Str', required=>1 );
64 has genre => ( is=>'ro', isa=>'Str' );
65 has id => ( is=>'ro', isa=>'Int' );
66 has pos => ( is=>'ro', isa=>'Int' );
67 has title => ( is=>'ro', isa=>'Str' );
68 has track => ( is=>'ro', isa=>'Int' );
69 has time => ( is=>'ro', isa=>'Int' );
72 # -- public methods
74 =method my $str = $song->as_string;
76 Return a string representing $song. This string will be:
78 =over 4
80 =item either "album = track = artist = title"
82 =item or "artist = title"
84 =item or "title"
86 =item or "file"
88 =back
90 (in this order), depending on the existing tags of the song. The last
91 possibility always exist of course, since it's a path.
93 This method is also used to automatically stringify the C<$song>.
95 =cut
97 sub as_string {
98 my ($self) = @_;
100 return $self->file unless defined $self->title;
101 my $str = $self->title;
102 return $str unless defined $self->artist;
103 $str = $self->artist . $SEP . $str;
104 return $str unless defined $self->album && defined $self->track;
105 return join $SEP,
106 $self->album,
107 $self->track,
108 $str;
112 __END__
114 =head1 DESCRIPTION
116 L<Audio::MPD::Common::Item::Song> is more a placeholder with some
117 attributes. Those attributes are taken from the song tags, so some of
118 them can be empty depending on the file.
120 The constructor should only be called by L<Audio::MPD::Common::Item>'s
121 constructor.