ExtDirect/API/Action.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 62.5% 5 / 8 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 93.7% 15 / 16 Podpod 100% 10 / 10 totaltotal 88.6% 109 / 123 CC 11 SCAR 11.1
CC
11
Cov
89%
new
6.1
merge
6.0
add_method
3.1
CRAP 3.0
« ExtDirect/Event.pm ↑ Summary ExtDirect/Demo/Profile.pm »
1 package RPC::ExtDirect::API::Action;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 26 ▶use RPC::ExtDirect::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use RPC::ExtDirect::Util::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ### PUBLIC CLASS METHOD (ACCESSOR) ###
13 #
14 # Return the hook types supported by this Action class
15 #
16  
17 110 ▶sub HOOK_TYPES { qw/ before instead after / }
Subroutine HOOK_TYPES: called CC=1 SCAR=0.0
Pod documented
18  
19 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
20 #
21 # Create a new Action instance
22 #
23  
24 84 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
25 84 my ($class, %arg) = @_;
26
27 84 my $config = delete $arg{config};
28 84 my $hook_class = $config->api_hook_class;
29
30 # For the caller, the 'action' parameter makes sense as the Action's
31 # name, but within the Action itself it's just "name" for clarity
32 84 my $name = delete $arg{action};
33 84 my $package = delete $arg{package};
34 84 ▶ my $methods = delete $arg{methods} || [];
Condition50%1/2
delete $arg{'methods'} || []
l!l
084
MC/DC0%0/1
delete $arg{'methods'} || []
delete $arg{'methods'}
Truth table50%1/2
A
A= delete $arg{'methods'}
Aresult
11
00
35
36 # These checks are mostly for debugging
37 84 ▶ croak "Can't create an Action without a name!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 84 84
38 unless defined $name;
39
40 # We accept :: in Action names so that the API would feel
41 # more natural on the Perl side, but convert them to dots
42 # anyway to be compatible with JavaScript
43 84 $name =~ s/::/./g;
44
45 # We avoid hard binding on the hook class
46 84 { local $@; eval "require $hook_class"; }
47
48 84 my %hooks;
49
50 84 for my $type ( $class->HOOK_TYPES ) {
51 252 my $hook = delete $arg{ $type };
52
53 252 ▶ $hooks{ $type } = $hook_class->new( type => $type, code => $hook )
Branch: 1 branch
BranchTrueFalseTotal
if $hook 0 252 252
54 if $hook;
55 }
56
57 84 my $self = bless {
58 config => $config,
59 name => $name,
60 package => $package,
61 methods => {},
62 %arg,
63 %hooks,
64 }, $class;
65
66 84 $self->add_method($_) for @$methods;
67
68 84 return $self;
69 }
70  
71 ### PUBLIC INSTANCE METHOD ###
72 #
73 # Merge method definitions from incoming Action object
74 #
75  
76 0 ▶sub merge {
Subroutine merge: not called CC=2 SCAR=17.9
Pod documented
77 0 my ($self, $action) = @_;
78
79 # Add the methods, or replace if they exist
80 0 $self->add_method(@_) for $action->methods();
81
82 0 return $self;
83 }
84  
85 ### PUBLIC INSTANCE METHOD ###
86 #
87 # Return the list of this Action's Methods' names
88 #
89  
90 88 ▶sub methods { keys %{ $_[0]->{methods} } }
Subroutine methods: called CC=1 SCAR=0.0
Pod documented
91  
92 ### PUBLIC INSTANCE METHOD ###
93 #
94 # Return the list of this Action's publishable
95 # (non-pollHandler) methods
96 #
97  
98 33 ▶sub remoting_methods {
Subroutine remoting_methods: called CC=1 SCAR=0.0
Pod documented
99 33 my ($self) = @_;
100
101 158 my @method_names = map { $_->[0] }
102 163 grep { !$_->[1]->pollHandler }
103 33 map { [ $_, $self->method($_) ] }
104 $self->methods;
105
106 33 return @method_names;
107 }
108  
109 ### PUBLIC INSTANCE METHOD ###
110 #
111 # Return the list of this Action's pollHandler methods
112 #
113  
114 47 ▶sub polling_methods {
Subroutine polling_methods: called CC=1 SCAR=0.0
Pod documented
115 47 my ($self) = @_;
116
117 15 my @method_names = map { $_->[0] }
118 208 grep { $_->[1]->pollHandler }
119 47 map { [ $_, $self->method($_) ] }
120 $self->methods;
121
122 47 return @method_names;
123 }
124  
125 ### PUBLIC INSTANCE METHOD ###
126 #
127 # Return the list of API definitions for this Action's
128 # remoting methods
129 #
130  
131 33 ▶sub remoting_api {
Subroutine remoting_api: called CC=3 SCAR=11.0
Pod documented
132 33 my ($self, $env) = @_;
133
134 # Guard against user overrides returning undefs instead of
135 # empty lists
136 33 my @method_names = $self->remoting_methods;
137 33 my @method_defs;
138
139 33 for my $method_name ( @method_names ) {
140 158 my $method = $self->method($method_name);
141 158 my $def = $method->get_api_definition($env);
142
143 158 ▶ push @method_defs, $def if $def;
Branch: 1 branch
BranchTrueFalseTotal
if $def 149 9 158
144 }
145
146 33 return @method_defs;
147 }
148  
149 ### PUBLIC INSTANCE METHOD ###
150 #
151 # Return true if this Action has any pollHandler methods
152 #
153  
154 26 ▶sub has_pollHandlers {
Subroutine has_pollHandlers: called CC=1 SCAR=0.0
Pod documented
155 26 my ($self, $env) = @_;
156
157 # By default we're not using the env object here,
158 # but an user override may do so
159
160 26 my @methods = $self->polling_methods;
161
162 26 return !!@methods;
163 }
164  
165 ### PUBLIC INSTANCE METHOD ###
166 #
167 # Add a method, or replace it if exists.
168 # Accepts Method instances, or hashrefs to be fed
169 # to Method->new()
170 #
171  
172 364 ▶sub add_method {
Subroutine add_method: called CC=3 SCAR=11.4
Pod documented
173 364 my ($self, $method) = @_;
174
175 364 my $config = $self->config;
176
177 364 ▶ if ( 'HASH' eq ref $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $method) { } 364 0 364
178 364 my $m_class = $config->api_method_class();
179
180 # This is to avoid hard binding on RPC::ExtDirect::API::Method
181 364 eval "require $m_class";
182
183 364 ▶ my $name = delete $method->{method} || delete $method->{name};
Condition33%1/3
delete $method->{'method'} || delete $method->{'name'}
l!l&&r!l&&!r
36400
MC/DC0%0/2
delete $method->{'method'} || delete $method->{'name'}
delete $method->{'method'} delete $method->{'name'}
Truth table33%1/3
A || B
A= delete $method->{'method'}B= delete $method->{'name'}
ABresult
1X1
011
000
184
185 364 $method = $m_class->new(
186 config => $config,
187 package => $self->package,
188 action => $self->name,
189 name => $name,
190 %$method,
191 );
192 }
193 else {
194 0 $method->config($config);
195 }
196
197 363 my $m_name = $method->name;
198
199 363 $self->{methods}->{ $m_name } = $method;
200
201 363 return $self;
202 }
203  
204 ### PUBLIC INSTANCE METHOD ###
205 #
206 # Returns a Method object by name
207 #
208  
209 1054 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod documented
210 1054 my ($self, $method_name) = @_;
211  
212 1054 return $self->{methods}->{ $method_name };
213 }
214  
215 ### PUBLIC INSTANCE METHODS ###
216 #
217 # Simple read-write accessors
218 #
219  
220 26 my $accessors = [qw/
221 config
222 name
223 package
224 /,
225 __PACKAGE__->HOOK_TYPES,
226 ];
227  
228 26 RPC::ExtDirect::Util::Accessor::mk_accessors(
229 simple => $accessors,
230 );
231  
232 26 1;
« ExtDirect/Event.pm ↑ Summary ExtDirect/Demo/Profile.pm »