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