File Coverage

blib/lib/Class/Workflow/State/TransitionHash.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::TransitionHash;
4 1     1   2380 use Moose::Role;
  0            
  0            
5              
6             use Carp qw/croak/;
7              
8             with qw/
9             Class::Workflow::State
10             Class::Workflow::State::TransitionSet
11             /;
12              
13             has transition_hash => (
14             isa => "HashRef",
15             is => "rw",
16             default => sub { {} },
17             );
18              
19             after "BUILDALL" => sub {
20             my $self = shift;
21             $self->_reindex_hash;
22             };
23              
24             sub _reindex_hash {
25             my $self = shift;
26             my @transitions = $self->transitions;
27              
28             for ( @transitions ) {
29             blessed($_)
30             or croak (($_||'') . " is not an object");
31              
32             $_->can("name")
33             or croak "All transitions registered with a hash based state must know their own name";
34             }
35              
36             $self->transition_hash({ map { $_->name => $_ } @transitions });
37             }
38              
39             after transitions => sub {
40             my ( $self, @transitions ) = @_;
41              
42             if ( @transitions ) {
43             $self->_reindex_hash;
44             }
45             };
46              
47             after clear_transitions => sub {
48             my $self = shift;
49             $self->transition_hash({});
50             };
51              
52             after qw/remove_transitions add_transitions/ => sub {
53             my $self = shift;
54             $self->_reindex_hash;
55             };
56              
57             around has_transition => sub {
58             my $next = shift;
59             my ( $self, $transition ) = @_;
60             if ( blessed( $transition ) ) {
61             return $self->$next( $transition );
62             } else {
63             return exists $self->transition_hash->{$transition};
64             }
65             };
66              
67             around has_transitions => sub {
68             my $next = shift;
69             my ( $self, @transitions ) = @_;
70              
71             foreach my $t ( @transitions ) {
72             return unless $self->has_transition( $t );
73             }
74              
75             return 1;
76             };
77              
78             sub get_transition {
79             my ( $self, $transition ) = @_;
80             return ( blessed($transition) ? $transition : $self->transition_hash->{$transition} );
81             }
82              
83             sub get_transitions {
84             my ( $self, @transitions ) = @_;
85              
86             if ( @transitions ) {
87             return map { $self->get_transition( $_ ) } @transitions;
88             } else {
89             return $self->transitions;
90             }
91             }
92              
93             __PACKAGE__;
94              
95             __END__
96              
97             =pod
98              
99             =head1 NAME
100              
101             Class::Workflow::State::TransitionHash - Implement transition metadata with a
102             hash.
103              
104             =head1 SYNOPSIS
105              
106             package MyState;
107             use Moose;
108              
109             with qw/Class::Workflow::State::TransitionHash/;
110              
111             =head1 DESCRIPTION
112              
113             This is a concrete role that implements C<transitions>, C<has_transition> and
114             C<has_transitions> as required by L<Class::Workflow::State>, and adds
115             C<add_transitions>, C<remove_transitions>, C<clear_transitions> ,
116             C<get_transitions>, and C<get_transition> as well.
117              
118             Transition storage is implemented internally with L<Set::Object>.
119              
120             This is an additional layer over L<Class::Workflow::State::TransitionSet> that
121             requires all transitions to respond to the C<name> method, but as a bonus
122             allows you to refer to your transitions by name or by value.
123              
124             =head1 METHODS
125              
126             See L<Class::Workflow::State::TransitionSet> and L<Class::Workflow::State>.
127              
128             =over 4
129              
130             =item get_transition $name
131              
132             =item get_transitions @names
133              
134             These methods allow you to pass in either a name or an object, and always get
135             back an object (unless the transition by that name does not exist, in which
136             case you get an undefined value).
137              
138             =back
139              
140             =cut
141              
142