lib/Ubic/Service/Common.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 41.6% 5 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 10 / 10 totaltotal 85.0% 74 / 87 SCAR 21.4
CC
8
Cov
81%
group
4.5
new
4.1
do_custom_command
2.1
CRAP 8.5
« lib/Ubic/Ping/Service.pm ↑ Summary lib/Ubic/AtomicFile.pm »
1 package Ubic::Service::Common;
2 97 $Ubic::Service::Common::VERSION = '1.59';
3 97 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 97 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: common way to construct new service by specifying several callbacks
7  
8  
9 97 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 97 ▶use parent qw(Ubic::Service::Skeleton);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 97 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 443 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
17 443 my $params = validate(@_, {
18 start => { type => CODEREF },
19 stop => { type => CODEREF },
20 status => { type => CODEREF },
21 name => { type => SCALAR, regex => qr/^[\w-]+$/, optional => 1 }, # violates Ubic::Service encapsulation...
22 port => { type => SCALAR, regex => qr/^\d+$/, optional => 1 },
23 custom_commands => { type => HASHREF, default => {} },
24 user => { type => SCALAR, optional => 1 },
25 group => { type => SCALAR | ARRAYREF, optional => 1 },
26 timeout_options => { type => HASHREF, default => {} },
27 });
28 443 ▶ if ($params->{custom_commands}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"custom_commands"}) 443 0 443
29 443 for (keys %{$params->{custom_commands}}) {
30 180 ▶ ref($params->{custom_commands}{$_}) eq 'CODE' or croak "Callback expected at custom command $_";
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{"custom_commands"}{$_} eq "CODE" or croak("Callback expected at custom command $_") 0 180 180
31 }
32 }
33 443 my $self = bless {%$params} => $class;
34 443 return $self;
35 }
36  
37 sub port {
38 0 ▶ my $self = shift;
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
39 0 return $self->{port};
40 }
41  
42 sub status_impl {
43 51 ▶ my $self = shift;
Subroutine status_impl: called CC=1 SCAR=0.0
Pod documented
44 51 return $self->{status}->();
45 }
46  
47 sub start_impl {
48 6 ▶ my $self = shift;
Subroutine start_impl: called CC=1 SCAR=0.0
Pod documented
49 6 return $self->{start}->();
50 }
51  
52 sub stop_impl {
53 12 ▶ my $self = shift;
Subroutine stop_impl: called CC=1 SCAR=0.0
Pod documented
54 12 return $self->{stop}->();
55 }
56  
57 sub timeout_options {
58 13 ▶ my $self = shift;
Subroutine timeout_options: called CC=1 SCAR=0.0
Pod documented
59 13 return $self->{timeout_options};
60 }
61  
62 sub custom_commands {
63 2 ▶ my $self = shift;
Subroutine custom_commands: called CC=1 SCAR=0.0
Pod documented
64 2 return keys %{$self->{custom_commands}};
65 }
66  
67 sub user {
68 153 ▶ my $self = shift;
Subroutine user: called CC=2 SCAR=7.1
Pod documented
69 153 ▶ return $self->{user} if defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"} 0 153 153
70 153 return $self->SUPER::user();
71 }
72  
73 # copypasted from Ubic::Service::SimpleDaemon... maybe we need moose after all
74 sub group {
75 153 ▶ my $self = shift;
Subroutine group: called CC=3 SCAR=15.1
Pod documented
76 153 my $groups = $self->{group};
77 153 ▶ return $self->SUPER::group() if not defined $groups;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $groups 153 0 153
78 0 ▶ return @$groups if ref $groups eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $groups eq "ARRAY" 0 0 0
79 0 return $groups;
80 }
81  
82 sub do_custom_command {
83 3 ▶ my ($self, $command) = @_;
Subroutine do_custom_command: called CC=2 SCAR=7.6
Pod documented
84 3 ▶ unless (exists $self->{custom_commands}{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"custom_commands"}{$command}) 0 3 3
85 0 croak "Command '$command' not implemented";
86 }
87 3 $self->{custom_commands}{$command}->();
88 }
89  
90 97 1;
91  
92 __END__
« lib/Ubic/Ping/Service.pm ↑ Summary lib/Ubic/AtomicFile.pm »