Lim/Component.pm

filter: Statementstmt 30.4% 38 / 125 Branchbran 3.5% 2 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.8% 11 / 19 Podpod 100% 7 / 7 totaltotal 27.3% 58 / 212 SCAR 63.2
CC
33
Cov
22%
Client
758.6
CLI
12.0
__ANON__
6.0
CRAP 555.0
« Lim.pm ↑ Summary Lim/RPC/Protocol/REST.pm »
1 package Lim::Component;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Lim::RPC::Value ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Lim::RPC::Value::Collection ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Lim::RPC::Call ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Lim::Component - Base class for plugins
19  
20 =head1 VERSION
21  
22 See L<Lim> for version.
23  
24 =cut
25  
26 10 our $VERSION = $Lim::VERSION;
27  
28 =head1 SYNOPSIS
29  
30 =over 4
31  
32 package Lim::Plugin::MyPlugin;
33  
34 use base qw(Lim::Component);
35  
36 sub Module {
37 'MyPlugin';
38 }
39  
40 sub Calls {
41 {
42 ReadVersion => {
43 out => {
44 version => 'string'
45 }
46 }
47 };
48 }
49  
50 sub Commands {
51 {
52 version => 1
53 };
54 }
55  
56 =back
57  
58 =head1 DESCRIPTION
59  
60 This is the base class of all plugins in Lim. It defines the name, RPC calls and
61 CLI commands. It must be present for any plugin to work but the different plugin
62 parts does not have to exist everywhere. For example the CLI part does not have
63 to have the Server and Client but it will most likly have the Client part if you
64 want to communicate with the Server.
65  
66 =head1 METHODS
67  
68 =over 4
69  
70 =item $plugin_name = Lim::Plugin::MyPlugin->Name
71  
72 Returns the plugin's name.
73  
74 This function must be overloaded or it will L<confess>.
75  
76 =cut
77  
78 sub Name {
79 0 ▶ confess 'Name not overloaded';
Subroutine Name: not called CC=1 SCAR=6.9
Pod documented
80 }
81  
82 =item $plugin_description = Lim::Plugin::MyPlugin->Description
83  
84 Returns the plugin's description.
85  
86 =cut
87  
88 sub Description {
89 0 ▶ 'No description for this plugin';
Subroutine Description: not called CC=1 SCAR=6.9
Pod documented
90 }
91  
92 =item $call_hash_ref = Lim::Plugin::MyPlugin->Calls
93  
94 Returns a hash reference to the calls that can be made to this plugin, used both
95 in Server and Client to verify input and output arguments.
96  
97 Read more about this in L<Lim::Component::Server>.
98  
99 This function must be overloaded or it will L<confess>.
100  
101 =cut
102  
103 sub Calls {
104 0 ▶ confess 'Calls not overloaded';
Subroutine Calls: not called CC=1 SCAR=6.9
Pod documented
105 }
106  
107 =item $command_hash_ref = Lim::Plugin::MyPlugin->Commands
108  
109 Returns a hash reference to the CLI commands that can be made by this plugin.
110  
111 This function must be overloaded or it will L<confess>.
112  
113 =cut
114  
115 sub Commands {
116 0 ▶ confess 'Commands not overloaded';
Subroutine Commands: not called CC=1 SCAR=6.9
Pod documented
117 }
118  
119 =item $cli = Lim::Plugin::MyPlugin->CLI
120  
121 Create a CLI object of the plugin, read more about this in
122 L<Lim::Component::CLI>.
123  
124 =cut
125  
126 sub CLI {
127 0 ▶ my $self = shift;
Subroutine CLI: not called CC=3 SCAR=24.8
Pod documented
128
129 0 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
130 0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
131 }
132 0 $self .= '::CLI';
133
134 0 eval 'use '.$self.' ();';
135 0 ▶ die $self.' : '.$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
136 0 $self->new(@_);
137 }
138  
139 =item $client = Lim::Plugin::MyPlugin->Client
140  
141 Create a Client object of the plugin, read more about this in
142 L<Lim::Component::Client>.
143  
144 =cut
145  
146 sub Client {
147 0 ▶ my $self = shift;
Subroutine Client: not called CC=28 SCAR=66.3
Pod documented
148
149 0 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
150 0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
151 }
152 0 my $calls = $self->Calls;
153 0 my $plugin = $self->Name;
154 0 $self .= '::Client';
155
156 0 eval 'use '.$self.' ();';
157 0 ▶ die $self.' : '.$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
158  
159 0 ▶ if ($self->can('__lim_bootstrapped')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("__lim_bootstrapped")) 0 0 0
160 0 return $self->new(@_);
161 }
162
163 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 0 foreach my $call (keys %$calls) {
165 0 ▶ unless ($self->can($call)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($call)) 0 0 0
166 0 my $sub = $self.'::'.$call;
167 0 my $call_def = $calls->{$call};
168  
169 0 ▶ unless (ref($call_def) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def eq "HASH") 0 0 0
170 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid definition';
171 }
172
173 0 ▶ if (exists $call_def->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call_def->{"in"}) 0 0 0
174 0 ▶ unless (ref($call_def->{in}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def->{"in"} eq "HASH") 0 0 0
175 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
176 }
177
178 0 my @keys = keys %{$call_def->{in}};
179 0 ▶ unless (scalar @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @keys) 0 0 0
180 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
181 }
182
183 0 my @values = ($call_def->{in});
184 0 ▶ while (defined (my $value = shift(@values))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = shift @values)) 0 0 0
185 0 foreach my $key (keys %$value) {
186 0 ▶ if (ref($value->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value->{$key} eq 'HASH') { } 0 0 0
elsif (builtin::blessed($value->{$key})) { } 0 0 0
187 0 ▶ if (exists $value->{$key}->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$key}{''}) { } 0 0 0
188 0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
189 0 delete $value->{$key}->{''};
190 0 $value->{$key} = $collection->set_children($value->{$key});
191 0 push(@values, $value->{$key}->children);
192 }
193 else {
194 0 push(@values, $value->{$key});
195 }
196 0 next;
197 }
198 elsif (blessed $value->{$key}) {
199 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value")) 0 0 0
200 0 next;
201 }
202 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value::Collection")) 0 0 0
203 0 push(@values, $value->{$key}->children);
204 0 next;
205 }
206 }
207 else {
208 0 $value->{$key} = Lim::RPC::Value->new($value->{$key});
209 0 next;
210 }
211  
212 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid in parameter definition';
213 }
214 }
215 }
216
217 0 ▶ if (exists $call_def->{out}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call_def->{"out"}) 0 0 0
218 0 ▶ unless (ref($call_def->{out}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $call_def->{"out"} eq "HASH") 0 0 0
219 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
220 }
221
222 0 my @keys = keys %{$call_def->{out}};
223 0 ▶ unless (scalar @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @keys) 0 0 0
224 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
225 }
226  
227 0 my @values = ($call_def->{out});
228 0 ▶ while (defined $calls and (my $value = shift(@values))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $calls and my $value = shift @values) 0 0 0
Condition0%0/3
defined $calls and my $value = shift @values
!ll&&!rl&&r
000
MC/DC0%0/2
defined $calls and my $value = shift @values
defined $calls my $value = shift @values
Truth table0%0/3
A and B
A= defined $callsB= my $value = shift @values
ABresult
0X0
100
111
229 0 foreach my $key (keys %$value) {
230 0 ▶ if (ref($value->{$key}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value->{$key} eq 'HASH') { } 0 0 0
elsif (builtin::blessed($value->{$key})) { } 0 0 0
231 0 ▶ if (exists $value->{$key}->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$key}{''}) { } 0 0 0
232 0 my $collection = Lim::RPC::Value::Collection->new($value->{$key}->{''});
233 0 delete $value->{$key}->{''};
234 0 $value->{$key} = $collection->set_children($value->{$key});
235 0 push(@values, $value->{$key}->children);
236 }
237 else {
238 0 push(@values, $value->{$key});
239 }
240 0 next;
241 }
242 elsif (blessed $value->{$key}) {
243 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value")) 0 0 0
244 0 next;
245 }
246 0 ▶ if ($value->{$key}->isa('Lim::RPC::Value::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{$key}->isa("Lim::RPC::Value::Collection")) 0 0 0
247 0 push(@values, $value->{$key}->children);
248 0 next;
249 }
250 }
251 else {
252 0 $value->{$key} = Lim::RPC::Value->new($value->{$key});
253 0 next;
254 }
255  
256 0 confess __PACKAGE__, ': Can not create client: call ', $call, ' has invalid out parameter definition';
257 }
258 }
259 }
260
261 *$sub = sub {
262 0 ▶ unless (Lim::RPC::Call->new($plugin, $call, $call_def, @_)) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ("Lim::RPC::Call"->new($plugin, $call, $call_def, @_)) 0 0 0
263 0 confess __PACKAGE__, ': Unable to create Lim::RPC::Call for ', $sub;
264 }
265 0 };
266 }
267 }
268
269 0 my $sub = $self.'::__lim_bootstrapped';
270 *$sub = sub {
271 0 ▶ 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 0 };
273
274 0 $self->new(@_);
275 }
276  
277 =item $client = Lim::Plugin::MyPlugin->Server
278  
279 Create a Server object of the plugin, read more about this in
280 L<Lim::Component::Server>.
281  
282 =cut
283  
284 sub Server {
285 4 ▶ my $self = shift;
Subroutine Server: called CC=3 SCAR=11.3
Pod documented
286
287 4 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 4 4
288 0 confess __PACKAGE__, ': Should not be called with refered/blessed argument';
289 }
290 4 $self .= '::Server';
291
292 4 ▶ eval 'use '.$self.' ();';
Subroutine BEGIN: called CC=1 SCAR=0.0
293 4 ▶ die $self.' : '.$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
294 4 $self->new(@_);
295 }
296  
297 =back
298  
299 =head1 AUTHOR
300  
301 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
302  
303 =head1 BUGS
304  
305 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
306  
307 =head1 SUPPORT
308  
309 You can find documentation for this module with the perldoc command.
310  
311 perldoc Lim::Component
312  
313 You can also look for information at:
314  
315 =over 4
316  
317 =item * Lim issue tracker (report bugs here)
318  
319 L<https://github.com/jelu/lim/issues>
320  
321 =back
322  
323 =head1 ACKNOWLEDGEMENTS
324  
325 =head1 LICENSE AND COPYRIGHT
326  
327 Copyright 2012-2013 Jerry Lundström.
328  
329 This program is free software; you can redistribute it and/or modify it
330 under the terms of either: the GNU General Public License as published
331 by the Free Software Foundation; or the Artistic License.
332  
333 See http://dev.perl.org/licenses/ for more information.
334  
335  
336 =cut
337  
338 10 1; # End of Lim::Component
« Lim.pm ↑ Summary Lim/RPC/Protocol/REST.pm »