1package App::Netdisco::Worker::Status;
2
3use strict;
4use warnings;
5
6use Dancer qw/:moose :syntax !error !info/;
7
8use Moo;
9use namespace::clean;
10
11has 'status' => (
12  is => 'rw',
13  default => undef,
14);
15
16has [qw/log phase/] => (
17  is => 'rw',
18  default => '',
19);
20
21=head1 INTRODUCTION
22
23The status can be:
24
25=over 4
26
27=item * C<done>
28
29Indicates a state of success and a log message which may be used as the
30outcome for the action.
31
32=item * C<info>
33
34The worker has completed successfully and a debug log will be issued, but the
35outcome is not the main goal of the action.
36
37=item * C<defer>
38
39Issued when the worker has failed to connect to the remote device, or is not
40permitted to connect (through user config).
41
42=item * C<error>
43
44Something went wrong which should not normally be the case.
45
46=item * C<()>
47
48This is not really a status. The worker can return any value not an instance
49of this class to indicate a "pass", or non-error conclusion.
50
51=back
52
53=head1 METHODS
54
55=head2 done, info, defer, error
56
57Shorthand for new() with setting param, accepts log as arg.
58
59=cut
60
61sub _make_new {
62  my ($self, $status, $log) = @_;
63  die unless $status;
64  my $new = (ref $self ? $self : $self->new());
65  $new->log($log);
66  $new->status($status);
67  return $new;
68}
69
70sub done  { shift->_make_new('done', @_)  }
71sub info  { shift->_make_new('info', @_)  }
72sub defer { shift->_make_new('defer', @_) }
73sub error { shift->_make_new('error', @_) }
74
75=head2 is_ok
76
77Returns true if status is C<done>.
78
79=cut
80
81sub is_ok { return $_[0]->status eq 'done' }
82
83=head2 not_ok
84
85Returns true if status is C<error>, C<defer>, or C<info>.
86
87=cut
88
89sub not_ok { return (not $_[0]->is_ok) }
90
91=head2 level
92
93A numeric constant for the status, to allow comparison.
94
95=cut
96
97sub level {
98  my $self = shift;
99  return (($self->status eq 'done')  ? 4
100        : ($self->status eq 'defer') ? 3
101        : ($self->status eq 'info')  ? 2
102        : ($self->status eq 'error') ? 1 : 0);
103}
104
1051;
106