blib/lib/QWizard/API.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 82 CC 2 SCAR 9.3
CC
2
Cov
0%
_qw_choices
6.0
qw_primary
2.0
_qw_textf
2.0
CRAP 2.5
1 package QWizard::API;
2  
3 use strict;
4  
5 our $VERSION = '3.15';
6 require Exporter;
7  
8 our @ISA = qw(Exporter);
9 our @EXPORT = qw(qw_hidden qw_primary qw_text qw_textbox qw_checkbox qw_radio
10 qw_menu qw_label qw_paragraph qw_button);
11  
12 ###########################################################################
13 # primary
14 sub qw_primary {
15 my $name = shift;
16 my $title = shift;
17 my $introduction = shift;
18 my $questions = shift;
19 my $post_answers = shift;
20 my $actions = shift;
21  
22 return {
23 name => $name,
24 title => $title,
25 introduction => $introduction,
26 questions => $questions,
27 post_answers => $post_answers,
28 actions => $actions,
29 @_
30 };
31 }
32  
33 ###########################################################################
34 # Questions
35  
36 # internal: qtype, name, text
37 sub _qw_textf {
38 my $type = shift;
39 my $name = shift;
40 my $text = shift;
41 return { type => $type,
42 name => $name,
43 text => $text,
44 @_
45 };
46 }
47  
48 # name, text
49 sub qw_text {
50 return _qw_textf('text',@_)
51 }
52  
53 # name, text
54 sub qw_textbox {
55 return _qw_textf('textbox',@_)
56 }
57  
58 sub qw_label {
59 my $text = shift;
60 my $right = shift;
61  
62 return { type => 'label',
63 text => $text,
64 values => [$right],
65 @_};
66 }
67  
68 sub qw_paragraph {
69 my $text = shift;
70 my $right = shift;
71  
72 return { type => 'paragraph',
73 text => $text,
74 values => [$right],
75 @_};
76 }
77  
78  
79 # name, text, onval, offval
80 sub qw_checkbox {
81 my $name = shift;
82 my $text = shift;
83 my $onval = shift || 1;
84 my $offval = shift || 0;
85 return { type => 'checkbox',
86 name => $name,
87 text => $text,
88 values => [$onval, $offval],
89 @_
90 };
91 }
92  
93 # internal: qtype, name, text, [values] | { labels }
94 sub _qw_choices {
95 my $qtype = shift;
96 my $name = shift;
97 my $text = shift;
98 my $vals = shift;
99 my $type = 'labels';
100 if (ref($vals) eq 'ARRAY') {
101 $type = 'values';
102 }
103 return { type => $qtype,
104 name => $name,
105 text => $text,
106 $type => $vals,
107 @_
108 };
109 }
110  
111 # name, text, [values] | { labels }
112 sub qw_radio {
113 return _qw_choices('radio',@_)
114 }
115  
116 # name, text, [values] | { labels }
117 sub qw_menu {
118 return _qw_choices('menu',@_)
119 }
120  
121 # name, value
122 sub qw_hidden {
123 return {type => 'hidden', name => $_[0], values => $_[1], @_};
124 }
125  
126 # name, text, clickedvalue, buttontext
127 sub qw_button {
128 return {type => 'button', name => $_[0], text => $_[1],
129 default => $_[2], values => $_[3],
130 @_};
131 }
132  
133 1;
134 =head1 NAME
135  
136 QWizard::API - Generate questions using an API.
137  
138 =head1 SYNOPSIS
139  
140 use QWizard::API;
141  
142 my $primaries =
143 (
144 qw_primary('aprim', 'My title', 'An introduction',
145 [ qw_text('textresult', 'Enter something'),
146 qw_checkbox('checkresult', 'display results?', 1, 0)]);
147 # ...
148 );
149  
150 =head1 DESCRIPTION
151  
152 QWizard::API is a wrapper around generating questions for use within a
153 QWizard question set. Functionally, the only reason for doing this is
154 to use an API instead of hand-encoding HASH and ARRAY structures. The
155 result might be slightly less verbose, however, since the tags can be
156 left off (I.E, name => 'something' becomes just 'something').
157  
158 =head1 API
159  
160 All of the APIs mentioned here take additional arguments at the end
161 which can be other hash pairs passed to the created objects beyond the
162 defaults that the APIs create.
163  
164 =head2 Primary creation
165 To create a primary:
166 qw_primary(name, title, introduction, [questions], [post_answers], [actions])
167  
168 =head2 Widget creation
169  
170 Text entry:
171 qw_text(name, question text);
172 qw_textbox(name, question text);
173  
174 Chekboxes:
175 qw_checkbox(name, question text, optional:onval, optional:offval);
176  
177 Menus/radios:
178 qw_menu(name, question text, [values] | {labels});
179 qw_radio(name, question text, [values] | {labels});
180  
181 Labels:
182 qw_label(lefttext, righttext);
183 qw_paragraph(lefttext, rightparagraph);
184  
185 Hidden vars:
186 qw_hidden(name, value);
187  
188 Buttons:
189 qw_button(name, question text, clicked value, button label);
190  
191  
192 =head1 TODO
193  
194 OO interface.
195  
196 =head1 EXPORT
197  
198 qw_primary
199 qw_text
200 qw_textbox
201 qw_checkbox
202 qw_radio
203 qw_menu
204  
205 =head1 AUTHOR
206  
207 Wes Hardaker <hardaker@tislabs.com>
208  
209 =head1 SEE ALSO
210  
211 perl(1).
212  
213 =cut