Acme-FishFarm

 view release on metacpan or  search on metacpan

lib/Acme/FishFarm/Feeder.pm  view on Meta::CPAN

=head1 CREATION RELATED METHODS

=head2 install ( %options )

Installs an automated fish feeder.

The following are available for C<%options>:

=over 4

=item * timer

The default is C<8>.

This is used as a threshold to identify that the time is up to feed the fish or not.

The clock will be set to this value for countdown.

=item * feeding_volume

The default is C<50 cm^3>.

=item * food_tank_capacity

The maximum volume of fish food. Default is C<500 cm^3>.

=item * current_food_amount

The initial amount of food to be filled into the food tank. Default is max ie C<500 cm^3>.

=back

=cut

sub install {
    my $class = shift;
    my %options = @_;
    
    # value of 0 should not work
    if ( not $options{timer} ) {
        $options{timer} = 8; # this is used as a reference only
    }
    
    # when clock is a multiple of timer, then feed fish
    $options{clock} = 0; # this is the actual one that will keep changing
    
    if ( not $options{feeding_volume} ) {
        $options{feeding_volume} = 50;
    }
    
    if ( not $options{food_tank_capacity} ) {
        $options{food_tank_capacity} = 500;
    }
    
    if ( not $options{current_food_amount} ) {
        $options{current_food_amount} = $options{food_tank_capacity};
    }
    
    $options{first_usage} = 1; # make sure the feeder doesn't say timer is up as soon as it is switched on
    
    bless \%options, $class;
}



=head1 TIMER RELATED SUBROUTINES/METHODS

=head2 get_timer

Returns the timer threshold of the feeder.

=cut

sub get_timer {
    ref( my $self = shift ) or croak "Please use this the OO way";
    $self->{timer};
}

=head2 set_timer ( $time )

Sets the new timer threshold of the feeder.

Setting this timer will not affect the clock within the feeder.

=cut

sub set_timer {
    ref( my $self = shift ) or croak "Please use this the OO way";
    $self->{timer} = shift;
}


=head2 timer_is_up

Check if the timer is up. If timer is up, please remember to feed your fish. See C<feed_fish> for more info.

=cut

sub timer_is_up {
    ref (my $self = shift) or croak "Please use this the OO way";
    
    # skip the first round, 0 % n is always 0 and the feeder might think it's time to feed the fish as soon as it's switched on
    if ( $self->{first_usage} ) {
        $self->{first_usage} = 0;
        return 0;
    }
    
    if ( $self->{clock} % $self->{timer} == 0 ) {
        # reset clock to 0 and return true
        $self->{clock} = 0; # just in case the clock runs for too long
        1;
    } else {
        0;
    }
}

=head2 time_remaining

Returns the time remaining to feed the fish.

This method might not be really useful, but anyway :)



( run in 1.574 second using v1.01-cache-2.11-cpan-5511b514fd6 )