NetSDS/Class/Abstract.pm

filter: Statementstmt 42.3% 22 / 52 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 46.1% 6 / 13 Podpod 100% 7 / 7 totaltotal 35.7% 35 / 98 CC 11 SCAR 21.4
CC
11
Cov
31%
error
20.0
use_modules
12.0
log
12.0
CRAP 8.5
« NetSDS/Conf.pm ↑ Summary NetSDS/Translate.pm »
1 #===============================================================================
2 #
3 # FILE: Abstract.pm
4 #
5 # DESCRIPTION: Abstract Class for other NetSDS code
6 #
7 # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
8 # COMPANY: Net.Style
9 # CREATED: 24.04.2008 11:42:42 EEST
10 #===============================================================================
11  
12 =head1 NAME
13  
14 NetSDS::Class::Abstract - superclass for all NetSDS APIs
15  
16 =head1 SYNOPSIS
17  
18 package MyClass;
19 use base 'NetSDS::Class::Abstract';
20  
21 __PACKAGE__->mk_accessors(qw/my_field/);
22  
23 sub error_sub {
24 my ($self) = @_;
25 if (!$self->my_field) {
26 return $self->error("No my_field defined");
27 }
28 }
29  
30 1;
31  
32 =head1 DESCRIPTION
33  
34 C<NetSDS::Class::Abstract> is a superclass for all other NetSDS classes, containing the following functionality:
35  
36 =over
37  
38 =item * common class constructor
39  
40 =item * safe modules inclusion
41  
42 =item * class and objects accessors
43  
44 =item * logging
45  
46 =item * error handling;
47  
48 =back
49  
50 All other class/object APIs should inherit this class to use it's functionality in standard way.
51  
52 =cut
53  
54 package NetSDS::Class::Abstract;
55  
56 3 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 3 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
61 Class::Accessor::Class
62 3 );
63  
64 # Error handling class variables
65 3 our $_ERRSTR; # error string
66 3 our $_ERRCODE; # error code
67  
68 3 ▶use Data::Structure::Util; # unblessing objects
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 3 ▶use version; our $VERSION = '1.301';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 #***********************************************************************
73  
74 =head1 CONSTRUCTOR, INITIALIZATION, APPLICATION
75  
76 =over
77  
78 =item B<new(%params)> - common constructor
79  
80 C<new()> method implements common constructor for NetSDS classes.
81 Constructor may be overwriten in inherited classes and usually
82 this happens to implement module specific functionality.
83  
84 Constructor requres parameters as hash that are set as object properties.
85  
86 my $object = NetSDS::SomeClass->new(
87 foo => 'abc',
88 bar => 'def',
89 );
90  
91 =cut
92  
93 #-----------------------------------------------------------------------
94 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
95  
96 0 my ( $proto, %params ) = @_;
97 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
98  
99 0 my $self = \%params;
100  
101 0 bless( $self, $class );
102  
103 0 return $self;
104  
105 }
106  
107 #***********************************************************************
108  
109 =item B<mk_class_accessors(@properties)> - class properties accessor
110  
111 See L<Class::Accessor> for details.
112  
113 __PACKAGE__->mk_class_accessors('foo', 'bar');
114  
115 =item B<mk_accessors(@propertire)> - object properties accessors
116  
117 See L<Class::Accessor::Class> for details.
118  
119 $self->mk_accessors('foo', 'bar');
120  
121 Other C<Class::Accessor::Class> methods available as well.
122  
123 =cut
124  
125 #-----------------------------------------------------------------------
126  
127 #***********************************************************************
128  
129 =item B<use_modules(@modules_list)> - load modules on demand
130  
131 C<use_modules()> provides safe on demand modules loader.
132 It requires list of modules names as parameters
133  
134 Return 1 in case of success or C<undef> if faied. Error messages in case
135 of failure are available using C<errstr()> call.
136  
137 Example:
138  
139 # Load modules for daemonization
140 if ($daemon_mode) {
141 $self->use_modules("Proc::Daemon", "Proc::PID::File");
142 }
143  
144 =cut
145  
146 #-----------------------------------------------------------------------
147 0 ▶sub use_modules {
Subroutine use_modules: not called CC=3 SCAR=24.8
Pod documented
148  
149 0 my $self = shift(@_);
150  
151 0 foreach my $mod (@_) {
152 0 eval "use $mod;";
153 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
154 0 return $self->error($@);
155 }
156 }
157  
158 0 return 1;
159  
160 }
161  
162 #***********************************************************************
163  
164 =item B<unbless()> - return unblessed object
165  
166 Return unblessed data structure of object that may be used when some
167 code requires non blessed structures (like JSON serialization).
168  
169 Example:
170  
171 my $var = $obj->unbless();
172  
173 =cut
174  
175 #-----------------------------------------------------------------------
176 0 ▶sub unbless {
Subroutine unbless: not called CC=1 SCAR=6.9
Pod documented
177  
178 0 my ($self) = @_;
179 0 return Data::Structure::Util::unbless($self);
180 }
181  
182 #***********************************************************************
183  
184 =back
185  
186 =head1 LOGGING
187  
188 =over
189  
190 =item B<logger()> - get/set logging handler
191  
192 C<logger> property is an object that should provide functionality
193 handling log messaging. Usually it's object of L<NetSDS::Logger>
194 class or C<undef>. However it may another object implementing
195 non-standard features like sending log to e-mail or to DBMS.
196  
197 Example:
198  
199 # Set logger and send log message
200 $obj->logger(NetSDS::Logger->new());
201 $obj->log("info", "Logger connected");
202  
203 =cut
204  
205 #-----------------------------------------------------------------------
206  
207 3 __PACKAGE__->mk_accessors('logger'); # Logger
208  
209 #***********************************************************************
210  
211 =item B<log($level, $message)> - write log message
212  
213 Paramters: log level, log message
214  
215 $obj->log("info", "We still alive");
216  
217 =cut
218  
219 #-----------------------------------------------------------------------
220  
221 0 ▶sub log {
Subroutine log: not called CC=3 SCAR=24.8
Pod documented
222  
223 0 my ( $self, $level, $msg ) = @_;
224  
225 # Logger expected to provide "log()" method
226 0 ▶ if ( $self->logger() and $self->logger()->can('log') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logger and $self->logger->can('log')) { } 0 0 0
Condition0%0/3
$self->logger and $self->logger->can("log")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->logger and $self->logger->can("log")
$self->logger $self->logger->can("log")
Truth table0%0/3
A and B
A= $self->loggerB= $self->logger->can("log")
ABresult
0X0
100
111
227 0 $self->logger->log( $level, $msg );
228 } else {
229 0 warn "[$level] $msg\n";
230 }
231 }
232  
233 #***********************************************************************
234  
235 =back
236  
237 =head1 ERROR HANDLING
238  
239 =over
240  
241 =item B<error($msg, [$code])> - set error message and code
242  
243 C<error()> method set error message and optional error code.
244 It can be invoked in both class and object contexts.
245  
246 Example 1: set class error
247  
248 NetSDS::Foo->error("Mistake found");
249  
250 Example 2: set object error with code
251  
252 $obj->error("Can't launch rocket", BUG_STUPID);
253  
254 =cut
255  
256 #-----------------------------------------------------------------------
257  
258 0 ▶sub error {
Subroutine error: not called CC=4 SCAR=30.0
Pod documented
259  
260 0 my ( $self, $msg, $code ) = @_;
261  
262 0 ▶ $msg ||= ''; # error message
Condition0%0/2
$msg ||= ""
l!l
00
MC/DC0%0/1
$msg ||= ""
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
263 0 ▶ $code ||= ''; # error code
Condition0%0/2
$code ||= ""
l!l
00
MC/DC0%0/1
$code ||= ""
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
264  
265 0 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
266 0 $self->{_errstr} = $msg;
267 0 $self->{_errcode} = $code;
268 } else {
269 0 $_ERRSTR = $msg;
270 0 $_ERRCODE = $code;
271 }
272  
273 0 return undef;
274 }
275  
276 #***********************************************************************
277  
278 =item B<errstr()> - retrieve error message
279  
280 C<errstr()> method returns error string in both object and class contexts.
281  
282 Example:
283  
284 warn "We have an error: " . $obj->errstr;
285  
286 =cut
287  
288 #-----------------------------------------------------------------------
289  
290 0 ▶sub errstr {
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
291  
292 0 my $self = shift;
293 0 ▶ return ref($self) ? $self->{_errstr} : $_ERRSTR;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
294  
295 }
296  
297 #***********************************************************************
298  
299 =item B<errcode()> - retrieve error code
300  
301 C<errcode()> method returns error code in both object and class contexts.
302  
303 Example:
304  
305 if ($obj->errcode == 42) {
306 print "Epic fail! We've found an answer!";
307 }
308  
309 =cut
310  
311 #-----------------------------------------------------------------------
312  
313 0 ▶sub errcode {
Subroutine errcode: not called CC=2 SCAR=17.9
Pod documented
314  
315 0 my $self = shift;
316 0 ▶ return ref($self) ? $self->{_errcode} : $_ERRCODE;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
317  
318 }
319  
320 3 1;
321  
322 __END__
« NetSDS/Conf.pm ↑ Summary NetSDS/Translate.pm »