File Coverage

blib/lib/Repl/Spec/Type/BooleanType.pm
Criterion Covered Total %
statement 18 19 94.7
branch 3 4 75.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 3 3 100.0
total 31 35 88.5


line stmt bran cond sub pod time code
1             =head1 NAME
2            
3             Repl::Spec::Type::BooleanType - A parameter guard for boolean values.
4            
5             =head1 SYNOPSIS
6            
7             This type guard ensures that a boolean parameter was passed by the user.
8             The guard converts truthy values to 1 and falsy values to 0.
9            
10             Truthy values are "true", "ok", "on", "yes".
11            
12             Falsy values are "false", "nok", "off", "no".
13            
14             =head1 DESCRIPTION
15            
16             =head1 Methods
17            
18             =over 4
19            
20             =item C
21            
22             =item C
23            
24             Parameters: A single expression.
25             Returns: 0 or 1.
26            
27             =item C
28            
29             =head1 SEE ALSO
30            
31             L
32             L
33             L
34             L
35             L
36             L
37             L
38             L
39            
40             =cut
41            
42             package Repl::Spec::Type::BooleanType;
43            
44 2     2   2195 use strict;
  2         5  
  2         68  
45 2     2   135 use warnings;
  2         4  
  2         56  
46 2     2   10 use Carp;
  2         4  
  2         826  
47            
48             # No parameters.
49             sub new
50             {
51 2     2 1 13 my $invocant = shift;
52 2   33     13 my $class = ref($invocant) || $invocant;
53 2         6 my $self = {};
54 2         63 return bless $self, $class;
55             }
56            
57             sub guard
58             {
59 4     4 1 6 my $self = shift;
60 4         7 my $arg = shift;
61            
62 4 100       40 return 1 if $arg =~ /true|yes|on|ok|1|t/i;
63 2 50       16 return 0 if $arg =~ /false|no|off|nok|0|f/i;
64 0         0 croak sprintf("Expected type boolean but received '%s'.", $arg);
65             }
66            
67             sub name
68             {
69 1     1 1 4 return 'boolean';
70             }
71            
72             1;