File Coverage

lib/Ubic/Result/Class.pm
Criterion Covered Total %
statement 48 51 94.1
branch 15 20 75.0
condition 1 2 50.0
subroutine 12 13 92.3
pod 4 4 100.0
total 80 90 88.8


line stmt bran cond sub pod time code
1             package Ubic::Result::Class;
2             $Ubic::Result::Class::VERSION = '1.60';
3 39     39   118 use strict;
  39         33  
  39         780  
4 39     39   92 use warnings;
  39         48  
  39         3414  
5              
6             # ABSTRACT: ubic result object
7              
8              
9             use overload '""' => sub {
10 347     347   4386 my $self = shift;
11 347         599 return $self->as_string;
12             }, 'eq' => sub {
13 29     29   4296 return ("$_[0]" eq "$_[1]")
14             }, 'ne' => sub {
15 0     0   0 return ("$_[0]" ne "$_[1]")
16 39     39   25652 };
  39         21221  
  39         355  
17              
18 39     39   2617 use Params::Validate qw(:all);
  39         6427  
  39         4662  
19 39     39   155 use Carp;
  39         39  
  39         1623  
20 39     39   1216 use parent qw(Class::Accessor::Fast);
  39         653  
  39         165  
21              
22             __PACKAGE__->mk_accessors(qw/ type msg /);
23              
24             sub new {
25 168     168 1 240 my $class = shift;
26 168         3161 my $self = validate(@_, {
27             type => { type => SCALAR, optional => 1 },
28             msg => { optional => 1 },
29             cached => { optional => 1 },
30             });
31 168   50     831 $self->{type} ||= 'unknown';
32 168         916 return bless $self => $class;
33             }
34              
35             sub status {
36 232     232 1 253 my $self = shift;
37 232 50       469 croak 'status() is read-only method' if @_;
38 232 100       359 if (grep { $_ eq $self->{type} } ('running', 'already running', 'started', 'already started', 'restarted', 'reloaded', 'stopping')) {
  1624 100       2486  
    50          
    100          
39 100         383 return 'running';
40             }
41 396         566 elsif (grep { $_ eq $self->{type} } ('not running', 'stopped', 'starting')) {
42 124         393 return 'not running';
43             }
44 8         22 elsif (grep { $_ eq $self->{type} } ('down')) {
45 0         0 return 'down';
46             }
47 8         18 elsif (grep { $_ eq $self->{type} } ('autostarting')) {
48 1         9 return 'autostarting';
49             }
50             else {
51 7         28 return 'broken';
52             }
53             }
54              
55             sub action {
56 6     6 1 37 my $self = shift;
57 6 50       18 croak 'action() is read-only method' if @_;
58 6 100       15 if (grep { $_ eq $self->{type} } ('started', 'stopped', 'reloaded')) {
  18         47  
59 5         49 return $self->{type};
60             }
61 1         3 return 'none';
62             }
63              
64             sub as_string {
65 347     347 1 312 my $self = shift;
66 347 50       581 my $cached_str = ($self->{cached} ? ' [cached]' : '');
67 347 100       534 if (defined $self->{msg}) {
68 159 50       242 if ($self->{type} eq 'unknown') {
69 0         0 return "$self->{msg}\n";
70             }
71             else {
72 159         700 return "$self->{type} ($self->{msg})".$cached_str;
73             }
74             }
75             else {
76 188         351 return $self->type.$cached_str;
77             }
78             }
79              
80              
81             1;
82              
83             __END__