From 1586e552683a80c7dbb11c9b4dc9680d52fb9e32 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Quelin?= Date: Thu, 1 Oct 2009 18:03:52 +0200 Subject: [PATCH] migrated to moose --- lib/Audio/MPD/Common/Status.pm | 2 +- lib/Audio/MPD/Common/Time.pm | 191 +++++++++++++++++++++++------------------ t/11-time.t | 4 +- 3 files changed, 109 insertions(+), 88 deletions(-) diff --git a/lib/Audio/MPD/Common/Status.pm b/lib/Audio/MPD/Common/Status.pm index bacf566..8baaa00 100644 --- a/lib/Audio/MPD/Common/Status.pm +++ b/lib/Audio/MPD/Common/Status.pm @@ -18,7 +18,7 @@ subtype 'Int_0_100' enum 'State' => qw{ play stop pause }; coerce 'Audio::MPD::Common::Time' => from 'Str' - => via { Audio::MPD::Common::Time->new($_) }; + => via { Audio::MPD::Common::Time->new(time=>$_) }; # -- public attributes diff --git a/lib/Audio/MPD/Common/Time.pm b/lib/Audio/MPD/Common/Time.pm index ba6ac09..429b121 100644 --- a/lib/Audio/MPD/Common/Time.pm +++ b/lib/Audio/MPD/Common/Time.pm @@ -4,28 +4,106 @@ use warnings; package Audio::MPD::Common::Time; # ABSTRACT: class representing time of current song -use base qw[ Class::Accessor::Fast ]; -__PACKAGE__->mk_accessors - ( qw[ percent sofar left total - sofar_secs sofar_mins seconds_sofar - total_secs total_mins seconds_total - left_secs left_mins seconds_left - ] ); - - -#-- -# Constructor - -# -# my $status = Audio::MPD::Common::Time->new( $time ) -# -# The constructor for the class Audio::MPD::Common::Time. $time is -# the time value (on the "time" line) of what the output MPD server -# returns to the status command. -# -sub new { - my ($class, $time) = @_; - $time ||= '0:0'; +use Moose; + + +# -- attributes + +=attr $time->time; + +The time passed to the constructor, used to compute all others values +(see methods). It is the time value (on the "time" line) of what the MPD +server returns to the status command. Defaults to C<0:0>. + +=cut + +has time => ( is=>'ro', isa=>'Str', default=>'0:0' ); + + + +=method my $str = $time->sofar; + +Return elapsed C<$time> (C format). + +=method my $str = $time->left; + +Return remaining C<$time> (C format). + +=method my $str = $time->left; + +Return total C<$time> (C format). + +=method my $percent = $time->percent; + +Return elapsed C<$time> (percentage, 1 digit). + +=method my $secs = $time->seconds_sofar; + +Return elapsed C<$time> in seconds. + +=method my $secs = $time->seconds_left; + +Return remaining C<$time> in seconds. + +=method my $secs = $time->seconds_total; + +Return total C<$time> in seconds. + +=method my $mins = $time->sofar_mins; + +Return minutes part of elapsed C<$time>. + +=method my $secs = $time->sofar_secs; + +Return seconds part of elapsed C<$time>. + +=method my $mins = $time->left_mins; + +Return minutes part of remaining C<$time>. + +=method my $secs = $time->left_secs; + +Return seconds part of remaining C<$time>. + +=method my $mins = $time->total_mins; + +Return minutes part of total C<$time>. + +=method my $mins = $time->total_secs; + +Return seconds part of total C<$time>. + +=cut + +# _cooked_values contains all the computed values. +has _cooked_values => ( + traits => [ 'Hash' ], + is => 'ro', + isa => 'HashRef', + lazy_build => 1, + handles => { + percent => [ get => 'percent' ], + sofar => [ get => 'sofar' ], + left => [ get => 'left' ], + total => [ get => 'total' ], + sofar_secs => [ get => 'sofar_secs' ], + sofar_mins => [ get => 'sofar_mins' ], + seconds_sofar => [ get => 'seconds_sofar' ], + total_secs => [ get => 'total_secs' ], + total_mins => [ get => 'total_mins' ], + seconds_total => [ get => 'seconds_total' ], + left_secs => [ get => 'left_secs' ], + left_mins => [ get => 'left_mins' ], + seconds_left => [ get => 'seconds_left' ], + }, +); + +# -- builders + +sub _build__cooked_values { + my $self = shift; + my $time = $self->time; + my ($seconds_sofar, $seconds_total) = split /:/, $time; my $seconds_left = $seconds_total - $seconds_sofar; my $percent = $seconds_total ? 100*$seconds_sofar/$seconds_total : 0; @@ -46,8 +124,7 @@ sub new { my $left = sprintf "%d:%02d", $left_mins, $left_secs; - # create object - my $self = { + return { # time elapsed in seconds seconds_sofar => $seconds_sofar, seconds_left => $seconds_left, @@ -67,79 +144,23 @@ sub new { left_secs => $left_secs, left_mins => $left_mins, }; - bless $self, $class; - return $self; } 1; __END__ -=head1 SYNOPSIS - - my $time = $status->time; - print $time->sofar; - =head1 DESCRIPTION -L returns some time information with the C -accessor. This information relates to the elapsed time of the current song, -as well as the remaining and total time. This information is encapsulated -in an L object. +L returns some time information with the +C accessor. This information relates to the elapsed time of the +current song, as well as the remaining and total time. This information +is encapsulated in an L object. -Note that an L object does B update itself +An L object does B update itself regularly, and thus should be used immediately. - -=head1 METHODS - -=head2 Constructor - -=over 4 - -=item new( $time ) - -The C method is the constructor for the L -class. - Note: one should B ever instantiate an L object directly - use the mpd modules instead. -=back - - -=head2 Accessors - -Once created, one can access the following members of the object: - -=over 4 - -=item cooked values: - -The C, C and C methods return the according values -under the form C. Note the existence of a C -method returning a percentage complete. (one decimal) - - -=item values in seconds: - -The C, C and C return the -according values in seconds. - - -=item detailled values: - -If you want to cook your own value, then the following methods can help: -C and C return the seconds and minutes elapsed. -Same for C and C (time remaining), C -and C. (total song length) - - -=back - - -Please note that those accessors are read-only: changing a value will B -change the current settings of MPD server. Use the mpd modules to alter the -settings. - diff --git a/t/11-time.t b/t/11-time.t index f3571a9..37c58d9 100644 --- a/t/11-time.t +++ b/t/11-time.t @@ -6,7 +6,7 @@ use Test::More tests => 14; # # formatted output -my $time = Audio::MPD::Common::Time->new( '126:225' ); +my $time = Audio::MPD::Common::Time->new( time => '126:225' ); is( $time->sofar, '2:06', 'sofar() formats time so far' ); is( $time->left, '1:39', 'left() formats remaining time' ); is( $time->total, '3:45', 'sofar() formats time so far' ); @@ -33,5 +33,5 @@ is( $time->seconds_total, 225, 'seconds_total() gives time total in secs' ); # # testing null time -$time = Audio::MPD::Common::Time->new( '126:0' ); +$time = Audio::MPD::Common::Time->new( time => '126:0' ); is( $time->percent, '0.0', 'percent() defaults to 0' ); -- 2.11.4.GIT