File Coverage

blib/lib/App/Math/Tutor/Cmd/Unit/Cmd/Add.pm
Criterion Covered Total %
statement 69 69 100.0
branch 22 22 100.0
condition 12 12 100.0
subroutine 7 7 100.0
pod n/a
total 110 110 100.0


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Cmd::Unit::Cmd::Add;
2              
3 1     1   6518 use warnings;
  1         2  
  1         46  
4 1     1   6 use strict;
  1         2  
  1         46  
5              
6 1     1   5 use vars qw(@ISA $VERSION);
  1         1  
  1         84  
7              
8             =head1 NAME
9              
10             App::Math::Tutor::Cmd::Unit::Cmd::Add - Plugin for addition and subtraction of numbers with units
11              
12             =cut
13              
14             our $VERSION = '0.004';
15              
16 1     1   5 use Moo;
  1         2  
  1         9  
17 1     1   415 use MooX::Cmd;
  1         3  
  1         11  
18 1     1   2383 use MooX::Options;
  1         3  
  1         9  
19              
20             has template_filename => (
21             is => "ro",
22             default => "twocols"
23             );
24              
25             with "App::Math::Tutor::Role::UnitExercise";
26              
27             sub _build_exercises
28             {
29 1     1   43 my ($self) = @_;
30              
31 1         2 my (@tasks);
32              
33 1         8 foreach my $i ( 1 .. $self->quantity )
34             {
35 15         22 my @line;
36 15         27 foreach my $j ( 0 .. 1 )
37             {
38 30         92 my ( $a, $b ) = $self->get_unit_numbers(2);
39 30         96 push @line, [ $a, $b ];
40             }
41 15         35 push @tasks, \@line;
42             }
43              
44 1         14 my $exercises = {
45             section => "Unit addition / subtraction",
46             caption => 'Units',
47             label => 'unit_addition',
48             header => [ [ 'Unit Addition', 'Unit Subtraction' ] ],
49             solutions => [],
50             challenges => [],
51             };
52              
53 1         4 foreach my $line (@tasks)
54             {
55 15         17 my ( @solution, @challenge );
56              
57 15         22 foreach my $i ( 0 .. 1 )
58             {
59 30         34 my ( $a, $b ) = @{ $line->[$i] };
  30         63  
60 30 100       63 my $op = $i ? '-' : '+';
61 30 100 100     113 $op eq '-' and $a < $b and ( $b, $a ) = ( $a, $b );
62 30         101 push( @challenge, "\$ $a $op $b = \$" );
63              
64 30         40 my @way; # remember Frank Sinatra :)
65 30         74 push( @way, "$a $op $b" );
66 30 100       122 my $beg = $a->begin < $b->begin ? $a->begin : $b->begin;
67 30 100       72 my $end = $a->end > $b->end ? $a->end : $b->end;
68 30         29 my @ap = @{ $a->parts };
  30         63  
69 30         30 my @bp = @{ $b->parts };
  30         55  
70 30         33 my ( @cparts, @dparts );
71 30         53 for my $i ( $beg .. $end )
72             {
73 87         87 my @cps;
74 87 100 100     466 $i >= $a->begin and $i <= $a->end and push( @cps, shift @ap );
75 87 100 100     363 $i >= $b->begin and $i <= $b->end and push( @cps, shift @bp );
76 87 100       176 scalar @cps or next;
77 86         220 my $cp = join( " $op ", @cps );
78 86         4037 my $dp = eval "$cp;";
79 86 100 100     612 if ( $dp < 0 )
    100          
80             {
81 9         13 --$dparts[-1];
82 9         28 $dp += $a->type->{spectrum}->[$i]->{max} + 1;
83             }
84             elsif ( defined $a->type->{spectrum}->[$i]->{max}
85             and $dp > $a->type->{spectrum}->[$i]->{max} )
86             {
87 4 100       11 @dparts and ++$dparts[-1];
88 4 100       11 @dparts or push @dparts, 1;
89 4         16 $dp -= $a->type->{spectrum}->[$i]->{max} + 1;
90             }
91 86         143 push( @cparts, $cp );
92 86         198 push( @dparts, $dp );
93             }
94 30         804 my $c = Unit->new(
95             type => $a->type,
96             begin => $beg,
97             end => $end,
98             parts => \@cparts
99             );
100 30         1543 my $d = Unit->new(
101             type => $a->type,
102             begin => $beg - ( scalar @cparts - scalar @dparts ),
103             end => $end,
104             parts => \@dparts
105             );
106              
107 30         757 push( @way, "$c" );
108 30         83 push( @way, "$d" );
109              
110 30         216 push( @solution, '$ ' . join( " = ", @way ) . ' $' );
111             }
112              
113 15         23 push( @{ $exercises->{solutions} }, \@solution );
  15         36  
114 15         20 push( @{ $exercises->{challenges} }, \@challenge );
  15         41  
115             }
116              
117 1         57 return $exercises;
118             }
119              
120             =head1 LICENSE AND COPYRIGHT
121              
122             Copyright 2010-2014 Jens Rehsack.
123              
124             This program is free software; you can redistribute it and/or modify it
125             under the terms of either: the GNU General Public License as published
126             by the Free Software Foundation; or the Artistic License.
127              
128             See http://dev.perl.org/licenses/ for more information.
129              
130             =cut
131              
132             1;