File Coverage

blib/lib/App/Math/Tutor/Cmd/VulFrac/Cmd/Cast.pm
Criterion Covered Total %
statement 64 65 98.4
branch 4 6 66.6
condition 2 3 66.6
subroutine 8 9 88.8
pod n/a
total 78 83 93.9


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Cmd::VulFrac::Cmd::Cast;
2              
3 1     1   22807 use warnings;
  1         4  
  1         43  
4 1     1   6 use strict;
  1         2  
  1         44  
5              
6 1     1   6 use vars qw(@ISA $VERSION);
  1         2  
  1         75  
7              
8             =head1 NAME
9              
10             App::Math::Tutor::Cmd::VulFrac::Cmd::Cast - Plugin for casting of vulgar fraction into decimal fraction and vice versa
11              
12             =cut
13              
14             our $VERSION = '0.004';
15              
16 1     1   5 use Moo;
  1         1  
  1         8  
17 1     1   330 use MooX::Cmd;
  1         2  
  1         7  
18 1     1   1737 use MooX::Options;
  1         1  
  1         9  
19              
20             has template_filename => (
21             is => "ro",
22             default => "twocols"
23             );
24              
25             with "App::Math::Tutor::Role::VulFracExercise", "App::Math::Tutor::Role::DecFracExercise";
26              
27             =head1 ATTRIBUTES
28              
29             =head2 chart
30              
31             Enable chart for fraction approximation.
32              
33             Warning: This is experimental and requires LaTeX::Driver 0.20+ and properly working xelatex
34              
35             Default: 0
36              
37             =cut
38              
39             option chart => (
40             is => "ro",
41             doc => "Enable chart for fraction approximation",
42             long_doc => "Enable chart for fraction approximation\n\n"
43             . "Warning: This is experimental and requires LaTeX::Driver 0.20+ and properly working xelatex\n\n"
44             . "Default: 0",
45             default => sub { 0 },
46             negativable => 1,
47             );
48              
49             sub _build_command_names
50             {
51 0     0   0 return qw(cast);
52             }
53              
54             sub _get_castable_numbers
55             {
56 30     30   38 my ( $self, $quantity ) = @_;
57              
58 30         30 my @result;
59 30         56 while ( $quantity-- )
60             {
61 30         28 my $vf;
62             do
63 30   66     25 {
64 195         495 $vf = $self->_guess_vulgar_fraction;
65             } while ( !$self->_check_vulgar_fraction($vf) or !$self->_check_decimal_fraction($vf) );
66              
67 30         104 push @result, $vf;
68             }
69              
70 30         63 return @result;
71             }
72              
73             sub _build_exercises
74             {
75 1     1   28 my ($self) = @_;
76              
77 1         2 my (@tasks);
78 1         6 foreach my $i ( 1 .. $self->quantity )
79             {
80 15         18 my @line;
81 15         25 foreach my $j ( 0 .. 1 )
82             {
83 30         68 my ($a) = $self->_get_castable_numbers(1);
84 30         82 push @line, [$a];
85             }
86 15         37 push @tasks, \@line;
87             }
88              
89 1         12 my $exercises = {
90             section => "Vulgar fraction <-> decimal fracion casting",
91             caption => 'Fractions',
92             label => 'vulgar_decimal_fractions',
93             header => [ [ 'Vulgar => Decimal Fraction', 'Decimal => Vulgar Fraction' ] ],
94             solutions => [],
95             challenges => [],
96             usepackages => [qw(pstricks pstricks-add)],
97             };
98              
99 1         4 my $digits = $self->digits;
100 1         3 foreach my $line (@tasks)
101             {
102 15         16 my ( @solution, @challenge );
103              
104             # cast vulgar fraction to decimal
105 15         13 my ($a) = @{ $line->[0] };
  15         27  
106 15         47 push @challenge, sprintf( '$ %s = $', $a );
107              
108 15         20 my @way; # remember Frank Sinatra :)
109 15         33 push @way, "" . $a;
110 15         42 $a = $a->_reduce;
111 15 100       264 $a->num != $line->[0]->[0]->num and push @way, "" . $a;
112 15         37 my $rd = $digits + length( int($a) ) + 1;
113 15         45 push @way, sprintf( "%0.${rd}g", $a );
114 15 50       42 $self->chart and push @way,
115             sprintf(
116             '\begin{pspicture}(-0.25,-0.25)(0.25,0.25)\psChart[chartColor=color,chartSep=1pt]{%d,%d}{}{0.25}\end{pspicture}',
117             $a->num % $a->denum, $a->denum - ( $a->num % $a->denum ) );
118 15         36 push( @solution, '$ ' . join( " = ", @way ) . ' $' );
119              
120             # cast decimal to vulgar fraction
121 15         22 @way = ();
122 15         14 ($a) = @{ $line->[1] };
  15         31  
123 15         45 $rd = $digits + length( int($a) ) + 1;
124 15         43 push @challenge, sprintf( "\$ %0.${rd}g = \$", $a );
125 15         43 push @way, sprintf( "%0.${rd}g", $a );
126 15         36 $a = $a->_reduce;
127 15         236 push @way, "" . $a;
128 15 50       38 $self->chart and push @way,
129             sprintf(
130             '\begin{pspicture}(-0.25,-0.25)(0.25,0.25)\psChart[chartColor=color,chartSep=1pt]{%d,%d}{}{0.25}\end{pspicture}',
131             $a->num % $a->denum, $a->denum - ( $a->num % $a->denum ) );
132 15         32 push( @solution, '$ ' . join( " = ", @way ) . ' $' );
133              
134 15         16 push( @{ $exercises->{solutions} }, \@solution );
  15         29  
135 15         16 push( @{ $exercises->{challenges} }, \@challenge );
  15         53  
136             }
137              
138 1         26 return $exercises;
139             }
140              
141             =head1 LICENSE AND COPYRIGHT
142              
143             Copyright 2010-2014 Jens Rehsack.
144              
145             This program is free software; you can redistribute it and/or modify it
146             under the terms of either: the GNU General Public License as published
147             by the Free Software Foundation; or the Artistic License.
148              
149             See http://dev.perl.org/licenses/ for more information.
150              
151             =cut
152              
153             1;