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.58_01'; # TRIAL
3 42     42   186 use strict;
  42         60  
  42         1306  
4 42     42   184 use warnings;
  42         66  
  42         5873  
5              
6             # ABSTRACT: ubic result object
7              
8              
9             use overload '""' => sub {
10 462     462   7016 my $self = shift;
11 462         1319 return $self->as_string;
12             }, 'eq' => sub {
13 37     37   8924 return ("$_[0]" eq "$_[1]")
14             }, 'ne' => sub {
15 0     0   0 return ("$_[0]" ne "$_[1]")
16 42     42   45525 };
  42         37272  
  42         543  
17              
18 42     42   3902 use Params::Validate qw(:all);
  42         7099  
  42         7732  
19 42     42   270 use Carp;
  42         74  
  42         2676  
20 42     42   1138 use parent qw(Class::Accessor::Fast);
  42         596  
  42         285  
21              
22             __PACKAGE__->mk_accessors(qw/ type msg /);
23              
24             sub new {
25 214     214 1 384 my $class = shift;
26 214         5097 my $self = validate(@_, {
27             type => { type => SCALAR, optional => 1 },
28             msg => { optional => 1 },
29             cached => { optional => 1 },
30             });
31 214   50     1464 $self->{type} ||= 'unknown';
32 214         1715 return bless $self => $class;
33             }
34              
35             sub status {
36 293     293 1 429 my $self = shift;
37 293 50       779 croak 'status() is read-only method' if @_;
38 293 100       532 if (grep { $_ eq $self->{type} } ('running', 'already running', 'started', 'already started', 'restarted', 'reloaded', 'stopping')) {
  2051 100       10035  
    50          
    100          
39 124         629 return 'running';
40             }
41 507         968 elsif (grep { $_ eq $self->{type} } ('not running', 'stopped', 'starting')) {
42 161         680 return 'not running';
43             }
44 8         20 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         31 return 'broken';
52             }
53             }
54              
55             sub action {
56 8     8 1 42 my $self = shift;
57 8 50       36 croak 'action() is read-only method' if @_;
58 8 100       25 if (grep { $_ eq $self->{type} } ('started', 'stopped', 'reloaded')) {
  24         141  
59 7         83 return $self->{type};
60             }
61 1         3 return 'none';
62             }
63              
64             sub as_string {
65 462     462 1 468 my $self = shift;
66 462 50       1276 my $cached_str = ($self->{cached} ? ' [cached]' : '');
67 462 100       913 if (defined $self->{msg}) {
68 210 50       410 if ($self->{type} eq 'unknown') {
69 0         0 return "$self->{msg}\n";
70             }
71             else {
72 210         1189 return "$self->{type} ($self->{msg})".$cached_str;
73             }
74             }
75             else {
76 252         673 return $self->type.$cached_str;
77             }
78             }
79              
80              
81             1;
82              
83             __END__