From 1013c990c165464526ae8491d1d0c4ac65fc28c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Thu, 1 Oct 2009 13:43:10 +0200 Subject: [PATCH] using type coercion for cleaner class --- lib/Audio/MPD/Common/Status.pm | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/lib/Audio/MPD/Common/Status.pm b/lib/Audio/MPD/Common/Status.pm index 2266fb7..10b1f26 100644 --- a/lib/Audio/MPD/Common/Status.pm +++ b/lib/Audio/MPD/Common/Status.pm @@ -13,6 +13,9 @@ subtype 'Int_0_100' => where { $_ >= 0 && $_ <= 100 } => message { "$_ is not between 0 and 100" }; enum 'State' => qw{ play stop pause }; +coerce 'Audio::MPD::Common::Time' + => from 'Str' + => via { Audio::MPD::Common::Time->new($_) }; has audio => ( is=>'ro', required=>1, isa=>'Str' ); has bitrate => ( is=>'ro', required=>1, isa=>'Int' ); @@ -24,29 +27,11 @@ has repeat => ( is=>'ro', required=>1, isa=>'Bool' ); has songid => ( is=>'ro', required=>1, isa=>'Int' ); has song => ( is=>'ro', required=>1, isa=>'Int' ); has state => ( is=>'ro', required=>1, isa=>'State' ); -has time => ( is=>'ro', required=>1, isa=>'Audio::MPD::Common::Time' ); +has time => ( is=>'ro', required=>1, isa=>'Audio::MPD::Common::Time', coerce=>1 ); has updating_db => ( is=>'ro', required=>1, isa=>'Int' ); has volume => ( is=>'ro', required=>1, isa=>'Int_0_100' ); has xfade => ( is=>'ro', required=>1, isa=>'Int' ); - -#-- -# Constructor - -# -# my $status = Audio::MPD::Common::Status->new( \%kv ) -# -# The constructor for the class Audio::MPD::Common::Status. %kv is -# a cooked output of what MPD server returns to the status command. -# -sub new { - my ($class, $kv) = @_; - my %kv = %$kv; - $kv{time} = Audio::MPD::Common::Time->new( delete $kv{time} ); - bless \%kv, $class; - return \%kv; -} - 1; __END__ @@ -73,8 +58,9 @@ itself regularly, and thus should be used immediately. =item new( \%kv ) -The C method is the constructor for the L -class. +The C method is the constructor for the +L class. C<%kv> is a cooked output of what +MPD server returns to the status command. Note: one should B ever instantiate an L object directly - use the mpd modules instead. -- 2.11.4.GIT