File Coverage

blib/lib/App/Math/Tutor/Cmd/Unit/Cmd/Cast.pm
Criterion Covered Total %
statement 75 75 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod n/a
total 89 89 100.0


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Cmd::Unit::Cmd::Cast;
2              
3 1     1   31658 use warnings;
  1         3  
  1         39  
4 1     1   7 use strict;
  1         2  
  1         43  
5              
6 1     1   6 use vars qw(@ISA $VERSION);
  1         3  
  1         75  
7              
8             =head1 NAME
9              
10             App::Math::Tutor::Cmd::Unit::Cmd::Cast - Plugin for casting of numbers with units
11              
12             =cut
13              
14             our $VERSION = '0.004';
15              
16 1     1   6 use Moo;
  1         3  
  1         11  
17 1     1   425 use MooX::Cmd;
  1         2  
  1         12  
18 1     1   2320 use MooX::Options;
  1         3  
  1         10  
19              
20 1     1   1870 use Carp qw(croak);
  1         3  
  1         74  
21 1     1   6 use File::ShareDir ();
  1         2  
  1         17  
22 1     1   6 use Template ();
  1         2  
  1         25  
23 1     1   6 use Scalar::Util qw(looks_like_number);
  1         2  
  1         751  
24              
25             has template_filename => (
26             is => "ro",
27             default => "twocols"
28             );
29              
30             with "App::Math::Tutor::Role::UnitExercise", "App::Math::Tutor::Role::DecFracExercise";
31              
32             sub _get_castable_numbers
33             {
34 30     30   48 my ( $self, $quantity ) = @_;
35              
36 30         30 my @result;
37 30         65 while ( $quantity-- )
38             {
39 30         32 my ( $un, $base, $ut );
40             do
41 30         30 {
42 48         176 ($un) = $self->get_unit_numbers( 1, $ut );
43 48         152 my $rng = $un->end - $un->begin;
44 48         187 $base = int( rand($rng) ) + $un->begin;
45 48 100       233 defined $ut or $ut = $un->type;
46             } while ( !$self->_check_decimal_fraction( $un->_numify($base) ) );
47              
48 30         104 push @result,
49             [
50             $un,
51             Unit->new(
52             parts => [ $un->_numify($base) ],
53             begin => $base,
54             end => $base,
55             type => $un->type
56             )
57             ];
58             }
59              
60 30         1302 return @result;
61             }
62              
63             sub _build_exercises
64             {
65 1     1   39 my ($self) = @_;
66              
67 1         4 my (@tasks);
68 1         8 foreach my $i ( 1 .. $self->quantity )
69             {
70 15         20 my @line;
71 15         29 foreach my $j ( 0 .. 1 )
72             {
73 30         67 push @line, $self->_get_castable_numbers(1);
74             }
75 15         43 push @tasks, \@line;
76             }
77              
78 1         12 my $exercises = {
79             section => "Unit casting",
80             caption => 'Units',
81             label => 'unit_castings',
82             header => [ [ 'Multiple entity => Single entity', 'Single entity => Multiple entity' ] ],
83             solutions => [],
84             challenges => [],
85             };
86              
87 1         5 my $digits = $self->digits;
88 1         4 foreach my $line (@tasks)
89             {
90 15         18 my ( @solution, @challenge );
91             # cast unit with multiple entities to unit with one entity
92 15         14 my ( $unit, $based ) = @{ $line->[0] };
  15         32  
93 15         39 my $base = $based->begin;
94 15         42 my $base_name = $unit->type->{spectrum}->[$base]->{unit};
95 15         126 push @challenge,
96             sprintf( '$ %s = %s\\text{%s} $',
97             $unit, "\\ldots" x int( length($based) / 3 ), $base_name );
98              
99 15         22 my @way; # remember Frank Sinatra :)
100 15         58 push @way, "" . $unit;
101 15         41 push @way, "" . $based;
102 15         47 push( @solution, '$ ' . join( " = ", @way ) . ' $' );
103              
104             # cast decimal to vulgar fraction
105 15         22 @way = ();
106 15         16 ( $unit, $based ) = @{ $line->[1] };
  15         35  
107 15         45 $base_name = $unit->type->{spectrum}->[$base]->{unit};
108 15         27 $base = $based->begin;
109 15         38 push @challenge, sprintf( '$ %s = %s $', $based, "\\ldots" x int( length($unit) / 3 ) );
110 15         44 push @way, "" . $based;
111 15         43 push @way, "" . $unit;
112 15         50 push( @solution, '$ ' . join( " = ", @way ) . ' $' );
113              
114 15         17 push( @{ $exercises->{solutions} }, \@solution );
  15         40  
115 15         17 push( @{ $exercises->{challenges} }, \@challenge );
  15         58  
116             }
117              
118 1         57 return $exercises;
119              
120             }
121              
122             =head1 LICENSE AND COPYRIGHT
123              
124             Copyright 2010-2014 Jens Rehsack.
125              
126             This program is free software; you can redistribute it and/or modify it
127             under the terms of either: the GNU General Public License as published
128             by the Free Software Foundation; or the Artistic License.
129              
130             See http://dev.perl.org/licenses/ for more information.
131              
132             =cut
133              
134             1;