File Coverage

lib/Ubic/Service/Common.pm
Criterion Covered Total %
statement 43 48 89.5
branch 5 12 41.6
condition n/a
subroutine 14 15 93.3
pod 10 10 100.0
total 72 85 84.7


line stmt bran cond sub pod time code
1             package Ubic::Service::Common;
2             $Ubic::Service::Common::VERSION = '1.58_01'; # TRIAL
3 6     6   48428 use strict;
  6         14  
  6         210  
4 6     6   31 use warnings;
  6         9  
  6         273  
5              
6             # ABSTRACT: common way to construct new service by specifying several callbacks
7              
8              
9 6     6   34 use Params::Validate qw(:all);
  6         8  
  6         1768  
10              
11 6     6   32 use parent qw(Ubic::Service::Skeleton);
  6         8  
  6         78  
12              
13 6     6   424 use Carp;
  6         11  
  6         6603  
14              
15             sub new {
16 12     12 1 73473 my $class = shift;
17 12         653 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 12 50       293 if ($params->{custom_commands}) {
29 12         26 for (keys %{$params->{custom_commands}}) {
  12         56  
30 8 50       36 ref($params->{custom_commands}{$_}) eq 'CODE' or croak "Callback expected at custom command $_";
31             }
32             }
33 12         61 my $self = bless {%$params} => $class;
34 12         127 return $self;
35             }
36              
37             sub port {
38 0     0 1 0 my $self = shift;
39 0         0 return $self->{port};
40             }
41              
42             sub status_impl {
43 30     30 1 36 my $self = shift;
44 30         85 return $self->{status}->();
45             }
46              
47             sub start_impl {
48 5     5 1 11 my $self = shift;
49 5         12 return $self->{start}->();
50             }
51              
52             sub stop_impl {
53 8     8 1 13 my $self = shift;
54 8         27 return $self->{stop}->();
55             }
56              
57             sub timeout_options {
58 8     8 1 18 my $self = shift;
59 8         99 return $self->{timeout_options};
60             }
61              
62             sub custom_commands {
63 2     2 1 7 my $self = shift;
64 2         6 return keys %{$self->{custom_commands}};
  2         17  
65             }
66              
67             sub user {
68 17     17 1 33 my $self = shift;
69 17 50       48 return $self->{user} if defined $self->{user};
70 17         90 return $self->SUPER::user();
71             }
72              
73             # copypasted from Ubic::Service::SimpleDaemon... maybe we need moose after all
74             sub group {
75 17     17 1 27 my $self = shift;
76 17         28 my $groups = $self->{group};
77 17 50       77 return $self->SUPER::group() if not defined $groups;
78 0 0       0 return @$groups if ref $groups eq 'ARRAY';
79 0         0 return $groups;
80             }
81              
82             sub do_custom_command {
83 4     4 1 11 my ($self, $command) = @_;
84 4 50       24 unless (exists $self->{custom_commands}{$command}) {
85 0         0 croak "Command '$command' not implemented";
86             }
87 4         20 $self->{custom_commands}{$command}->();
88             }
89              
90             1;
91              
92             __END__