blib/lib/Text/UberText/Dispatch.pm

filter: Statementstmt 13.9% 11 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 60.0% 3 / 5 totaltotal 13.0% 17 / 130 CC 18 SCAR 37.0
CC
18
Cov
10%
extend
90.0
involke
30.0
main
12.0
CRAP 40.3
« blib/lib/Text/UberText/Tree.pm ↑ Summary blib/lib/Text/UberText/Node.pm »
1 #
2 # Package Definition
3 #
4  
5 package Text::UberText::Dispatch;
6  
7 #
8 # Compiler Directives
9 #
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #
15 # Global Variables
16 #
17  
18 2 ▶use vars qw/$VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 $VERSION=0.95;
21  
22 #
23 # Methods
24 #
25  
26 sub new
27 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($class)=shift;
29 0 my ($object);
30 0 $object={};
31 0 bless ($object,$class);
32 0 $object->_init(@_);
33 0 return $object;
34 }
35  
36 sub main
37 0 ▶{
Subroutine main: not called CC=3 SCAR=24.8
Pod undocumented
38 0 my ($self)=shift;
39 0 ▶if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
40 {
41 0 $self->{main}=shift;
42 }
43 0 ▶if ($self->{main})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"main"}) 0 0 0
44 {
45 0 $self->{log}=$self->{main}->log();
46 0 print("Dispatch log set to $self->{log}\n");
47 }
48 0 return $self->{main};
49 }
50  
51 sub extend
52 0 ▶{
Subroutine extend: not called CC=9 SCAR=45.0
Pod documented
53 0 my ($self)=shift;
54 0 ▶if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
55 {
56 0 my ($object,$space)=@_;
57 0 my ($newobj,$defspace,$table);
58 # Determine if $object is a package name or a class
59 0 ▶ unless (ref($object))
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $object) 0 0 0
60 {
61 0 ▶ unless ($object->VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if ($object->VERSION) { } 0 0 0
62 {
63 0 eval " require $object; ";
64 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
65 {
66 0 $self->{log}->write("Dispatch",
67 "Import of $object failed ($@)",
68 undef,"ERROR");
69 0 return;
70 }
71 } else {
72 0 $self->{log}->write("Dispatch",
73 "Object $object already imported",
74 undef,"NOTICE");
75 }
76 }
77 # Run the "uberText" method to get data on recommended namespace,
78 # dispatch table, and object to run methods against
79 0 eval { $object->uberText() };
80 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
81 {
82 0 $self->{log}->write("Dispatch",
83 "Object $object does not support uberText method",
84 undef,"ERROR");
85 0 return;
86 } else {
87 0 ($newobj,$defspace,$table)=$object->uberText();
88 0 ▶ $space=$defspace unless ($space);
Branch: 1 branch
BranchTrueFalseTotal
unless $space 0 0 0
89 0 $self->{log}->write("Dispatch","Object $object loaded",
90 undef,"NOTICE");
91 }
92 0 ▶ unless ($newobj->VERSION)
Branch: 1 branch
BranchTrueFalseTotal
unless ($newobj->VERSION) 0 0 0
93 {
94 0 $self->{log}->write("Dispatch",
95 "Object $newobj did not return VERSION",
96 undef,"NOTICE");
97 # Either there's no VERSION, or this object
98 # isn't working
99 }
100 0 ▶ if ($self->{table}->{$space})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'table'}{$space}) { } 0 0 0
101 {
102 # Something else is already using this space
103 0 $self->{log}->write("Dispatch",
104 "Another object is already using namespace $space",
105 undef,"ERROR");
106 0 return undef;
107 } else {
108 0 $self->{log}->write("Dispatch",
109 "Object loaded in dispatch table",undef,"DEBUG");
110 0 $self->{table}->{$space}->{object}=$newobj;
111 0 $self->{table}->{$space}->{dispatch}=$table;
112 }
113 }
114 0 return;
115 }
116  
117 sub involke
118 0 ▶{
Subroutine involke: not called CC=5 SCAR=34.0
Pod documented
119 0 my ($self)=shift;
120 0 ▶if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
121 {
122 0 my ($node,$namespace,$command,$object,$method,$output);
123 0 $node=shift;
124 0 $namespace=$node->namespace();
125 0 ($command)=$node->command();
126 0 ▶ $command="_default" unless ($command);
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 0 0
127 0 $object=$self->{table}->{$namespace}->{object};
128 0 $method=$self->{table}->{$namespace}->{dispatch}->{$command};
129 0 ▶ if ($object && $method)
Branch: 1 branch
BranchTrueFalseTotal
if ($object and $method) { } 0 0 0
Condition0%0/3
$object and $method
!ll&&!rl&&r
000
MC/DC0%0/2
$object and $method
$object $method
Truth table0%0/3
A and B
A= $objectB= $method
ABresult
0X0
100
111
130 {
131 0 $output=$object->$method($node);
132 0 return $output;
133 } else {
134 0 $self->{log}->write("Dispatch",
135 "No object or method available for $namespace/$command",
136 undef,"ERROR");
137 }
138 }
139 0 return;
140 }
141  
142 sub fetch
143 0 ▶{
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
144 0 my ($self)=shift;
145 0 ▶if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
146 {
147 0 my ($namespace)=shift;
148 0 return $self->{table}->{$namespace}->{object};
149 }
150 0 return;
151 }
152  
153 #
154 # Hidden Methods
155 #
156  
157 sub _init
158 0 ▶{
Subroutine _init: not called CC=3 SCAR=24.8
159 0 my ($self)=shift;
160 0 my ($a);
161 0 ▶while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
162 {
163 0 ($a)=shift;
164 0 ▶ if ($a eq "-main")
Branch: 1 branch
BranchTrueFalseTotal
if ($a eq "-main") 0 0 0
165 {
166 0 $self->main(shift);
167 }
168 }
169 0 $self->{table}={};
170 0 return;
171 }
172  
173  
174 #
175 # Exit Block
176 #
177 2 1;
178  
179 #
180 # POD Documentation
181 #
182  
183 =head1 NAME
184  
185 Text::UberText::Dispatch - UberText Code Dispatcher
186  
187 =head1 DESCRIPTION
188  
189 Text::UberText::Dispatch keeps track of loaded code modules that extend
190 the UberText template language. A Dispatch object is automatically
191 created for new Text::UberText objects.
192  
193 =head1 EXTENDING UBERTEXT
194  
195 If you write a module that integrates with an UberText template, the UberText
196 object needs to be aware of it.
197  
198 $uber=Text::UberText->new();
199  
200 $uber->extend($myObject);
201  
202 $uber->extend(MyClass);
203  
204 The UberText module passes the object or class name to the
205 Text::UberText::Dispatch object. The Dispatch object then calls the
206 C<uberText> method of the module it was passed.
207  
208 The C<uberText> method will need to return 3 variables. The first is the
209 object that the dispatch table will need to use when it encounters
210 your custom namesapce. The second variable is the preferred namespace
211 the object will use, and the third is an anonymous hash containing the
212 dispatch table matching UberText tags and Perl code.
213  
214 =head1 EXAMPLE
215  
216 =head2 Custom Module
217  
218 package Automobile;
219  
220 $Dispatch={
221 "make" => \&make,
222 "model" => \&model,
223 "color" => \&color,
224 "odometer' => \&mileage,
225 };
226  
227 sub uberText
228 {
229 my ($self)=shift;
230 return ($self,"my.automobile",$Dispatch);
231 }
232  
233 sub make
234 {
235 my ($self)=shift;
236 return ($self->{color});
237 }
238  
239 sub mileage
240 {
241 my ($self,$node)=@_;
242 my ($value);
243 if ($node->commandValue() eq "trip")
244 {
245 $value=$self->{odometer}->{trip};
246 } else {
247 $value=$self->{odometer}->{basic};
248 }
249 if ($node->getOptValue("units") eq "metric")
250 {
251 # convert miles to kilometers
252 $value=$value*1.61;
253 }
254 return $value;
255 }
256  
257 =head2 UberText File
258  
259 The manufacturer of my car is [my.automobile make ]
260 It is described as a [my.automobile color ] [my.automobile model ].
261 My last trip was [my.automobile odometer:(trip) units:(metric) ] kilometers.
262  
263  
264 =head1 METHODS
265  
266 =over 4
267  
268 =item $dispatch->extend($module)
269  
270 When a class name is passed to the Dispatch object, the module is loaded,
271 and the uberText() method is called. When a blessed object is passed, the
272 loading isn't necessary, so only the uberText() method is called.
273  
274 Based on the data returned from UberText, the data returned from the
275 uberText() method is saved in the internal dispatch table.
276  
277 =item $dispatch->involke($node);
278  
279 Takes the internal data from a Command node, and then runs the command
280 associated with the namespace.
281  
282 =item $dispatch->fetch($namespace);
283  
284 Returns the object in the dispatch table assigned to a particular UberText
285 namespace.
286  
287 =back
288  
289 =head1 AUTHOR
290  
291 Chris Josephes E<lt>cpj1@visi.comE<gt>
292  
293 =head1 SEE ALSO
294  
295 L<Text::UberText>
296  
297 =head1 COPYRIGHT
298  
299 Copyright 2002, Chris Josephes. All rights reserved.
300 This module is free software. It may be used, redistributed,
301 and/or modified under the same terms as Perl itself.
302 ~
« blib/lib/Text/UberText/Tree.pm ↑ Summary blib/lib/Text/UberText/Node.pm »