File Coverage

blib/lib/Class/Workflow/State/AcceptHooks.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Class::Workflow::State::AcceptHooks;
4 1     1   6464 use Moose::Role;
  0            
  0            
5              
6             has hooks => (
7             isa => "ArrayRef",
8             is => "rw",
9             auto_deref => 1,
10             default => sub { [] },
11             );
12              
13             sub clear_hooks {
14             my $self = shift;
15             $self->hooks( [] );
16             }
17              
18             sub add_hook {
19             my ( $self, $hook ) = @_;
20             $self->add_hooks( $hook );
21             }
22              
23             sub add_hooks {
24             my ( $self, @hooks ) = @_;
25             push @{ $self->hooks }, @hooks;
26             }
27              
28             after accept_instance => sub {
29             my ( $self, $instance, @args ) = @_;
30             $_->( $instance, @args ) for $self->hooks;
31             };
32              
33             __PACKAGE__;
34              
35             __END__
36              
37             =pod
38              
39             =head1 NAME
40              
41             Class::Workflow::State::AcceptHooks - Add hooks that are fired when the state
42             accepts an instance.
43              
44             =head1 SYNOPSIS
45              
46             use Class::Workflow::State::AcceptHooks;
47              
48             =head1 DESCRIPTION
49              
50             When an instance enters a state it is sometimes convenient to call hooks, for
51             e.g. notification or logging purposes.
52              
53             These hooks should not have any side effect that directly affects the workflow
54             instance in any way - for that functionality you should use transitions.
55              
56             Hooks' returns values are thus ignored.
57              
58             =head1 METHODS
59              
60             =over 4
61              
62             =item add_hook
63              
64             =item add_hooks
65              
66             Add hooks. These should be sub references.
67              
68             =item clear_hooks
69              
70             Clear the list of hooks.
71              
72             =item hooks
73              
74             Get the list of registered hooks.
75              
76             =back
77              
78             =head1 AUGMENTED METHODS
79              
80             =over 4
81              
82             =item accept_instance
83              
84             This method has an C<after> hook that calls the hooks in the order of their definition.
85              
86             =back
87              
88             =cut
89              
90