blib/lib/Froody/Method.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 95.8% 23 / 24 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 96.6% 117 / 121 CC 15 SCAR 9.0
CC
15
Cov
97%
match_to_regex
4.0
invoker
4.0
full_name
3.0
CRAP 2.5
« blib/lib/Froody/Structure.pm ↑ Summary blib/lib/Froody/Response/Terse.pm »
1 package Froody::Method;
2 30 ▶use base qw(Froody::Structure);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 our $VERSION = 0.01;
12  
13 30 __PACKAGE__->mk_accessors(qw{ arguments errors needslogin description });
14  
15 =head1 NAME
16  
17 Froody::Method - object representing a method callable by Froody
18  
19 =head1 SYNOPSIS
20  
21 # create
22 use Froody::Method;
23 my $method = Froody::Method->new()
24 ->full_name("wibble.fred.burt")
25 ->invoker($invoker);
26  
27 # run the method
28 my $froody_response = $method->call( fred => "wilma", barney => "betty" );
29  
30 # inspect
31 $invoker = $method->invoker;
32 $full_name = $method->full_name;
33 $name = $method->name;
34 $module = $method->module;
35  
36 =head1 DESCRIPTION
37  
38 An accessor class for definition of method APIs. Once a method is declared
39 here you can execute it by calling its C<call> function.
40  
41 Froody::API modules must return from their C<load> method one of these objects
42 per method that can be called by the Froody server. That said, it's not
43 normal to have to write code that creates these, but instead use the
44 Froody::API::XML to create the Froody::Module objects from
45 definitions in an XML file.
46  
47 Method objects must know what they're called and what invoker defines them (and
48 likewise the invoker class must know what Perl code to run when they're handed
49 this method object via C<invoke>.)
50  
51 =head1 METHODS
52  
53 =over
54  
55 =item new()
56  
57 Create a new reflection object
58  
59 =cut
60  
61 =item call($params_hashref)
62  
63 Calls this method. This dispatches this method via the correct implementation
64 (as defined by C<implementation> below and returns a Froody::Response object.
65  
66 =cut
67  
68 sub call
69 20 ▶{
Subroutine call: called CC=2 SCAR=6.9
Pod documented
70 20 my ($self, $params_hash, $metadata) = @_;
71
72 20 ▶ my $invoker = $self->invoker
Branch: 1 branch
BranchTrueFalseTotal
my $invoker = $self->invoker or "Froody::Error"->throw("froody.invoke.noinvoker", "No invoker defined for this method") 19 1 20
73 or Froody::Error->throw("froody.invoke.noinvoker", "No invoker defined for this method");
74
75 19 return $invoker->invoke( $self, $params_hash, $metadata );
76 }
77  
78 =item match_to_regex( "foo.bar.*" )
79  
80 Class method that returns a regular expression that will determine if a string
81 matches the specification passed in
82  
83 =cut
84  
85 sub match_to_regex
86 30 ▶{
Subroutine match_to_regex: called CC=4 SCAR=14.0
Pod documented
87 30 my $whatever = shift;
88 30 ▶ my $query = shift || qr/.*/;
Condition100%2/2
shift() || qr/.*/
l!l
264
MC/DC100%1/1
shift() || qr/.*/
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89 30 ▶ return $query if ref $query eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $query eq "Regexp" 7 23 30
90
91 23 ▶ if ($query =~ /[^a-zA-Z.*]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ /[^a-zA-Z.*]/) 0 23 23
92 0 { Froody::Error->throw("perl.methodcall.param", "Bad method spec '$query'"); }
93
94 23 $query =~ s{\.}{\.}g; # dots are dots
95 23 $query =~ s/\*{2}/\[\\w\\d.\]+/g; # double stars match anything.
96 23 $query =~ s/\*/[^.]+/g; # stars are not dots
97 23 return qr/^$query$/;
98 }
99  
100  
101 # right, previously these were all defined using Class::Accessor::Chained::Fast and
102 # just set in C<new>, which is really, really dumb, since then they could be
103 # set to inconsistent values later. Let's not do that, let's compute them on an
104 # as-needed basis
105  
106 69 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
107 69 my $self = shift;
108 69 ▶ croak __PACKAGE__."->name is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 68 69
109 68 $self->full_name =~ /\.([^.]+)$/;
110 68 return $1;
111 }
112  
113 6 ▶sub service {
Subroutine service: called CC=2 SCAR=6.9
Pod documented
114 6 my $self = shift;
115 6 ▶ croak __PACKAGE__."->service is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 5 6
116 5 $self->full_name =~ /^([^.]+)/;
117 5 return $1;
118 }
119  
120 6 ▶sub object {
Subroutine object: called CC=2 SCAR=6.9
Pod documented
121 6 my $self = shift;
122 6 ▶ croak __PACKAGE__."->object is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 5 6
123 5 my @parts = split /\./, $self->full_name;
124 5 shift @parts; pop @parts; # lose the service and name
125 5 return join "::", map { ucfirst $_ } @parts
126 }
127  
128 4 ▶sub module {
Subroutine module: called CC=2 SCAR=6.9
Pod documented
129 4 my $self = shift;
130 4 ▶ croak __PACKAGE__."->module is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 3 4
131 3 return ucfirst($self->service) . "::" . $self->object;
132 }
133  
134 # and this should be a more complicated accessor that checks that we've
135 # got all the parts we need
136  
137 sub full_name
138 990 ▶{
Subroutine full_name: called CC=3 SCAR=11.0
Pod documented
139 990 my $self = shift;
140 990 ▶ return $self->{full_name} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 794 196 990
141 196 my $name = shift;
142
143 # check the name has at least two dots and otherwise consists
144 # of upper and lower case a-z.
145 # HACKERS: Note, if you change this, you'll need to change the
146 # code in C<match_to_regex> above
147 196 ▶ Froody::Error->throw("perl.methodcall.param", "Invalid Method name '$name'")
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[^a-zA-Z.0-9_]/ 2 194 196
148 unless $name !~ m/[^a-zA-Z.0-9_]/;
149  
150 194 $self->{full_name} = $name;
151 194 return $self;
152 }
153  
154 =item source
155  
156 Provide diagnostic information about this method.
157  
158 =cut
159  
160 2 ▶sub source {
Subroutine source: called CC=2 SCAR=6.9
Pod documented
161 2 my $self = shift;
162  
163 2 ▶ return ($self->invoker ? $self->invoker->source : "unbound").": ".$self->full_name;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoker ? : 1 1 2
164 }
165  
166 =back
167  
168 =head1 ACCESSORS
169  
170 =over
171  
172 =item full_name
173  
174 The full dot-path name of the method.
175  
176 =item name
177  
178 The method name. Read only (set as a side-effect of setting C<full_name>.)
179  
180 =item module
181  
182 The perl-style package name of the method. Read only (set as a side-effect of
183 setting C<full_name>.)
184  
185 =item service
186  
187 The service name of of the method. Read only (set as a side-effect of setting
188 C<full_name>.)
189  
190 =item object
191  
192 The class responsible for the overall handling of a method. Read only (set as a
193 side-effect of setting C<full_name>.)
194  
195 =item arguments
196  
197 A hash reference with the names of each argument, with the following structure:
198 { 'name' => {
199 multiple => 1,
200 optional => 1,
201 doc => 'Argument documentation',
202 type => 'text', #user defined type label.
203 }
204 }
205  
206 If the argument encodes (somehow) multiple values, then multiple must be set.
207 If it does not, the 'multiple' key may be omitted, or set to a false value. An
208 argument is assumed to be required unless the 'optional' key is set to a true
209 value.
210  
211 =item structure
212  
213 A hash reference containing the specification of how the data that will be
214 returned by a call to this method will be aranged - essentially the blueprint
215 for constructing the response.
216  
217 You probably don't want to create these by hand; The Froody::API::XML
218 module will create these given a suitable example. See that module documentaion
219 for more info.
220  
221 The structure is a simple hash with a 'xpath' style key pointing to a hash
222 containing C<elts> (elements) and C<attr> (attributes) arrayrefs.
223  
224 { 'people' =>
225 { attr => ['group'],
226 elts => [qw/person/],
227 },
228 'people/person' =>
229 { elts => [qw/name/],
230 attr => [qw/nick number/],
231 text => 0,
232 multi => 1,
233 },
234 };
235  
236 C<elts> should contain a list of all elements under this node. Each one of these
237 elements will require a further entry in the hash unless the element contains
238 only text and cannot be repeated (for example, the title and description
239 in the above data structure are like this.)
240  
241 The hashrefs may also contain other flags. The C<text> flag can be used to
242 indicate if it is valid for this node to contain text or not. The C<multi>
243 flag is used to indicate if there can be repeate occurances of the elements.
244  
245 This would mean that the above data structure would validate this XML structure:
246  
247 <people group="frameworks" />
248 <person nick="clkao" number="243">
249 <name>Chia-liang Kao</name>
250 </person>
251 <person nick="Trelane" number="234">
252 <name>Mark Fowler</name>
253 </person>
254 <person nick="Nichloas" number="238">
255 <name>Nicholas Clark</name>
256 </person>
257 <person nick="nnunley" number="243">
258 <name>Norman Nunley</name>
259 </person>
260 <person nick="skugg" number="214">
261 <name>Stig Brautaset</name>
262 </person>
263 <person nick="jerakeen" number="235">
264 <name>Tom Insam</name>
265 </person>
266 Frameworks is a department of Fotango. We work on lots of
267 software, including writing tools like Froody.
268 </people>
269  
270 =item example_response
271  
272 An example of the response we expect to see from the method. This should
273 be a Froody::Response object.
274  
275 =item errors
276  
277 A hashref containing a mapping of numeric error code (as the key) to a message
278 description (as the value.)
279  
280 =item needslogin
281  
282 A boolean field. 1 if the user must be 'logged in' in order to use the method.
283  
284 =item description
285  
286 A briefer documentation section for the method represented by the
287 current instance.
288  
289 =item invoker
290  
291 The invoker instance that knows about how to run the Perl code needed to
292 actually do whatever this method is meant to represent. This must be
293 a subclass of Froody::Invoker.
294  
295 =cut
296  
297 234 ▶sub invoker {
Subroutine invoker: called CC=4 SCAR=13.9
Pod documented
298 234 my $self = shift;
299 234 ▶ return $self->{invoker} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 73 161 234
300
301 # check that that we've been passed an implementation
302 161 ▶ unless (blessed($_[0]) && $_[0]->isa("Froody::Invoker"))
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_[0]) and $_[0]->isa("Froody::Invoker")) 1 160 161
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Froody::Invoker")
!ll&&!rl&&r
10160
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("Froody::Invoker")
builtin::blessed($_[0]) $_[0]->isa("Froody::Invoker")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Froody::Invoker")
ABresult
0X0
100
111
303 {
304 1 Froody::Error->throw("perl.methodcall.param",
305 "You must pass invoker an instance".
306 "of something that is a Froody::Invoker, and '$_[0]' isn't");
307 }
308
309 160 $self->{invoker} = shift;
310 160 return $self;
311 }
312  
313 =back
314  
315 =head1 BUGS
316  
317 None known.
318  
319 Please report any bugs you find via the CPAN RT system.
320 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
321  
322 =head1 AUTHOR
323  
324 Copyright Fotango 2005. All rights reserved.
325  
326 Please see the main L<Froody> documentation for details of who has worked
327 on this project.
328  
329 This module is free software; you can redistribute it and/or modify it under
330 the same terms as Perl itself.
331  
332 =head1 SEE ALSO
333  
334 L<Froody>, L<Froody::Repository>, L<Froody::Invoker>
335  
336 =cut
337  
338 30 1;
« blib/lib/Froody/Structure.pm ↑ Summary blib/lib/Froody/Response/Terse.pm »