File Coverage

blib/lib/Acme/FishFarm/WaterLevelMaintainer.pm
Criterion Covered Total %
statement 39 39 100.0
branch 14 24 58.3
condition n/a
subroutine 12 12 100.0
pod 8 8 100.0
total 73 83 87.9


line stmt bran cond sub pod time code
1             package Acme::FishFarm::WaterLevelMaintainer;
2              
3 3     3   68295 use 5.006;
  3         20  
4 3     3   17 use strict;
  3         15  
  3         92  
5 3     3   24 use warnings;
  3         6  
  3         127  
6 3     3   22 use Carp "croak";
  3         7  
  3         1576  
7              
8             =head1 NAME
9              
10             Acme::FishFarm::WaterLevelMaintainer - Water Level Maintainer for Acme::FishFarm
11              
12             =head1 VERSION
13              
14             Version 1.01
15              
16             =cut
17              
18             our $VERSION = '1.01';
19              
20              
21             =head1 SYNOPSIS
22              
23             use 5.010;
24              
25             use Acme::FishFarm qw( reduce_precision );
26             use Acme::FishFarm::WaterFiltration;
27              
28             my $water_level = Acme::FishFarm::WaterLevelMaintainer->install;
29              
30             say "Water level maintainer installed and switched on!\n";
31              
32             my $height_increase;
33             my $water_level_threshold;
34             my $current_reading;
35              
36             while ( "Fish are living under the water..." ) {
37              
38             $current_reading = reduce_precision( rand(10) );
39             $height_increase = $water_level->water_level_increase_height;
40             $water_level_threshold = $water_level->low_water_level_threshold;
41            
42             $water_level->current_water_level( $current_reading ); # input by user
43             print "Current Water Level: ", $current_reading, " m (low: < ", $water_level_threshold, " m)\n";
44              
45             if ( $water_level->is_low_water_level ) {
46             print " !! Water level is low!\n";
47             $water_level->pump_water_in;
48             print " Pumping in ", $height_increase, " m of water...\n";
49             print "Current Water Level: ", $water_level->current_water_level, "\n";
50             } else {
51             print " Water level is still normal.\n";
52             }
53            
54             sleep(1);
55             say "";
56             }
57              
58             =head1 EXPORT
59              
60             None
61              
62             =head1 CREATION RELATED MEHODS
63              
64             =head2 install ( %options )
65              
66             Installs a water level maintainer system. This system only pumps water in if the water level is lower than the threshold value.
67              
68             The supported C<%options> are:
69              
70             =over 4
71              
72             =item current_water_level
73              
74             The default water level is to C<5 unit>.
75              
76             =item low_water_level_threshold
77              
78             The default threshold is C<2 unit>.
79              
80             If the current water level is lower than this threshold, then you need to pump water into the tank.
81              
82             =item increase_water_level_by
83              
84             This is the height of the water level to increase when the water is pumped in.
85              
86             The default value is C<0.5 unit>.
87              
88             =back
89              
90             =cut
91              
92             sub install {
93 2     2 1 427 my $class = shift;
94 2         5 my %options = @_;
95            
96 2 50       22 if ( not $options{current_water_level} ) {
97 2         6 $options{current_water_level} = 5;
98             }
99            
100 2 50       9 if ( not $options{low_water_level_threshold} ) {
101 2         4 $options{low_water_level_threshold} = 2;
102             }
103            
104 2 50       47 if ( not $options{increase_water_level_by} ) {
105 2         7 $options{increase_water_level_by} = 0.5;
106             }
107            
108 2         4 $options{is_low_water_level} = 0; # might be useless :)
109            
110 2         9 bless \%options, "Acme::FishFarm::WaterLevelMaintainer";
111             }
112              
113             =head1 WATER LEVEL DETECTION RELATED METHODS
114              
115             =head2 current_water_level ( $new_water_level )
116              
117             Sets / returns the current water level of the water.
118              
119             C<$new_water_level> is optional. If present, the current water level will be set to C<$new_water_level>. Otherwise, returns the current water level (depth).
120              
121             =cut
122              
123             sub current_water_level {
124 7 50   7 1 27 ref( my $self = shift ) or croak "Please use this the OO way";
125 7 100       20 if ( @_ ) {
126 3         8 $self->{current_water_level} = shift;
127             } else {
128 4         28 $self->{current_water_level};
129             }
130             }
131              
132             =head2 low_water_level_threshold
133              
134             Returns the low water level threshold.
135              
136             =cut
137              
138             sub low_water_level_threshold {
139 3 50   3 1 12 ref( my $self = shift ) or croak "Please use this the OO way";
140 3         11 $self->{low_water_level_threshold};
141             }
142              
143             =head2 set_low_water_level_threshold ( $new_threshold )
144              
145             Sets the low water level threshold.
146              
147             =cut
148              
149             sub set_low_water_level_threshold {
150 1 50   1 1 20 ref( my $self = shift ) or croak "Please use this the OO way";
151 1         6 $self->{low_water_level_threshold} = shift;
152             }
153              
154             =head2 is_low_water_level
155              
156             Returns C<1> if the DO level is less than the threshold value. Otherwise, returns C<0>.
157              
158             =cut
159              
160             sub is_low_water_level {
161 3 50   3 1 12 ref( my $self = shift ) or croak "Please use this the OO way";
162 3 100       21 if ( $self->{current_water_level} < $self->{low_water_level_threshold} ) {
163 2         10 return 1;
164             } else {
165 1         5 return 0;
166             }
167             }
168              
169             =head1 PUMPS RELATED METHODS
170              
171             For the pumping mechanism, just assume that the pumps can actually pump in certain metres of water each time :)
172              
173             =head2 water_level_increase_height
174              
175             Returns the height of water level to increase each water pumping action. See C below.
176              
177             =cut
178              
179             sub water_level_increase_height {
180 3 50   3 1 24 ref( my $self = shift ) or croak "Please use this the OO way";
181 3         18 $self->{increase_water_level_by};
182             }
183              
184             =head2 set_water_level_increase_height ( $new_height )
185              
186             Sets the height of water level to increase to C<$new_height>.
187              
188             =cut
189              
190             sub set_water_level_increase_height {
191 1 50   1 1 4 ref( my $self = shift ) or croak "Please use this the OO way";
192 1         2 $self->{increase_water_level_by} = shift;
193             }
194              
195             =head2 pump_water_in
196              
197             Pumps water into the tank to increase the height of the water level.
198              
199             =cut
200              
201             sub pump_water_in {
202 2 50   2 1 10 ref( my $self = shift ) or croak "Please use this the OO way";
203 2         10 $self->{current_water_level} += $self->{increase_water_level_by};
204             }
205              
206             =head1 AUTHOR
207              
208             Raphael Jong Jun Jie, C<< >>
209              
210             =head1 BUGS
211              
212             Please report any bugs or feature requests to C, or through
213             the web interface at L. I will be notified, and then you'll
214             automatically be notified of progress on your bug as I make changes.
215              
216              
217              
218              
219             =head1 SUPPORT
220              
221             You can find documentation for this module with the perldoc command.
222              
223             perldoc Acme::FishFarm::WaterLevelMaintainer
224              
225              
226             You can also look for information at:
227              
228             =over 4
229              
230             =item * RT: CPAN's request tracker (report bugs here)
231              
232             L
233              
234             =item * CPAN Ratings
235              
236             L
237              
238             =item * Search CPAN
239              
240             L
241              
242             =back
243              
244              
245             =head1 ACKNOWLEDGEMENTS
246              
247             Besiyata d'shmaya
248              
249             =head1 LICENSE AND COPYRIGHT
250              
251             This software is Copyright (c) 2021 by Raphael Jong Jun Jie.
252              
253             This is free software, licensed under:
254              
255             The Artistic License 2.0 (GPL Compatible)
256              
257              
258             =cut
259              
260             1; # End of Acme::FishFarm::WaterLevelMaintainer