File Coverage

blib/lib/Class/Workflow/State/TransitionSet.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::TransitionSet;
4 1     1   2609 use Moose::Role;
  0            
  0            
5             use Moose::Util::TypeConstraints;
6              
7             use Set::Object;
8              
9             subtype 'Set::Object'
10             => as 'Object'
11             => where { $_[0]->isa("Set::Object") };
12              
13             coerce "Set::Object"
14             => from 'ArrayRef'
15             => via { Set::Object->new(@{ $_[0] }) };
16              
17             has transitions => (
18             isa => "Set::Object",
19             coerce => 1,
20             accessor => "transition_set",
21             default => sub { Set::Object->new },
22             );
23              
24             sub transitions {
25             my ( $self, @transitions ) = @_;
26              
27             if ( @transitions ) {
28             # special case to allow set-as-array-ref
29             no warnings 'uninitialized';
30             @transitions = @{ $transitions[0] } if @transitions == 1 and ref($transitions[0]) eq "ARRAY";
31              
32             $self->transition_set( Set::Object->new( @transitions ) );
33              
34             return @transitions;
35             } else {
36             return $self->transition_set->members;
37             }
38             }
39              
40             sub clear_transitions {
41             my $self = shift;
42             $self->transition_set( Set::Object->new );
43             }
44              
45             sub remove_transitions {
46             my ( $self, @transitions ) = @_;
47             $self->transition_set->remove( @transitions );
48             }
49              
50             sub add_transitions {
51             my ( $self, @transitions ) = @_;
52             $self->transition_set->insert( @transitions );
53             }
54              
55             sub has_transition {
56             my ( $self, $transition ) = @_;
57             $self->transition_set->includes( $transition );
58             }
59              
60             sub has_transitions {
61             my ( $self, @transitions ) = @_;
62             $self->transition_set->includes( @transitions );
63             }
64              
65             __PACKAGE__;
66              
67             __END__
68              
69             =pod
70              
71             =head1 NAME
72              
73             Class::Workflow::State::TransitionSet - A state that implements transition meta
74             data using Set::Object.
75              
76             =head1 SYNOPSIS
77              
78             package MyState;
79             with "Class::Workflow::State::TransitionSet";
80              
81             =head1 DESCRIPTION
82              
83             This is a concrete role that implements C<transitions>, C<has_transition> and
84             C<has_transitions> as required by L<Class::Workflow::State>, and adds
85             C<add_transitions>, C<remove_transitions> and C<clear_transitions> as well.
86              
87             Transition storage is implemented internally with L<Set::Object>.
88              
89             Unlike L<Class::Workflow::State::TransitionHash> this role does not require
90             transitions to respond to the C<name> method, but as a consequence you must
91             refer to the transitions by value.
92              
93             Note that you may construct like this:
94              
95             Class->new(
96             transitions => \@transitions,
97             );
98              
99             and the transition set will be coerced from that array reference.
100              
101             =head1 METHODS
102              
103             See L<Class::Workflow::State>
104              
105             =over 4
106              
107             =item has_transition
108              
109             =item has_transitions
110              
111             =item transitions
112              
113             =item add_transitions
114              
115             =item transition_set
116              
117             =back
118              
119             =cut
120              
121