Ubic

 view release on metacpan or  search on metacpan

lib/Ubic/Result/Class.pm  view on Meta::CPAN

package Ubic::Result::Class;
$Ubic::Result::Class::VERSION = '1.60';
use strict;
use warnings;

# ABSTRACT: ubic result object


use overload '""' => sub {
    my $self = shift;
    return $self->as_string;
}, 'eq' => sub {
    return ("$_[0]" eq "$_[1]")
}, 'ne' => sub {
    return ("$_[0]" ne "$_[1]")
};

use Params::Validate qw(:all);
use Carp;
use parent qw(Class::Accessor::Fast);

__PACKAGE__->mk_accessors(qw/ type msg /);

sub new {
    my $class = shift;
    my $self = validate(@_, {
        type => { type => SCALAR, optional => 1 },
        msg => { optional => 1 },
        cached => { optional => 1 },
    });
    $self->{type} ||= 'unknown';
    return bless $self => $class;
}

sub status {
    my $self = shift;
    croak 'status() is read-only method' if @_;
    if (grep { $_ eq $self->{type} } ('running', 'already running', 'started', 'already started', 'restarted', 'reloaded', 'stopping')) {
        return 'running';
    }
    elsif (grep { $_ eq $self->{type} } ('not running', 'stopped', 'starting')) {
        return 'not running';
    }
    elsif (grep { $_ eq $self->{type} } ('down')) {
        return 'down';
    }
    elsif (grep { $_ eq $self->{type} } ('autostarting')) {
        return 'autostarting';
    }
    else {
        return 'broken';
    }
}

sub action {
    my $self = shift;
    croak 'action() is read-only method' if @_;
    if (grep { $_ eq $self->{type} } ('started', 'stopped', 'reloaded')) {
        return $self->{type};
    }
    return 'none';
}

sub as_string {
    my $self = shift;
    my $cached_str = ($self->{cached} ? ' [cached]' : '');
    if (defined $self->{msg}) {
        if ($self->{type} eq 'unknown') {
            return "$self->{msg}\n";
        }
        else {
            return "$self->{type} ($self->{msg})".$cached_str;
        }
    }
    else {
        return $self->type.$cached_str;
    }
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Ubic::Result::Class - ubic result object

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.567 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )