ExtDirect.pm

filter: Statementstmt 92.8% 91 / 98 Branchbran 67.6% 23 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 10 totaltotal 81.3% 131 / 161 CC 21 SCAR 13.6
CC
21
Cov
86%
import
7.0
get_method_parameters
6.7
get_method_list
5.0
CRAP 3.9
« ExtDirect/Config.pm ↑ Summary ExtDirect/API/Hook.pm »
1 package RPC::ExtDirect;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use RPC::ExtDirect::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use RPC::ExtDirect::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ### PACKAGE VARIABLE ###
14 #
15 # Version of this module. This should be kept as a string
16 # because otherwise 'make dist' strips "insignificant" digits
17 # at the end.
18 #
19  
20 22 our $VERSION = '3.23';
21  
22 ### PACKAGE GLOBAL VARIABLE ###
23 #
24 # Debugging; defaults to off.
25 #
26 # DEPRECATED. Use `debug` Config option instead.
27 #
28  
29 22 our $DEBUG;
30  
31 # This is a bit hacky, but we've got to keep a reference to the API object
32 # so that *compilation time* attributes would work as expected,
33 # as well as the configuration options for the RPC::ExtDirect::API class.
34 {
35 22 my $api = RPC::ExtDirect::API->new();
36
37 ### PUBLIC CLASS METHOD ###
38 #
39 # Return the global API instance
40 #
41
42 538 ▶ sub get_api { $api }
Subroutine get_api: called CC=1 SCAR=0.0
Pod undocumented
43 }
44  
45  
46 ### PUBLIC PACKAGE SUBROUTINE ###
47 #
48 # Provides a facility to assign package-level (action) properties.
49 # Despite its name, does not import anything to the caller package's
50 # namespace.
51 #
52  
53 22 sub import {
54 85 ▶ my ($class, @args) = @_;
Subroutine import: called CC=7 SCAR=19.5
55  
56 # Nothing to do
57 85 ▶ return unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 47 38 85
58  
59 # Only hash-like arguments are supported
60 38 ▶ croak "Odd number of arguments in RPC::ExtDirect::import()"
Branch: 1 branch
BranchTrueFalseTotal
unless @args % 2 == 0 0 38 38
61 unless (@args % 2) == 0;
62  
63 38 my %arg = @args;
64 38 %arg = map { lc $_ => delete $arg{ $_ } } keys %arg;
65  
66 38 my ($package, $filename, $line) = caller();
67
68 38 my $api = $class->get_api;
69  
70 # Store Action (class) name as an alias for a package
71 my $action_name = defined $arg{action} ? $arg{action}
72 : defined $arg{class} ? $arg{class}
73 : undef
74 38 ▶ ;
Branch: 2 branches
BranchTrueFalseTotal
defined $arg{'action'} ? : 17 21 38
defined $arg{'class'} ? : 0 21 21
75
76 # We don't want to overwrite the existing Action, if any
77 38 $api->add_action(
78 package => $package,
79 action => $action_name,
80 no_overwrite => 1,
81 );
82  
83 # Store package level hooks
84 38 for my $type ( $api->HOOK_TYPES ) {
85 114 my $code = $arg{ $type };
86  
87 114 ▶ $api->add_hook( package => $package, type => $type, code => $code )
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 40 74 114
88 if defined $code;
89 };
90 }
91  
92 ### PUBLIC ATTRIBUTE DEFINITION ###
93 #
94 # Define ExtDirect attribute subroutine and export it into UNIVERSAL
95 # namespace. Attribute processing phase depends on the perl version
96 # we're running under.
97 #
98  
99 {
100 22 ▶ my $phase = $] >= 5.012 ? 'BEGIN' : 'CHECK';
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.012 ? : 22 0 22
101 22 my $pkg = __PACKAGE__;
102
103 22 ▶ eval <<END;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine ExtDirect: called CC=3 SCAR=11.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 1 307 308
'ARRAY' eq ref $@ ? : 1 0 1
104 sub UNIVERSAL::ExtDirect : ATTR(CODE,$phase) {
105 my \$attr = RPC::ExtDirect::Util::parse_attribute(\@_);
106
107 eval { ${pkg}->add_method(\$attr) };
108  
109 if (\$@) { die 'ARRAY' eq ref(\$@) ? \$\@->[0] : \$@ }; };
110 END
111 }
112  
113 ### PUBLIC CLASS METHOD ###
114 #
115 # Add a hook to the global API
116 #
117 # DEPRECATED. See RPC::ExtDirect::API for replacement.
118 #
119  
120 sub add_hook {
121 0 ▶ my ($class, %arg) = @_;
Subroutine add_hook: not called CC=1 SCAR=6.9
Pod undocumented
122  
123 0 my $api = $class->get_api();
124
125 0 $api->add_hook(%arg);
126  
127 0 return $arg{code};
128 }
129  
130 ### PUBLIC CLASS METHOD ###
131 #
132 # Return hook coderef by package and method, with hierarchical lookup.
133 #
134 # DEPRECATED. See RPC::ExtDirect::API for replacement.
135 #
136  
137 sub get_hook {
138 22 ▶ my ($class, %arg) = @_;
Subroutine get_hook: called CC=2 SCAR=7.0
Pod undocumented
139  
140 22 my $api = $class->get_api();
141 22 my $hook = $api->get_hook(%arg);
142
143 22 ▶ return $hook ? $hook->code : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hook ? : 22 0 22
144 }
145  
146 ### PUBLIC CLASS METHOD ###
147 #
148 # Adds Action name as an alias for a package
149 #
150 # DEPRECATED. See RPC::ExtDirect::API for replacement.
151 #
152  
153 sub add_action {
154 0 ▶ my ($class, $package, $action_for_pkg) = @_;
Subroutine add_action: not called CC=1 SCAR=6.9
Pod undocumented
155
156 0 my $api = $class->get_api();
157
158 0 return $api->add_action(
159 package => $package,
160 action => $action_for_pkg,
161 );
162 }
163  
164 ### PUBLIC CLASS METHOD ###
165 #
166 # Returns the list of Actions that have ExtDirect methods
167 #
168 # DEPRECATED. See RPC::ExtDirect::API for replacement.
169 #
170  
171 sub get_action_list {
172 1 ▶ my ($class) = @_;
Subroutine get_action_list: called CC=1 SCAR=0.0
Pod undocumented
173
174 1 my $api = $class->get_api();
175
176 1 my @actions = sort $api->actions();
177
178 1 return @actions;
179 }
180  
181 ### PUBLIC CLASS METHOD ###
182 #
183 # Returns the list of poll handler methods as list of
184 # arrayrefs: [ $action, $method ]
185 #
186 # DEPRECATED. See RPC::ExtDirect::API for replacement.
187 #
188  
189 sub get_poll_handlers {
190 1 ▶ my ($class) = @_;
Subroutine get_poll_handlers: called CC=3 SCAR=11.0
Pod undocumented
191
192 1 my $api = $class->get_api();
193 1 my @actions = $class->get_api->actions;
194 1 my @handlers;
195
196 1 for my $name ( @actions ) {
197 4 my $action = $api->get_action_by_name($name);
198 4 my @methods = $action->polling_methods;
199
200 4 push @handlers, [ $name, $_ ] for @methods;
201 }
202
203 1 return @handlers;
204 }
205  
206 ### PUBLIC CLASS METHOD ###
207 #
208 # Adds a method to the global API
209 #
210 # DEPRECATED. See RPC::ExtDirect::API for replacement.
211 #
212  
213 sub add_method {
214 308 ▶ my ($class, $attribute_ref) = @_;
Subroutine add_method: called CC=1 SCAR=0.0
Pod undocumented
215
216 308 my $api = $class->get_api;
217
218 308 return $api->add_method( %$attribute_ref );
219 }
220  
221 ### PUBLIC CLASS METHOD ###
222 #
223 # Returns the list of method names with ExtDirect attribute
224 # for $action_name, or all methods for all actions if $action_name
225 # is empty
226 #
227 # DEPRECATED. See RPC::ExtDirect::API for replacement.
228 #
229  
230 sub get_method_list {
231 5 ▶ my ($class, $action_name) = @_;
Subroutine get_method_list: called CC=5 SCAR=16.1
Pod undocumented
232
233 5 my $api = $class->get_api;
234
235 5 ▶ my @actions = $action_name ? ( $action_name ) : $api->actions;
Branch: 1 branch
BranchTrueFalseTotal
$action_name ? : 4 1 5
236 5 my @list;
237
238 5 for my $name ( @actions ) {
239 8 my $action = $api->get_action_by_name($name);
240
241 # The output of this method is inconsistent:
242 # when called with $action_name it returns the list of
243 # method names; when it is called with empty @_
244 # it returns the list of Action::method pairs.
245 # I don't remember what was the original intent here but
246 # we've got to keep up compatibility. The whole method is
247 # deprecated anyway...
248 8 ▶ my $tpl = $action_name ? "" : $name.'::';
Branch: 1 branch
BranchTrueFalseTotal
$action_name ? : 4 4 8
249
250 8 push @list, map { $tpl.$_ } $action->methods;
251 }
252
253 5 ▶ return wantarray ? @list : shift @list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
254 }
255  
256 ### PUBLIC CLASS METHOD ###
257 #
258 # Returns parameters for given action and method name
259 # with ExtDirect attribute.
260 #
261 # Returns full attribute hash in list context.
262 # Croaks if called in scalar context.
263 #
264 # DEPRECATED. See RPC::ExtDirect::API for replacement.
265 #
266  
267 sub get_method_parameters {
268 15 ▶ my ($class, $action_name, $method_name) = @_;
Subroutine get_method_parameters: called CC=6 SCAR=19.0
Pod undocumented
269
270 15 ▶ croak "Wrong context" unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 15 15
271
272 15 ▶ croak "ExtDirect action name is required" unless defined $action_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action_name 0 15 15
273 15 ▶ croak "ExtDirect method name is required" unless defined $method_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method_name 0 15 15
274
275 15 my $action = $class->get_api->get_action_by_name($action_name);
276
277 15 ▶ croak "Can't find ExtDirect action $action"
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 15 15
278 unless $action;
279
280 15 my $method = $action->method($method_name);
281  
282 15 ▶ croak "Can't find ExtDirect properties for method $method_name"
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 15 15
283 unless $method;
284
285 15 return $method->get_api_definition_compat();
286 }
287  
288 22 1;
« ExtDirect/Config.pm ↑ Summary ExtDirect/API/Hook.pm »