From 439e5ba9b4458bfcffd211917fc6336057296b22 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Thu, 1 Oct 2009 17:28:53 +0200 Subject: [PATCH] converted to moose --- lib/Audio/MPD/Common/Item/Song.pm | 119 ++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 45 deletions(-) diff --git a/lib/Audio/MPD/Common/Item/Song.pm b/lib/Audio/MPD/Common/Item/Song.pm index 584a210..9c23d70 100644 --- a/lib/Audio/MPD/Common/Item/Song.pm +++ b/lib/Audio/MPD/Common/Item/Song.pm @@ -4,72 +4,74 @@ use warnings; package Audio::MPD::Common::Item::Song; # ABSTRACT: a song object with some audio tags -use overload '""' => \&as_string; +use Moose; use Readonly; -use base qw[ Class::Accessor::Fast Audio::MPD::Common::Item ]; -__PACKAGE__->mk_accessors( qw[ album artist date file genre id pos title track time ] ); - +use base qw{ Audio::MPD::Common::Item }; +use overload '""' => \&as_string; Readonly my $SEP => ' = '; -# -# my $str = $song->as_string; -# -# Return a string representing $song. This string will be; -# - either "album = track = artist = title" -# - or "artist = title" -# - or "title" -# - or "file" -# (in this order), depending on the existing tags of the song. The last -# possibility always exist of course, since it's a path. -# -sub as_string { - my ($self) = @_; +# -- attributes - return $self->file unless defined $self->title; - my $str = $self->title; - return $str unless defined $self->artist; - $str = $self->artist . $SEP . $str; - return $str unless defined $self->album && defined $self->track; - return join $SEP, - $self->album, - $self->track, - $str; -} +=attr $song->album; -1; -__END__ +Album of the song. -=head1 DESCRIPTION +=attr $song->artist; -L is more a placeholder for a -hash ref with some pre-defined keys, namely some audio tags. +Artist of the song. +=attr $song->date; -=head1 PUBLIC METHODS +Last modification date of the song. -This module has a C constructor, which should only be called by -L's constructor. +=attr $song->file; -The only other public methods are the accessors - see below. +Path to the song. Only attribute which will always be defined. +=attr $song->genre; -=head2 Accessors +Genre of the song. -The following methods are the accessors to their respective named -fields: C, C, C, C, C, C, -C, C, C, C. You can call them either with -no arg to get the value, or with an arg to replace the current value. +=attr $song->id; +Id of the song in MPD's database. -=head2 Methods +=attr $song->pos; +Position of the song in the playlist. -=over 4 +=attr $song->title; + +Title of the song. + +=attr $song->track; + +Track number of the song. + +=attr $song->time; + +Length of the song in seconds. + +=cut -=item $song->as_string() +has album => ( is=>'ro', isa=>'Str' ); +has artist => ( is=>'ro', isa=>'Str' ); +has date => ( is=>'ro', isa=>'Int' ); +has file => ( is=>'ro', isa=>'Str', required=>1 ); +has genre => ( is=>'ro', isa=>'Str' ); +has id => ( is=>'ro', isa=>'Int' ); +has pos => ( is=>'ro', isa=>'Int' ); +has title => ( is=>'ro', isa=>'Str' ); +has track => ( is=>'ro', isa=>'Int' ); +has time => ( is=>'ro', isa=>'Int' ); + + +# -- public methods + +=method my $str = $song->as_string; Return a string representing $song. This string will be: @@ -88,5 +90,32 @@ Return a string representing $song. This string will be: (in this order), depending on the existing tags of the song. The last possibility always exist of course, since it's a path. -=back +This method is also used to automatically stringify the C<$song>. + +=cut + +sub as_string { + my ($self) = @_; + + return $self->file unless defined $self->title; + my $str = $self->title; + return $str unless defined $self->artist; + $str = $self->artist . $SEP . $str; + return $str unless defined $self->album && defined $self->track; + return join $SEP, + $self->album, + $self->track, + $str; +} + +1; +__END__ + +=head1 DESCRIPTION + +L is more a placeholder with some +attributes. Those attributes are taken from the song tags, so some of +them can be empty depending on the file. +The constructor should only be called by L's +constructor. -- 2.11.4.GIT