File Coverage

blib/lib/App/Math/Tutor/Role/NaturalExercise.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Role::NaturalExercise;
2              
3 1     1   487 use warnings;
  1         2  
  1         29  
4 1     1   5 use strict;
  1         1  
  1         22  
5              
6             =head1 NAME
7              
8             App::Math::Tutor::Role::NaturalExercise - role for for exercises with natural numbers
9              
10             =cut
11              
12 1     1   3 use Moo::Role;
  1         1  
  1         7  
13 1     1   259 use MooX::Options;
  1         1  
  1         5  
14              
15             with "App::Math::Tutor::Role::Exercise", "App::Math::Tutor::Role::Natural";
16              
17 1     1   462 use Scalar::Util qw/looks_like_number/;
  1         2  
  1         322  
18              
19             our $VERSION = '0.005';
20              
21             =head1 ATTRIBUTES
22              
23             =head2 format
24              
25             Specifies format of operand
26              
27             =cut
28              
29             option format => (
30             is => "ro",
31             doc => "specifies format of natural number",
32             long_doc => "Allow specifying the format of the natural number "
33             . "whereby any digit is typed with 'n' as placeholder:\n\n"
34             . "\t--format 5nnn\n\n"
35             . "creates natural numbers from 0002 .. 5999.\n\n"
36             . "Default: 100",
37             isa => sub {
38             defined( $_[0] )
39             and !looks_like_number( $_[0] )
40             and $_[0] !~ m,^\d?n+?$,
41             and die("Invalid format");
42             },
43             coerce => sub {
44             defined( $_[0] ) or return 100;
45             looks_like_number( $_[0] ) and int( $_[0] ) == $_[0] and return $_[0];
46              
47             my ($fmtv) = ( $_[0] =~ m,^(\d?n+)?$, );
48             my $startv = "1";
49             $fmtv =~ s/^(\d)(.*)/$2/ and $startv = $1;
50             my $maxv = $startv . "0" x length($fmtv);
51             $maxv;
52             },
53             default => sub { 100 },
54             format => "s",
55             short => "f",
56             );
57              
58             =head1 LICENSE AND COPYRIGHT
59              
60             Copyright 2010-2014 Jens Rehsack.
61              
62             This program is free software; you can redistribute it and/or modify it
63             under the terms of either: the GNU General Public License as published
64             by the Free Software Foundation; or the Artistic License.
65              
66             See http://dev.perl.org/licenses/ for more information.
67              
68             =cut
69              
70             1;