File Coverage

blib/lib/App/Math/Tutor/Role/Exercise.pm
Criterion Covered Total %
statement 39 42 92.8
branch 3 4 75.0
condition n/a
subroutine 11 13 84.6
pod 0 1 0.0
total 53 60 88.3


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Role::Exercise;
2              
3 1     1   598 use warnings;
  1         2  
  1         22  
4 1     1   4 use strict;
  1         1  
  1         22  
5              
6             =head1 NAME
7              
8             App::Math::Tutor::Role::Exercise - basic role for getting exercises
9              
10             =cut
11              
12 1     1   4 use Moo::Role;
  1         1  
  1         5  
13 1     1   262 use MooX::Options;
  1         1  
  1         6  
14              
15 1     1   650 use Carp qw(croak);
  1         2  
  1         64  
16 1     1   5 use Cwd qw(abs_path getcwd);
  1         1  
  1         49  
17 1     1   4 use File::Spec ();
  1         2  
  1         21  
18 1     1   1050 use File::ShareDir ();
  1         9179  
  1         26  
19 1     1   11341 use Template ();
  1         35519  
  1         462  
20              
21             our $VERSION = '0.004';
22              
23             =head1 ATTRIBUTES
24              
25             =head2 quantity
26              
27             Specifies number of calculations to generate
28              
29             =cut
30              
31             option quantity => (
32             is => "ro",
33             doc => "Specifies number of exercises to generate",
34             long_doc => "Specify number of exercises to generate. In "
35             . "case of several kind of exercises, \$quantity exercises "
36             . "are generated per kind.",
37             format => "i",
38             short => "n",
39             default => sub { 15 },
40             );
41              
42             =head2 exercises
43              
44             Lazy hash containing the exercises to fill into the template.
45              
46             Expected attribute:
47              
48             =over 4
49              
50             =item section
51              
52             The caption of the section containing the challenges. The solutions section
53             will reuse the section caption prepended by I<Solution:>.
54              
55             =item caption
56              
57             Table caption for challenges table. The solutions table will be prepended
58             by the word I<Solution>.
59              
60             =item label
61              
62             Label of the table containing the challenges - the solutions table will
63             be the given label prepended by I<solution>.
64              
65             =item header
66              
67             List of table headers - one header per column
68              
69             =item challenges
70              
71             List of challenges to exercise
72              
73             =item solutions
74              
75             List of solutions of challenges
76              
77             =item usepackages
78              
79             List of additional package names to pass to \usepackage
80              
81             =item headeritems
82              
83             List of additional lines to put to document header
84              
85             =back
86              
87             =cut
88              
89             has exercises => (
90             is => "lazy",
91             # requires _build_exercises
92             );
93              
94             =head2 output_name
95              
96             Lazy string representing the basename without extension of the output
97             file. The default builder returns the names of all commands in chain
98             joined with empty string.
99              
100             =cut
101              
102             has output_name => (
103             is => "lazy",
104             );
105              
106             sub _build_output_name
107             {
108 10     10   2417 my $self = shift;
109              
110 10 100       23 my $cmdnames = join( "", map { $_->command_name || "" } @{ $self->command_chain } );
  30         200  
  10         70  
111              
112 10         77 return $cmdnames;
113             }
114              
115             =head2 output_type
116              
117             Lazy string representing the extension of the output file. The default
118             builder returns 'pdf'. Permitted to be set via MooX::Options.
119              
120             =cut
121              
122             option output_type => (
123             is => "lazy",
124             doc => "Specifies the output type (tex, pdf, ps)",
125             format => "s",
126             short => "t",
127             );
128              
129 0     0   0 sub _build_output_type { 'pdf' }
130              
131             =head2 output_location
132              
133             Lazy string representing the location of the output file in file system. The
134             default builder returns the full qualified path name to the current working
135             directory. Permitted to be set via MooX::Options.
136              
137             =cut
138              
139             option output_location => (
140             is => "lazy",
141             doc => "Specifies the output location",
142             format => "s",
143             short => "o",
144             );
145              
146 0     0   0 sub _build_output_location { abs_path(getcwd) }
147              
148             =head1 REQUIRED ATTRIBUTES
149              
150             =head2 template_filename
151              
152             The basename of the template file for processing to get the exercises.
153              
154             =cut
155              
156             requires "template_filename";
157              
158             sub execute
159             {
160 10     10 0 2271 my $self = shift;
161              
162 10         51 my $exercises = $self->exercises;
163              
164 10         82 my $sharedir = File::ShareDir::dist_dir("App-Math-Tutor");
165 10         1945 my $ttcpath = File::Spec->catfile( $sharedir, $self->template_filename . ".tt2" );
166              
167 10         173 my $template = Template->new(
168             {
169             ABSOLUTE => 1,
170             }
171             );
172 10         33525 my $rc = $template->process(
173             $ttcpath,
174             {
175             exercises => $exercises,
176             output => {
177             format => $self->output_type,
178             },
179             },
180             File::Spec->catfile(
181             $self->output_location,
182             join( ".", $self->output_name, $self->output_type )
183             ),
184             );
185 10 50       1715 $rc or croak( $template->error() );
186              
187 0           return 0;
188             }
189              
190             =head1 LICENSE AND COPYRIGHT
191              
192             Copyright 2010-2014 Jens Rehsack.
193              
194             This program is free software; you can redistribute it and/or modify it
195             under the terms of either: the GNU General Public License as published
196             by the Free Software Foundation; or the Artistic License.
197              
198             See http://dev.perl.org/licenses/ for more information.
199              
200             =cut
201              
202             1;