blib/lib/Froody/Plugin.pm

filter: Statementstmt 83.8% 26 / 31 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 8 / 12 Podpod 100% 9 / 9 totaltotal 80.3% 45 / 56 CC 4 SCAR 7.3
CC
4
Cov
80%
new
4.0
get_plugin_methods
2.0
pre_process
2.0
CRAP 2.1
« blib/lib/Froody/Response/Error.pm ↑ Summary blib/lib/Froody/Response.pm »
1 package Froody::Plugin;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Froody::Invoker::PluginService;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Froody::Plugin
9  
10 =head1 SYNOPSIS
11  
12 package My::Plugin;
13 use base qw( Froody::Plugin );
14
15 sub new {
16 my ($class, $impl, @args) = @_;
17 my $self = $class->SUPER::new($impl, @args);
18 $impl->mk_accessors( 'leg_count' );
19 return $self;
20 }
21
22 sub pre_process {
23 my ($impl, $method, $params) = @_;
24 $impl->leg_count( delete $params->{legs} || 2 );
25 }
26  
27 ...
28
29 package My::Implementation;
30 use base qw( Froody::Implementation );
31
32 __PACKAGE__->register_plugin('My::Plugin');
33
34 sub my_method {
35 my ($self, $params) = @_;
36 return $self->leg_count()." legs found.\n";
37 }
38  
39 =head1 DESCRIPTION
40  
41 Froody plugins let you extend an implementation and add functionality without
42 scary messing around with superclasses. They have a chance to get involved
43 in the pre_process step of the request, and can add accessors to the importing
44 class and instance.
45  
46 =head1 METHODS
47  
48 =over
49  
50 =item new( plugin class, implementation class, arguments... )
51
52 sub new {
53 my ($class, $impl, @args) = @_;
54 my $self = $class->SUPER::new($impl, @args);
55 $impl->mk_accessors( 'leg_count' );
56 return $self;
57 }
58  
59 Constructor, called as a class method. 'implementation class' here is the
60 class doing the requiring of the plugin, and 'arguments' are all the remaining
61 args passed to the require_plugin call.
62  
63 =cut
64  
65 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
66 1 my ($class, $impl, @arg) = @_;
67  
68 1 ▶ $impl->plugin_methods( [] ) unless $impl->plugin_methods;
Branch: 1 branch
BranchTrueFalseTotal
unless $impl->plugin_methods 1 0 1
69  
70 1 my $self = bless {}, $class;
71
72 1 $self->init($impl, @arg);
73
74 1 my $inv = Froody::Invoker::PluginService->new
75 ->delegate_class($class)->plugin_impl($impl);
76  
77 1 foreach my $method ($self->get_plugin_methods($impl)) {
78 1 ▶ $method->invoker($inv) unless $method->invoker;
Branch: 1 branch
BranchTrueFalseTotal
unless $method->invoker 1 0 1
79 1 push @{$impl->plugin_methods}, $method;
80 }
81
82 1 return $self;
83 }
84  
85 =item init( implementation, args )
86  
87 the place to initialize your object. The return value is ignored.
88  
89 =cut
90  
91 1 ▶sub init { return }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
92  
93 =item get_plugin_methods( implementation class )
94  
95 plugins should override this method to return froody methods provided
96 by the plugin. The methods will then be registered.
97  
98 =cut
99  
100 0 ▶sub get_plugin_methods { () }
Subroutine get_plugin_methods: not called CC=1 SCAR=6.9
Pod documented
101  
102 =item pre_process( implementation class, method, params )
103  
104 This is where your plugin gets to do interesting things to the request.
105 See the docs for pre_process in L<Froody::Invoker::Implementation> for
106 details on what you can do here. Importantly, this is before parameter
107 validation. For instance, a session management plugin could remove a
108 'session_id' param from the request here, and load a session:
109  
110 sub pre_process {
111 my ($self, $method, $param) = @_;
112 my $session_id = delete $param->{session_id};
113 $self->session( $session_id );
114 }
115  
116 and the method signature of the implementation wouldn't need to include
117 it.
118  
119 Note that the method is called with the first parameter being the
120 implementation class, not the plugin class. If you need access to the
121 plugin object, override L<plugin_pre_process> (see below).
122  
123 =cut
124  
125 # pre_process is documented
126 0 ▶sub pre_process {
Subroutine pre_process: not called CC=1 SCAR=6.9
Pod documented
127 0 return undef;
128 }
129  
130 =item plugin_pre_process(self, implementation class, method, params)
131  
132 If you need access to your plugin instance, override this method. Its
133 default behaviour (in Froody::Plugin) is to call pre_process, and not
134 pass the plugin instance. Override this method I<or> pre_process.
135  
136 =cut
137  
138 2 ▶sub plugin_pre_process {
Subroutine plugin_pre_process: called CC=1 SCAR=0.0
Pod documented
139 2 my $self = shift;
140 # first arg to actual pre_process is not the plugin itself
141 2 $self->can('pre_process')->(@_);
142 }
143  
144 =item post_process( implementation, method, args )
145  
146 By overriding this method, you can preform actions after the
147 implementation has done its work. See the docs for post_process in
148 L<Froody::Invoker::Implementation> for details on what you can do here.
149 The 'args' param here is the hash returned from the implementation,
150 before it is interpolated into the XML by the path walker, so you can
151 change the returned XML, but only within the spec of the API.
152  
153 =cut
154  
155 2 ▶sub post_process { return undef }
Subroutine post_process: called CC=1 SCAR=0.0
Pod documented
156  
157 =item plugin_post_process
158  
159 If you need access to your plugin instance, override this method. Its
160 default behaviour (in Froody::Plugin) is to call post_process, and not
161 pass the plugin instance. Override this method I<or> post_process.
162  
163 =cut
164  
165 2 ▶sub plugin_post_process {
Subroutine plugin_post_process: called CC=1 SCAR=0.0
Pod documented
166 2 my $self = shift;
167 # first arg to actual post_process is not the plugin itself
168 2 $self->can('post_process')->(@_);
169 }
170  
171  
172 =item error_handler( implementation, method, args )
173  
174 By overriding this method, you can preform actions after the
175 implementation has done it's work. See the docs for error_handler in
176 L<Froody::Invoker::Implementation> for details on what you can do here.
177 The 'args' param here is the hash returned from the implementation,
178 before it is interpolated into the XML by the path walker, so you can
179 change the returned XML, but only within the spec of the API.
180  
181 =cut
182  
183 0 ▶sub error_handler { return undef }
Subroutine error_handler: not called CC=1 SCAR=6.9
Pod documented
184  
185 =item plugin_error_handler
186  
187 If you need access to your plugin instance, override this method. Its
188 default behaviour (in Froody::Plugin) is to call error_handler, and not
189 pass the plugin instance. Override this method I<or> error_handler.
190  
191 =cut
192  
193 0 ▶sub plugin_error_handler {
Subroutine plugin_error_handler: not called CC=1 SCAR=6.9
Pod documented
194 0 my $self = shift;
195 # first arg to actual error_handler is not the plugin itself
196 0 $self->can('error_handler')->(@_);
197 }
198  
199  
200 =back
201  
202 =head1 AUTHOR
203  
204 Copyright Fotango 2005. All rights reserved.
205  
206 Please see the main L<Froody> documentation for details of who has worked
207 on this project.
208  
209 This module is free software; you can redistribute it and/or modify it under
210 the same terms as Perl itself.
211  
212 =cut
213  
214 3 1;
« blib/lib/Froody/Response/Error.pm ↑ Summary blib/lib/Froody/Response.pm »