File Coverage

blib/lib/Workflow/Lite/Role/Workflow.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Workflow::Lite::Role::Workflow;
2              
3 1     1   1501 use namespace::autoclean;
  1         2  
  1         6  
4 1     1   377 use Moose::Role;
  0            
  0            
5             use MooseX::ClassAttribute;
6              
7              
8             class_has _steps => ( is => 'ro', isa => 'HashRef', default => sub { { } } );
9              
10             has _step => ( is => 'rw', isa => 'Str', default => 'START' );
11             has _flowing => ( is => 'rw', isa => 'Bool', default => 1 );
12              
13              
14             sub work {
15             my ( $self, @args ) = @_;
16              
17             die q!Cannot work if workflow has ended.!
18             unless $self->is_flowing;
19              
20             my ( $step, $steps ) = ( $self->_step, $self->_steps );
21             die q!No handler defined for step '!, $step, q!'.!
22             unless defined $steps->{$step};
23              
24             my $code = $steps->{$step};
25             $self->$code( @args )
26             }
27              
28             sub flow {
29             my ( $self, $step ) = @_;
30              
31             my $steps = $self->_steps;
32             die q!Destination step '!, $step, q!' does not exist.!
33             unless exists $steps->{$step};
34              
35             $self->_step( $step )
36             }
37              
38             sub end { $_[0]->_flowing( 0 ) }
39              
40             sub is_flowing { $_[0]->_flowing }
41              
42              
43             1
44             __END__
45              
46             =pod
47              
48             =head1 NAME
49              
50             Workflow::Lite::Role::Workflow - A role comprising the main functionality for workflows
51              
52             =head1 SYNOPSIS
53              
54             package MyWorkflow;
55              
56             use namespace::autoclean;
57             use Moose;
58              
59             with qw( Workflow::Lite::Role::Workflow );
60              
61             __PACKAGE__->_steps->{START} = sub {
62             my ( $self, $text ) = @_;
63              
64             print 'This is the START step: ', $text, "\n";
65             $self->flow( 'foo' );
66              
67             reverse $text
68             };
69              
70             __PACKAGE__->_steps->{foo} = sub {
71             my ( $self, $text ) = @_;
72              
73             print 'This is the foo step: ', $text, "\n";
74             $self->flow( 'bar' );
75              
76             reverse $text
77             };
78              
79             __PACKAGE__->_steps->{bar} = sub {
80             my ( $self, $text ) = @_;
81              
82             print 'This is the bar step: ', $text, "\n";
83             $self->end;
84              
85             reverse $text
86             };
87              
88             1
89              
90             ...
91              
92             my $wf = MyWorkflow->new;
93              
94             my @words = qw( Bar Foo Start );
95              
96             while( $wf->is_flowing ) {
97             my $rv = $wf->work( pop @words );
98             print ' -> ', $rv, "\n";
99             }
100              
101             =head1 DESCRIPTION
102              
103             Workflow::Lite::Role::Workflow is a role that implements the
104             functionality necessary for handling workflows. This role is
105             applied automatically when C<use>ing L<Workflow::Lite|Workflow::Lite>.
106              
107             =head1 CLASS ATTRIBUTES
108              
109             =head2 _steps
110              
111             A HashRef that contains the defined steps for the class. Steps
112             are simply CodeRefs that get called when the named step is
113             executed in a call to C<work()>.
114              
115             =head1 OBJECT ATTRIBUTES
116              
117             =head2 _step
118              
119             Contains the name of the step to be executed upon the next call
120             to C<work()>. This defaults to C<'START'>, but can be overridden
121             if necessary.
122              
123             =head2 _flowing
124              
125             A boolean value that provides the flowing status of the workflow.
126              
127             =head1 OBJECT METHODS
128              
129             =head2 work( [@args] )
130              
131             Runs the handler for the current step. Any arguments provided
132             in C<@args> are passed to the handler. Handlers are run as object
133             methods so C<$self> is passed as well. The return from the handler
134             is passed as-is back from C<work()>. If the workflow is not
135             currently flowing or the current step does not have a handler,
136             an exception is thrown.
137              
138             =head2 flow( $step )
139              
140             Flows to the step named by C<$step>. If C<$step> has not been
141             previously defined, an exception is thrown.
142              
143             =head2 end()
144              
145             Halts the workflow. Further calls to C<work()> will result in
146             an exception being thrown.
147              
148             =head2 is_flowing()
149              
150             Returns true if the workflow is still flowing, false otherwise.
151              
152             =head1 BUGS
153              
154             None are known at this time, but if you find one, please feel free
155             to submit a report to the author.
156              
157             =head1 AUTHOR
158              
159             jason hord E<lt>pravus@cpan.orgE<gt>
160              
161             =head1 SEE ALSO
162              
163             =over 4
164              
165             =item L<Workflow::Lite>
166              
167             =back
168              
169             =head1 COPYRIGHT
170              
171             Copyright (c) 2011-2014, jason hord
172              
173             Permission is hereby granted, free of charge, to any person obtaining a copy
174             of this software and associated documentation files (the "Software"), to deal
175             in the Software without restriction, including without limitation the rights
176             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
177             copies of the Software, and to permit persons to whom the Software is
178             furnished to do so, subject to the following conditions:
179              
180             The above copyright notice and this permission notice shall be included in
181             all copies or substantial portions of the Software.
182              
183             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
186             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
187             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
188             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
189             THE SOFTWARE.
190              
191             =cut