File Coverage

blib/lib/Workflow/Lite/Registry.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Workflow::Lite::Registry;
2              
3 1     1   5502 use namespace::autoclean;
  1         2  
  1         8  
4 1     1   1606 use Class::Load;
  1         14560  
  1         39  
5 1     1   325 use Moose;
  0            
  0            
6             use MooseX::ClassAttribute;
7              
8             # FIXME: should dups be allowed?
9             # FIXME: add ->add_workflow_class() and ->remove_workflow_class()?
10              
11              
12             class_has _registry => ( is => 'ro', isa => 'HashRef', default => sub { { } } );
13              
14              
15             sub register {
16             my ( $class, @pairs ) = @_;
17              
18             while( my ( $id, $workflow_class ) = splice @pairs, 0, 2 ) {
19             Class::Load::load_class( $workflow_class );
20             $class->_registry->{$id} = $workflow_class;
21             }
22             }
23              
24             sub workflow_class {
25             my ( $class, $id ) = @_;
26              
27             my $workflow_class = $class->_registry->{$id};
28             die q!No class registered for '!, $id, q!'.!, "\n"
29             unless defined $workflow_class;
30              
31             $workflow_class
32             }
33              
34             sub new_workflow {
35             my ( $class, $id, @args ) = @_;
36              
37             my $workflow_class = $class->workflow_class( $id );
38             $workflow_class->new( @args )
39             }
40              
41              
42             __PACKAGE__->meta->make_immutable;
43              
44             1
45             __END__
46              
47             =pod
48              
49             =head1 NAME
50              
51             Workflow::Lite::Registry - A workflow registry
52              
53             =head1 SYNOPSIS
54              
55             package MyRegistry;
56              
57             use namespace::autoclean;
58             use Moose;
59              
60             extends qw( Workflow::Lite::Registry );
61              
62             __PACKAGE__->register(
63             Even => 'MyApp::Workflow::Even',
64             Odd => 'MyApp::Workflow::Odd',
65             );
66              
67             1
68              
69             ...
70              
71             use MyRegistry;
72              
73             my $wf_name = time % 2 == 0 ? 'Even' : 'Odd';
74             my @args = ( foo => 'Foo', bar => 'Bar' );
75             my $wf = MyRegistry->new_workflow( $wf_name => @args );
76              
77             =head1 DESCRIPTION
78              
79             Workflow::Lite::Registry is meant to be used as a base class
80             to application-specific workflow registries that can be used
81             to shorten class names. L<Workflow::Lite|Workflow::Lite> classes
82             are registered and given a short, unique name that can be referenced
83             at a later time. This is probably most advantageous when there are
84             many different workflow classes being used or if workflows need to
85             be used based on configuration or user input.
86              
87             =head1 CLASS ATTRIBUTES
88              
89             =head2 _registry
90              
91             A HashRef that contains the workflow registry.
92              
93             =head1 CLASS METHODS
94              
95             =head2 register( $id, $workflow_class )
96              
97             Registers a new workflow class into the registry under the given id.
98             The workflow class is also loaded for convenience.
99              
100             =head2 workflow_class( $id )
101              
102             Returns the workflow class that is associated with the given id in
103             the registry.
104              
105             =head2 new_workflow( $id, @args )
106              
107             Instantiates a new workflow object by looking up the workflow class
108             associated with the given id and then calling C<new()> with the
109             given arguments.
110              
111             =back
112              
113             =head1 BUGS
114              
115             None are known at this time, but if you find one, please feel free
116             to submit a report to the author.
117              
118             =head1 AUTHOR
119              
120             jason hord E<lt>pravus@cpan.orgE<gt>
121              
122             =head1 SEE ALSO
123              
124             =over 4
125              
126             =item L<Workflow::Lite>
127              
128             =back
129              
130             =head1 COPYRIGHT
131              
132             Copyright (c) 2011-2014, jason hord
133              
134             Permission is hereby granted, free of charge, to any person obtaining a copy
135             of this software and associated documentation files (the "Software"), to deal
136             in the Software without restriction, including without limitation the rights
137             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
138             copies of the Software, and to permit persons to whom the Software is
139             furnished to do so, subject to the following conditions:
140              
141             The above copyright notice and this permission notice shall be included in
142             all copies or substantial portions of the Software.
143              
144             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
146             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
147             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
148             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
149             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
150             THE SOFTWARE.
151              
152             =cut