File Coverage

blib/lib/Workflow/Lite.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;
2              
3 3     3   130782 use namespace::autoclean;
  3         27516  
  3         18  
4 3     3   1329 use Moose qw();
  0            
  0            
5             use Moose::Exporter;
6             use Workflow::Lite::Role::Workflow;
7              
8              
9             our $VERSION = '0.08';
10              
11              
12             Moose::Exporter->setup_import_methods(
13             with_caller => [qw( steps step )],
14             also => [qw( Moose )],
15             );
16              
17              
18             sub init_meta {
19             my ( $class, %args ) = @_;
20              
21             Moose->init_meta( %args );
22              
23             my $meta = $args{for_class}->meta;
24             Workflow::Lite::Role::Workflow->meta->apply( $meta );
25              
26             $meta
27             }
28              
29             sub steps {
30             my ( $class, @args ) = @_;
31              
32             my $args = @args == 1 ? $args[0] : { @args };
33             step( $class, $_, $args->{$_} )
34             for keys %$args;
35             }
36              
37             sub step { $_[0]->_steps->{$_[1]} = $_[2] }
38              
39              
40             1
41             __END__
42              
43             =pod
44              
45             =head1 NAME
46              
47             Workflow::Lite - A very simplistic workflow framework
48              
49             =head1 SYNOPSIS
50              
51             package MyWorkflow;
52              
53             use namespace::clean;
54             use Workflow::Lite;
55              
56             steps
57             START => sub {
58             my ( $self, $text ) = @_;
59              
60             print 'This is the START step: ', $text, "\n";
61             $self->flow( 'foo' );
62              
63             reverse $text
64             },
65              
66             foo => sub {
67             my ( $self, $text ) = @_;
68              
69             print 'This is the foo step: ', $text, "\n";
70             $self->flow( 'bar' );
71              
72             reverse $text
73             },
74             ;
75              
76             step bar => sub {
77             my ( $self, $text ) = @_;
78              
79             print 'This is the bar step: ', $text, "\n";
80             $self->end;
81              
82             reverse $text
83             };
84              
85             1
86              
87             ...
88              
89             my $wf = MyWorkflow->new;
90              
91             my @words = qw( Bar Foo Start );
92              
93             while( $wf->is_flowing ) {
94             my $rv = $wf->work( pop @words );
95             print ' -> ', $rv, "\n";
96             }
97              
98             =head1 DESCRIPTION
99              
100             Workflow::Lite is a very simple framework for defining and
101             implementing workflows. This module is actually just a wrapper
102             that provides helpers for implementing a workflow object.
103              
104             Workflows are simply collections of steps that are able to
105             flow to each other as necessary. They are implemented as
106             L<Moose|Moose> objects so that attributes can be defined
107             to preserve stateful information between steps. The
108             main functionality for workflows is implemented as a
109             L<Moose|Moose> role which is installed automatically when
110             C<use>ing C<Workflow::Lite>. This module also exports
111             L<Moose|Moose> for convenience.
112              
113             Steps are simply named CodeRefs that perform the actions
114             necessary when a workflow needs to execute a particular step.
115             This module provides helpers to make it easy to define
116             workflow steps. Steps are purposely not implemented as
117             object methods to keep from cluttering up the class's
118             method namespace.
119              
120             When a workflow object is instantiated, it's initialized to
121             work from the start step (the default is named 'START'). Calls
122             to the C<work()> method will execute step handlers which may
123             call the C<flow()> method to move to another step. The C<end()>
124             method is used to terminate a workflow. Subsequent calls to
125             C<work()> will result in an exception being thrown if a workflow
126             has ended. The C<is_flowing()> method can be used to check if
127             a workflow is still active or not.
128              
129             Arguments can be passed to individual step handlers by simply
130             passing them to the C<work()> method. The return from a step
131             handler is passed through C<work()> to the caller as-is.
132              
133             =head1 EXPORTED FUNCTIONS
134              
135             =head2 step $name =E<gt> $code
136              
137             Used to define a step named C<$name> with the handler C<$code>.
138              
139             =head2 steps $name =E<gt> $code, [...]
140              
141             Same as above, but allows multiple steps to be defined at once.
142              
143             =head1 BUGS
144              
145             None are known at this time, but if you find one, please feel free
146             to submit a report to the author.
147              
148             =head1 AUTHOR
149              
150             jason hord E<lt>pravus@cpan.orgE<gt>
151              
152             =head1 SEE ALSO
153              
154             =over 4
155              
156             =item L<Workflow::Lite::Registry>
157              
158             =item L<Workflow::Lite::Role::Workflow>
159              
160             =back
161              
162             =head1 COPYRIGHT
163              
164             Copyright (c) 2011-2014, jason hord
165              
166             Permission is hereby granted, free of charge, to any person obtaining a copy
167             of this software and associated documentation files (the "Software"), to deal
168             in the Software without restriction, including without limitation the rights
169             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
170             copies of the Software, and to permit persons to whom the Software is
171             furnished to do so, subject to the following conditions:
172              
173             The above copyright notice and this permission notice shall be included in
174             all copies or substantial portions of the Software.
175              
176             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
179             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
180             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
181             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
182             THE SOFTWARE.
183              
184             =cut