| 1 | package Ubic::Result::Class; | ||
| 2 | $Ubic::Result::Class::VERSION = '1.59'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: ubic result object | ||
| 7 | |||
| 8 | |||
| 9 | use overload '""' => sub { | ||
| 10 | my $self = shift; | ||
| 11 | return $self->as_string; | ||
| 12 | }, 'eq' => sub { | ||
| 13 | return ("$_[0]" eq "$_[1]") | ||
| 14 | }, 'ne' => sub { | ||
| 15 | return ("$_[0]" ne "$_[1]") | ||
| 16 | }; | ||
| 17 | |||
| 18 | use Params::Validate qw(:all); | ||
| 19 | use Carp; | ||
| 20 | use parent qw(Class::Accessor::Fast); | ||
| 21 | |||
| 22 | __PACKAGE__->mk_accessors(qw/ type msg /); | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $class = shift; | ||
| 26 | my $self = validate(@_, { | ||
| 27 | type => { type => SCALAR, optional => 1 }, | ||
| 28 | msg => { optional => 1 }, | ||
| 29 | cached => { optional => 1 }, | ||
| 30 | }); | ||
| 31 | $self->{type} ||= 'unknown'; | ||
| 32 | return bless $self => $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub status { | ||
| 36 | my $self = shift; | ||
| 37 | croak 'status() is read-only method' if @_; | ||
| 38 | if (grep { $_ eq $self->{type} } ('running', 'already running', 'started', 'already started', 'restarted', 'reloaded', 'stopping')) { | ||
| 39 | return 'running'; | ||
| 40 | } | ||
| 41 | elsif (grep { $_ eq $self->{type} } ('not running', 'stopped', 'starting')) { | ||
| 42 | return 'not running'; | ||
| 43 | } | ||
| 44 | elsif (grep { $_ eq $self->{type} } ('down')) { | ||
| 45 | return 'down'; | ||
| 46 | } | ||
| 47 | elsif (grep { $_ eq $self->{type} } ('autostarting')) { | ||
| 48 | return 'autostarting'; | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | return 'broken'; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub action { | ||
| 56 | my $self = shift; | ||
| 57 | croak 'action() is read-only method' if @_; | ||
| 58 | if (grep { $_ eq $self->{type} } ('started', 'stopped', 'reloaded')) { | ||
| 59 | return $self->{type}; | ||
| 60 | } | ||
| 61 | return 'none'; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub as_string { | ||
| 65 | my $self = shift; | ||
| 66 | my $cached_str = ($self->{cached} ? ' [cached]' : ''); | ||
| 67 | if (defined $self->{msg}) { | ||
| 68 | if ($self->{type} eq 'unknown') { | ||
| 69 | return "$self->{msg}\n"; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | return "$self->{type} ($self->{msg})".$cached_str; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | return $self->type.$cached_str; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |