FishFarm/Feeder.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 65.2% 30 / 46 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 94.7% 18 / 19 Podpod 100% 13 / 13 totaltotal 84.3% 135 / 160 CC 26 SCAR 11.9
CC
26
Cov
83%
set_timer
6.0
refill
5.0
install
5.0
CRAP 3.3
« FishFarm/WaterConditionMonitor.pm ↑ Summary FishFarm/WaterFiltration.pm »
1 package Acme::FishFarm::Feeder;
2  
3 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp "croak";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Acme::FishFarm::Feeder - Automated Feeder for Acme::FishFarm
12  
13 =head1 VERSION
14  
15 Version 1.01
16  
17 =cut
18  
19 3 our $VERSION = '1.01';
20  
21  
22 =head1 SYNOPSIS
23  
24 use 5.010;
25 use Acme::FishFarm::Feeder;
26  
27 my $feeder = Acme::FishFarm::Feeder->install( timer => 3, feeding_volume => 150 );
28  
29 say "Feeder installed and switched on!";
30 say "";
31  
32 while ( "fish are living happilly" ) {
33  
34 if ( $feeder->timer_is_up ) {
35 say "\nTimer is up, time to feed the fish!";
36 say "Feeding ", $feeder->feeding_volume, " cm^3 of fish food to the fish...";
37
38 $feeder->feed_fish;
39
40 say $feeder->food_remaining, " cm^3 of fish food remaining in the tank.\n";
41 }
42
43 if ( $feeder->food_remaining <= 0 ) {
44 $feeder->refill; # default back to 500 cm^3
45 say "Refilled food tank back to ", $feeder->food_tank_capacity, " cm^3.\n";
46 }
47
48 say $feeder->time_remaining, " hours left until it's time to feed the fish.";
49  
50 sleep(1);
51 $feeder->tick_clock;
52 }
53  
54 say "";
55 say "Feeder was switched off, please remeber to feed your fish on time :)";
56  
57 =head1 EXPORT
58  
59 None
60  
61 =head1 CREATION RELATED METHODS
62  
63 =head2 install ( %options )
64  
65 Installs an automated fish feeder.
66  
67 The following are available for C<%options>:
68  
69 =over 4
70  
71 =item * timer
72  
73 The default is C<8>.
74  
75 This is used as a threshold to identify that the time is up to feed the fish or not.
76  
77 The clock will be set to this value for countdown.
78  
79 =item * feeding_volume
80  
81 The default is C<50 cm^3>.
82  
83 =item * food_tank_capacity
84  
85 The maximum volume of fish food. Default is C<500 cm^3>.
86  
87 =item * current_food_amount
88  
89 The initial amount of food to be filled into the food tank. Default is max ie C<500 cm^3>.
90  
91 =back
92  
93 =cut
94  
95 6 ▶sub install {
Subroutine install: called CC=5 SCAR=16.1
Pod documented
96 6 my $class = shift;
97 6 my %options = @_;
98
99 # value of 0 should not work
100 6 ▶ if ( not $options{timer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"timer"}) 4 2 6
101 4 $options{timer} = 8; # this is used as a reference only
102 }
103
104 # when clock is a multiple of timer, then feed fish
105 6 $options{clock} = 0; # this is the actual one that will keep changing
106
107 6 ▶ if ( not $options{feeding_volume} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"feeding_volume"}) 4 2 6
108 4 $options{feeding_volume} = 50;
109 }
110
111 6 ▶ if ( not $options{food_tank_capacity} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"food_tank_capacity"}) 6 0 6
112 6 $options{food_tank_capacity} = 500;
113 }
114
115 6 ▶ if ( not $options{current_food_amount} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"current_food_amount"}) 6 0 6
116 6 $options{current_food_amount} = $options{food_tank_capacity};
117 }
118
119 6 $options{first_usage} = 1; # make sure the feeder doesn't say timer is up as soon as it is switched on
120
121 6 bless \%options, $class;
122 }
123  
124  
125  
126 =head1 TIMER RELATED SUBROUTINES/METHODS
127  
128 =head2 get_timer
129  
130 Returns the timer threshold of the feeder.
131  
132 =cut
133  
134 2 ▶sub get_timer {
Subroutine get_timer: called CC=2 SCAR=7.2
Pod documented
135 2 ▶ ref( my $self = shift ) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 2 0 2
136 2 $self->{timer};
137 }
138  
139 =head2 set_timer ( $time )
140  
141 Sets the new timer threshold of the feeder.
142  
143 Setting this timer will not affect the clock within the feeder.
144  
145 =cut
146  
147 0 ▶sub set_timer {
Subroutine set_timer: not called CC=2 SCAR=17.9
Pod documented
148 0 ▶ ref( my $self = shift ) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 0 0 0
149 0 $self->{timer} = shift;
150 }
151  
152  
153 =head2 timer_is_up
154  
155 Check if the timer is up. If timer is up, please remember to feed your fish. See C<feed_fish> for more info.
156  
157 =cut
158  
159 5 ▶sub timer_is_up {
Subroutine timer_is_up: called CC=4 SCAR=13.9
Pod documented
160 5 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 5 0 5
161
162 # 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
163 5 ▶ if ( $self->{first_usage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"first_usage"}) 2 3 5
164 2 $self->{first_usage} = 0;
165 2 return 0;
166 }
167
168 3 ▶ if ( $self->{clock} % $self->{timer} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'clock'} % $self->{'timer'} == 0) { } 1 2 3
169 # reset clock to 0 and return true
170 1 $self->{clock} = 0; # just in case the clock runs for too long
171 1 1;
172 } else {
173 2 0;
174 }
175 }
176  
177 =head2 time_remaining
178  
179 Returns the time remaining to feed the fish.
180  
181 This method might not be really useful, but anyway :)
182  
183 =cut
184  
185 3 ▶sub time_remaining {
Subroutine time_remaining: called CC=2 SCAR=7.2
Pod documented
186 3 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 3 0 3
187 3 $self->{timer} - $self->{clock};
188 }
189  
190 =head2 tick_clock ( $custom_tick )
191  
192 C<$custom_tick> is optional and the default is C<1>.
193  
194 This will cause the timer of the feeder to increase by C<1> (default) or by C<$custom_tick>.
195  
196 =cut
197  
198 10 ▶sub tick_clock {
Subroutine tick_clock: called CC=2 SCAR=7.2
Pod documented
199 10 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 10 0 10
200 10 ++$self->{clock};
201 }
202  
203  
204 =head1 FOOD TANK RELATED SUBROUTINE/METHODS
205  
206 =head2 food_tank_capacity
207  
208 Returns the current food tank capacity.
209  
210 =cut
211  
212 2 ▶sub food_tank_capacity {
Subroutine food_tank_capacity: called CC=2 SCAR=7.2
Pod documented
213 2 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 2 0 2
214 2 $self->{food_tank_capacity};
215 }
216  
217 =head2 set_food_tank_capacity ( $new_capacity )
218  
219 Set the new food tank capacity to C<$new_capacity>.
220  
221 =cut
222  
223 1 ▶sub set_food_tank_capacity {
Subroutine set_food_tank_capacity: called CC=3 SCAR=11.8
Pod documented
224 3 ▶ no warnings "numeric";
Subroutine BEGIN: called CC=1 SCAR=0.0
225 1 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 1 0 1
226 1 ▶ my $new_capacity = int (shift) || return;
Condition50%1/2
int shift() || (return)
l!l
01
MC/DC0%0/1
int shift() || (return)
int shift()
Truth table50%1/2
A
A= int shift()
Aresult
11
00
227 0 $self->{food_tank_capacity} = $new_capacity;
228 }
229  
230 =head2 food_remaining
231  
232 Returns the remaining amount of food left.
233  
234 =cut
235  
236 11 ▶sub food_remaining {
Subroutine food_remaining: called CC=2 SCAR=7.2
Pod documented
237 11 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 11 0 11
238 11 $self->{current_food_amount};
239 }
240  
241 =head1 FEEDING RELATED SUBROUTINES/METHODS
242  
243 =head2 feed_fish ( %options )
244  
245 Feeds the fish.
246  
247 Take note that this will feed the fish no matter what. So it's up to you to make sure that you check if the
248 feeder timer is really up or not before calling this method. See C<timer_is_up> for more info.
249  
250 C<%options> supports the following key:
251  
252 =over 4
253  
254 =item * verbose
255  
256 Setting this to a true value will give output about the feeder's situation when feeding the fish.
257  
258 =back
259  
260 =cut
261  
262 4 ▶sub feed_fish {
Subroutine feed_fish: called CC=4 SCAR=14.0
Pod documented
263 4 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 4 0 4
264 4 my %options = @_;
265 4 ▶ if ( $self->{current_food_amount} - $self->{feeding_volume} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'current_food_amount'} - $self->{'feeding_volume'} <= 0) { } 2 2 4
266 2 ▶ if ( $options{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"verbose"}) 2 0 2
267 2 print "Your feeder has run out of food, please refill as soon as possible.\n";
268 2 print "Only managed to feed $self->{current_food_amount} cm^3 of food to the fish.\n";
269 }
270 2 $self->{current_food_amount} = 0;
271 } else {
272 2 $self->{current_food_amount} = $self->{current_food_amount} - $self->{feeding_volume};
273 }
274 }
275  
276 =head2 set_feeding_volume ( $volume )
277  
278 Sets the fish food feeding volume.
279  
280 C<$volume> must be a positive number. No error checking is done for this yet.
281  
282 =cut
283  
284 2 ▶sub set_feeding_volume {
Subroutine set_feeding_volume: called CC=3 SCAR=11.1
Pod documented
285 2 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 2 0 2
286 2 ▶ my $volume = shift or croak "Please specify feeding volume";
Branch: 1 branch
BranchTrueFalseTotal
my $volume = shift() or croak("Please specify feeding volume") 1 1 2
287 1 $self->{feeding_volume} = $volume;
288 }
289  
290 =head2 feeding_volume
291  
292 Returns the amount of food to feed the fish each time the C<feed_fish> method is called.
293  
294 =cut
295  
296 3 ▶sub feeding_volume {
Subroutine feeding_volume: called CC=2 SCAR=7.2
Pod documented
297 3 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 3 0 3
298 3 $self->{feeding_volume};
299 }
300  
301 =head2 refill ( $volume )
302  
303 Refills the fish food tank B<TO> C<$volume>.
304  
305 If C<$volume> is not specified, the food tank will be filled to max.
306  
307 If C<$volume> is a strange value, it will be ignored and filled to max.
308  
309 =cut
310  
311 5 ▶sub refill {
Subroutine refill: called CC=5 SCAR=16.2
Pod documented
312 3 ▶ no warnings "numeric";
Subroutine BEGIN: called CC=1 SCAR=0.0
313 5 ▶ ref (my $self = shift) or croak "Please use this the OO way";
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or croak("Please use this the OO way") 5 0 5
314  
315 5 ▶ my $volume = shift || $self->{food_tank_capacity};
Condition66%2/3
shift() || $self->{'food_tank_capacity'}
l!l&&r!l&&!r
410
MC/DC0%0/2
shift() || $self->{'food_tank_capacity'}
shift() $self->{'food_tank_capacity'}
Truth table66%2/3
A || B
A= shift()B= $self->{'food_tank_capacity'}
ABresult
1X1
011
000
316 5 ▶ return if not int($volume);
Branch: 1 branch
BranchTrueFalseTotal
unless int $volume 1 4 5
317
318 4 ▶ if ( $volume > $self->{food_tank_capacity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($volume > $self->{'food_tank_capacity'}) { } 1 3 4
319 1 $self->{current_food_amount} = $self->{food_tank_capacity};
320 } else {
321 3 $self->{current_food_amount} = $volume;
322 }
323
324 }
325  
326 =head1 AUTHOR
327  
328 Raphael Jong Jun Jie, C<< <ellednera at cpan.org> >>
329  
330 =head1 BUGS
331  
332 Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
333 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
334 automatically be notified of progress on your bug as I make changes.
335  
336  
337  
338  
339 =head1 SUPPORT
340  
341 You can find documentation for this module with the perldoc command.
342  
343 perldoc Acme::FishFarm::Feeder
344  
345  
346 You can also look for information at:
347  
348 =over 4
349  
350 =item * RT: CPAN's request tracker (report bugs here)
351  
352 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
353  
354 =item * CPAN Ratings
355  
356 L<https://cpanratings.perl.org/d/.>
357  
358 =item * Search CPAN
359  
360 L<https://metacpan.org/release/.>
361  
362 =back
363  
364  
365 =head1 ACKNOWLEDGEMENTS
366  
367 Besiyata d'shmaya
368  
369 =head1 SEE ALSO
370  
371 Acme::FishFarm
372  
373 =head1 LICENSE AND COPYRIGHT
374  
375 This software is Copyright (c) 2021 by Raphael Jong Jun Jie.
376  
377 This is free software, licensed under:
378  
379 The Artistic License 2.0 (GPL Compatible)
380  
381  
382 =cut
383  
384 3 1; # End of Acme::FishFarm::Feeder
« FishFarm/WaterConditionMonitor.pm ↑ Summary FishFarm/WaterFiltration.pm »