blib/lib/Froody/Repository.pm

filter: Statementstmt 95.8% 116 / 121 Branchbran 80.5% 29 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 23 / 23 Podpod 100% 11 / 11 totaltotal 91.8% 180 / 196 CC 23 SCAR 11.3
CC
23
Cov
91%
load
6.0
register_api
5.9
register_method
4.2
CRAP 3.1
« blib/lib/Froody/Implementation.pm ↑ Summary blib/lib/Froody/Upload.pm »
1 package Froody::Repository;
2 19 ▶use base qw(Froody::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 19 ▶use Froody::Reflection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Froody::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Froody::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Froody::ErrorType;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 19 my $logger = get_logger('froody.repository');
14  
15 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 19 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 19 our $VERSION = 0.01;
21  
22 =head1 NAME
23  
24 Froody::Repository - a repository of Froody::Method objects.
25  
26 =head1 SYNOPSIS
27  
28 use Froody::Repository;
29 my $repository = Froody::Repository->new();
30
31 # methods for putting stuff in the repository
32
33 $repository->register_implementation('Implementation::Package');
34
35 $repository->register_method($froody_method);
36 $repository->register_errortype($froody_errortype);
37
38 # methods for getting froody methods out
39 my $method = $repository->get_method('name.of.method');
40 my @methods = $repository->get_methods;
41 my @methods2 = $repository->get_methods('namespace.of.interest');
42 my @methods3 = $repository->get_methods(qr/delete/);
43
44 # methods for getting froody errortype out
45 my $errortype = $repository->get_errortype('name.of.errortype');
46 my @errortype = $repository->get_errortypes;
47 my @errortype2 = $repository->get_errortypes('errortypes.of.interest');
48 my @errortype3 = $repository->get_errortypes(qr/delete/);
49 my $errortype2 = $repository->get_closest_errortype('fragment.of.type');
50  
51 =head1 DESCRIPTION
52  
53 L<Froody::Repository> provides a central location to register and
54 discover L<Froody::Method> instances.
55  
56 =head1 METHODS
57  
58 =cut
59  
60 12 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
61 12 my $self = shift;
62  
63 ### default error ###
64
65 12 my $default = Froody::ErrorType->new()->name("");
66 12 $self->register_errortype($default);
67  
68 ### reflection methods and errortypes ###
69  
70 12 $self->register_implementation('Froody::Reflection');
71
72  
73 12 return $self;
74 }
75  
76 =head2 Methods for dealing with Froody::Method instances
77  
78 =over
79  
80 =item register_method( Froody::Method )
81  
82 Register a method with the repository. You don't ever normally have
83 to call this method yourself, things like your Froody::Implementation
84 subclasses do it themselves from C<register_in_repository>.
85  
86 =cut
87  
88 163 ▶sub register_method {
Subroutine register_method: called CC=4 SCAR=14.5
Pod documented
89 163 my $self = shift;
90 163 my $method = shift;
91  
92 163 ▶ Froody::Error->throw("perl.methodcall.param", "you didn't pass a Froody::Method object")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($method, "Froody::Method") 3 160 163
93 unless UNIVERSAL::isa($method, 'Froody::Method');
94
95 160 my $old_method = $self->{method_cache}{$method->full_name};
96 160 ▶ unless ($old_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_method) { } 10 150 160
if (do { not $old_method->full_name =~ /^froody\.reflection\./ }) 0 10 10
97 150 $self->{method_cache}{$method->full_name} = $method;
98 0 } elsif ($old_method->full_name !~ m/^froody\.reflection\./) {
99 0 my $inv = $old_method->invoker;
100
101 0 Froody::Error->throw("perl.method.alreadyregistered",
102 $method->source." was already registered through ".ref $inv);
103 }
104  
105 160 return $self;
106 }
107  
108 =item get_methods() || get_methods( $regex ) || get_methods( "foo.bar.*" )
109  
110 Return all methods (as Froody::Method objects) if invoked with no arguments, or
111 only the methods matching the query if invoked with an argument.
112  
113 =cut
114  
115 12 ▶sub get_methods {
Subroutine get_methods: called CC=2 SCAR=6.9
Pod documented
116 12 my $self = shift;
117
118 # return everything if no arguments
119 12 ▶ unless (@_) { return values %{ $self->{method_cache} } }
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 5 7 12
120  
121 # what methods are we looking for? Make it a regex if it's not already
122 7 my $query = Froody::Method->match_to_regex( shift );
123 59 return grep { $_->full_name =~ $query }
124 7 values %{ $self->{method_cache} };
125 }
126  
127 =item get_method($name)
128  
129 Return a single method (as a Froody::Method object) matching $name exactly.
130 Throws a Froody::Error of type "froody.invoke.nosuchmethod" if no method
131 matching the $name is registered with this repository.
132  
133 =cut
134  
135 54 ▶sub get_method {
Subroutine get_method: called CC=2 SCAR=6.9
Pod documented
136 54 my $self = shift;
137 54 my $name = shift;
138  
139 54 ▶ my $method = $self->{method_cache}{ $name }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->{"method_cache"}{$name} or "Froody::Error"->throw("froody.invoke.nosuchmethod", "Method '${name}' not found.") 48 6 54
140 or Froody::Error->throw("froody.invoke.nosuchmethod", "Method '$name' not found.");
141 48 return $method;
142 }
143  
144 =back
145  
146 =head2 Methods for dealing with Froody::ErrorType instances
147  
148 =over
149  
150 =item register_errortype( Froody::ErrorType )
151  
152 Register an errortype with the repository. You don't ever normally have
153 to call this method yourself, things like your Froody::Implementation
154 subclasses do it themselves from C<register_in_repository>.
155  
156 =cut
157  
158 50 ▶sub register_errortype {
Subroutine register_errortype: called CC=3 SCAR=11.0
Pod documented
159 50 my $self = shift;
160 50 my $errortype = shift;
161
162 50 ▶ Froody::Error->throw("perl.methodcall.param", "you didn't pass a Froody::ErrorType object")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($errortype, "Froody::ErrorType") 3 47 50
163 unless UNIVERSAL::isa($errortype, 'Froody::ErrorType');
164
165 47 my $code = $errortype->name;
166 19 ▶ use Carp; Carp::confess unless defined $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 47 47
167 47 $self->{errortypes_cache}{ $code } = $errortype;
168  
169 47 return $self;
170 }
171  
172 =item get_errortype($name)
173  
174 Return a single errortype (as a Froody::ErrorType object) matching $name exactly.
175 Throws a Froody::Error of type "froody.invoke.nosucherrortype" if no method
176 matching the $name is registered with this repository.
177  
178 =cut
179  
180 7 ▶sub get_errortype {
Subroutine get_errortype: called CC=2 SCAR=7.0
Pod documented
181 7 my $self = shift;
182 7 my $name = shift;
183  
184 7 ▶ my $errortype = $self->{errortypes_cache}{ $name }
Branch: 1 branch
BranchTrueFalseTotal
my $errortype = $self->{"errortypes_cache"}{$name} or "Froody::Error"->throw("froody.invoke.nosucherrortype", "ErrorType '${name}' not found") 7 0 7
185 or Froody::Error->throw("froody.invoke.nosucherrortype", "ErrorType '$name' not found");
186 7 return $errortype;
187 }
188  
189 =item get_errortypes() || get_errortypes( $regex ) || get_errortypes( "foo.bar.*" )
190  
191 Return all errortypes (as Froody::ErrorTypes objects) if invoked with no
192 arguments, or only the methods matching the query if invoked with an argument.
193  
194 =cut
195  
196 4 ▶sub get_errortypes {
Subroutine get_errortypes: called CC=3 SCAR=11.0
Pod documented
197 4 my $self = shift;
198 4 my $err_hash = $self->{errortypes_cache};
199
200 # return everything if no arguments
201 4 ▶ unless (@_) { return values %{ $err_hash } };
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 2 2 4
202  
203 # what methods are we looking for? Make it a regex if it's not already
204 2 ▶ my $query = ref($_[0]) ? shift : Froody::Method->match_to_regex( shift );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 1 2
205 8 return grep { $_->name =~ $query }
206 2 values %{ $err_hash };
207 }
208  
209 =item get_closest_errortype
210  
211 =cut
212  
213 23 ▶sub get_closest_errortype {
Subroutine get_closest_errortype: called CC=3 SCAR=11.0
Pod documented
214 23 my $self = shift;
215 23 my $code = shift;
216 23 my @bits = split /\./, $code;
217
218 23 my $err_hash = $self->{errortypes_cache};
219
220 23 foreach (reverse 0..$#bits)
221 {
222 38 my $string = join '.', @bits[0..$_];
223 38 ▶ if ($err_hash->{ $string }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_hash->{$string}) 17 21 38
224 17 return $err_hash->{ $string };
225 }
226 }
227
228 # return the default errortype hash which is always there, as it was
229 # created during init.
230 6 return $err_hash->{""};
231 }
232  
233 =item register_implementation(PACKAGE NAME)
234  
235 Registers all the methods associated with a given implementation
236 and API in this repository.
237  
238 =cut
239  
240 sub register_implementation
241 14 ▶{
Subroutine register_implementation: called CC=2 SCAR=7.0
Pod documented
242 14 my $self = shift;
243 14 my $class = shift;
244  
245 # load the api, if we can.
246 14 $class->require;
247  
248 # what of this API do we implement?
249 14 my ($api_class, @method_matches) = $class->implements;
250 14 ▶ return unless $api_class; # Allow for superclasses doing crazy things.
Branch: 1 branch
BranchTrueFalseTotal
unless $api_class 0 14 14
251 14 my $invoker = $class->new();
252  
253 14 return $self->register_api($api_class, $invoker, @method_matches);
254 }
255  
256 =item register_api($api, $invoker, @method_matches)
257  
258 Registers all the methods associated with a given API.
259  
260 =cut
261 14 ▶sub register_api {
Subroutine register_api: called CC=5 SCAR=17.8
Pod documented
262 14 my ($self, $api_class, $invoker, @method_matches) = @_;
263  
264 14 @method_matches = map { Froody::Method->match_to_regex( $_ ) } @method_matches;
265 # load the api
266 14 ▶ $api_class->require
Branch: 1 branch
BranchTrueFalseTotal
$api_class->require or "Froody::Error"->throw("perl.use", "unknown or broken API class: $api_class") 14 0 14
267 or Froody::Error->throw("perl.use", "unknown or broken API class: $api_class");
268  
269 # process each thing based on it's type
270 14 my @structures;
271 14 foreach my $thingy ($api_class->load())
272 {
273 89 ▶ if (!blessed($thingy) || !$thingy->isa("Froody::Structure")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($thingy) and $thingy->isa("Froody::Structure")) 0 89 89
Condition33%1/3
builtin::blessed($thingy) and $thingy->isa("Froody::Structure")
!ll&&!rl&&r
0089
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("Froody::Structure")
builtin::blessed($thingy) $thingy->isa("Froody::Structure")
Truth table33%1/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("Froody::Structure")
ABresult
0X0
100
111
274 0 $logger->info("$api_class: load() returned the string '$thingy',"
275 ."which is not a Froody::Structure");
276 0 next;
277 }
278 89 push @structures, $thingy;
279 }
280 14 $self->load($invoker, \@structures, @method_matches);
281 }
282  
283  
284 =item load($invoker, [structures], [method filters])
285  
286 Loads a set of method and errortype structures into the repository
287  
288 =cut
289  
290 24 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod documented
291 24 my ($self, $invoker, $structures, @method_matches) = @_;
292  
293 # process each thing based on its type
294 24 foreach my $thingy (@$structures)
295 {
296 # froody method? Set the invoker and register it
297 195 ▶ if ($thingy->isa("Froody::Method")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thingy->isa('Froody::Method')) { } 168 27 195
elsif ($thingy->isa('Froody::ErrorType')) { } 27 0 27
298 168 my $full_name = $thingy->full_name;
299 # Only bind methods marked as being 'implemented' by the implementation.
300 168 ▶ if (@method_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@method_matches) 127 41 168
301 127 ▶ next unless any { $full_name =~ $_ } @method_matches;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $full_name =~ /$_/; } , @method_matches) 10 117 127
302 }
303 158 $thingy->invoker($invoker);
304 158 $self->register_method($thingy);
305 } elsif ($thingy->isa("Froody::ErrorType")) {
306 27 $self->register_errortype($thingy);
307 }
308 }
309  
310 24 return $self;
311 }
312  
313  
314 =back
315  
316 =head1 BUGS
317  
318 None known
319  
320 We're using C<match_to_regex> from Froody::Method, maybe that should
321 be refactored elsewhere
322  
323 Please report any bugs you find via the CPAN RT system.
324 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
325  
326 =head1 AUTHOR
327  
328 Copyright Fotango 2005. All rights reserved.
329  
330 Please see the main L<Froody> documentation for details of who has worked
331 on this project.
332  
333 This module is free software; you can redistribute it and/or modify it under
334 the same terms as Perl itself.
335  
336 =head1 SEE ALSO
337  
338 L<Froody>, L<Froody::Method>
339  
340 =cut
341  
342 19 1;
« blib/lib/Froody/Implementation.pm ↑ Summary blib/lib/Froody/Upload.pm »