Workflow.pm

filter: Statementstmt 77.6% 94 / 121 Branchbran 52.0% 26 / 50 Conditioncond 22.4% 13 / 58 MC/DCmcdc 6.2% 2 / 32 Subroutinesub 91.6% 22 / 24 Podpod 100% 14 / 14 totaltotal 57.1% 171 / 299 CC 49 SCAR 39.0
CC
49
Cov
58%
state
112.3
transition
91.5
rename_state
6.0
CRAP 49.6
↑ Summary Workflow/Transition/Validate/Simple.pm »
1 #!/usr/bin/perl
2  
3 package Class::Workflow;
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Class::Workflow::State::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Class::Workflow::Transition::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Class::Workflow::Instance::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = "0.11_01";
11  
12 5 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 has initial_state => (
16 isa => "Str | Object",
17 is => "rw",
18 );
19  
20 5 has instance_class => (
21 isa => "Str",
22 is => "rw",
23 default => "Class::Workflow::Instance::Simple",
24 );
25  
26 5 ▶sub new_instance {
Subroutine new_instance: called CC=3 SCAR=11.8
Pod documented
27 5 my ( $self, %attrs ) = @_;
28  
29 5 ▶ if ( !$attrs{state} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attrs{"state"}) 5 0 5
30 5 ▶ if ( my $initial_state = $self->state( $self->initial_state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $initial_state = $self->state($self->initial_state)) { } 5 0 5
31 5 $attrs{state} = $initial_state;
32 } else {
33 0 croak "Explicit state not specified and no initial state is set in the workflow.";
34 }
35 }
36  
37 5 $self->instance_class->new( %attrs );
38 }
39  
40 5 has state_class => (
41 isa => "Str",
42 is => "rw",
43 default => "Class::Workflow::State::Simple",
44 );
45  
46 5 has transition_class => (
47 isa => "Str",
48 is => "rw",
49 default => "Class::Workflow::Transition::Simple",
50 );
51  
52 has _state_fields => (
53 isa => "HashRef",
54 is => "ro",
55 traits => [ 'Hash' ],
56 4 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 5 handles => {
58 states => 'values',
59 state_names => 'keys',
60 get_state => 'get',
61 get_states => 'get',
62 add_state => 'set',
63 delete_state => 'delete',
64 has_state => 'exists',
65 },
66 );
67  
68 has _transition_fields => (
69 isa => "HashRef",
70 is => "ro",
71 traits => [ 'Hash' ],
72 4 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 5 handles => {
74 transitions => 'values',
75 transition_names => 'keys',
76 get_transition => 'get',
77 get_transitions => 'get',
78 add_transition => 'set',
79 delete_transition => 'delete',
80 has_transition => 'exists',
81 },
82 );
83  
84 0 ▶sub rename_state {
Subroutine rename_state: not called CC=2 SCAR=17.9
Pod documented
85 0 my ( $self, $name, $new_name ) = @_;
86 0 my $obj = $self->delete_state( $name );
87 0 ▶ $obj->name( $new_name ) if $obj->can("name");
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can("name") 0 0 0
88 0 $self->add_state( $new_name => $obj );
89 }
90  
91 0 ▶sub rename_transition {
Subroutine rename_transition: not called CC=2 SCAR=17.9
Pod documented
92 0 my ( $self, $name, $new_name ) = @_;
93 0 my $obj = $self->delete_transition( $name );
94 0 ▶ $obj->name( $new_name ) if $obj->can("name");
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can("name") 0 0 0
95 0 $self->add_transition( $new_name => $obj );
96 }
97  
98 17 ▶sub create_state {
Subroutine create_state: called CC=1 SCAR=0.0
Pod documented
99 17 my ( $self, $name, @attrs ) = @_;
100 17 $self->add_state( $name => $self->construct_state( @attrs ) );
101 }
102  
103 24 ▶sub create_transition {
Subroutine create_transition: called CC=1 SCAR=0.0
Pod documented
104 24 my ( $self, $name, @attrs ) = @_;
105 24 $self->add_transition( $name => $self->construct_transition( @attrs ) );
106 }
107  
108 17 ▶sub construct_state {
Subroutine construct_state: called CC=2 SCAR=7.0
Pod documented
109 17 my ( $self, %attrs ) = @_;
110 17 ▶ my $class = delete($attrs{class}) || $self->state_class;
Condition66%2/3
delete $attrs{'class'} || $self->state_class
l!l&&r!l&&!r
1160
MC/DC0%0/2
delete $attrs{'class'} || $self->state_class
delete $attrs{'class'} $self->state_class
Truth table66%2/3
A || B
A= delete $attrs{'class'}B= $self->state_class
ABresult
1X1
011
000
111 17 $class->new( %attrs );
112 }
113  
114 24 ▶sub construct_transition {
Subroutine construct_transition: called CC=2 SCAR=7.6
Pod documented
115 24 my ( $self, %attrs ) = @_;
116 24 ▶ my $class = delete($attrs{class}) || $self->transition_class;
Condition33%1/3
delete $attrs{'class'} || $self->transition_class
l!l&&r!l&&!r
0240
MC/DC0%0/2
delete $attrs{'class'} || $self->transition_class
delete $attrs{'class'} $self->transition_class
Truth table33%1/3
A || B
A= delete $attrs{'class'}B= $self->transition_class
ABresult
1X1
011
000
117 24 $class->new( %attrs );
118 }
119  
120 21 ▶sub autovivify_states {
Subroutine autovivify_states: called CC=2 SCAR=7.5
Pod documented
121 21 my ( $self, $thing ) = @_;
122  
123 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 21 ▶ if ( ref $thing eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 0 21 21
125 0 return [ map { $self->state( $_ ) } @$thing ];
126 } else {
127 21 return $self->state( $thing );
128 }
129 }
130  
131 11 ▶sub autovivify_transitions {
Subroutine autovivify_transitions: called CC=2 SCAR=6.9
Pod documented
132 11 my ( $self, $thing ) = @_;
133  
134 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
135 11 ▶ if ( ref $thing eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 9 2 11
136 9 return [ map { $self->transition( $_ ) } @$thing ];
137 } else {
138 2 return $self->transition( $thing );
139 }
140 }
141  
142 45 ▶sub create_or_set_state {
Subroutine create_or_set_state: called CC=4 SCAR=13.9
Pod documented
143 45 my ( $self, %attrs ) = @_;
144  
145 45 ▶ my $name = $attrs{name} || croak "Every state must have a name";
Condition50%1/2
$attrs{'name'} || croak('Every state must have a name')
l!l
450
MC/DC0%0/1
$attrs{'name'} || croak('Every state must have a name')
$attrs{'name'}
Truth table50%1/2
A
A= $attrs{'name'}
Aresult
11
00
146  
147 45 $self->expand_attrs( \%attrs );
148  
149 45 ▶ if ( my $obj = $self->get_state( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_state($name)) { } 28 17 45
150 28 delete $attrs{name};
151 28 foreach my $attr ( keys %attrs ) {
152 4 $obj->$attr( $attrs{$attr} );
153 }
154  
155 28 return $obj;
156 } else {
157 17 return $self->create_state( $name, %attrs );
158 }
159 }
160  
161 39 ▶sub create_or_set_transition {
Subroutine create_or_set_transition: called CC=4 SCAR=13.9
Pod documented
162 39 my ( $self, %attrs ) = @_;
163  
164 39 ▶ my $name = $attrs{name} || croak "Every transition must have a name";
Condition50%1/2
$attrs{'name'} || croak('Every transition must have a name')
l!l
390
MC/DC0%0/1
$attrs{'name'} || croak('Every transition must have a name')
$attrs{'name'}
Truth table50%1/2
A
A= $attrs{'name'}
Aresult
11
00
165  
166 39 $self->expand_attrs( \%attrs );
167  
168 39 ▶ if ( my $obj = $self->get_transition( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_transition($name)) { } 15 24 39
169 15 delete $attrs{name};
170 15 foreach my $attr ( keys %attrs ) {
171 24 $obj->$attr( $attrs{$attr} );
172 }
173  
174 15 return $obj;
175 } else {
176 24 return $self->create_transition( $name, %attrs );
177 }
178 }
179  
180 45 ▶sub state {
Subroutine state: called CC=17 SCAR=47.2
Pod documented
181 45 my ( $self, @params ) = @_;
182  
183 45 ▶ if ( @params == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1) 34 11 45
184 34 ▶ if ( ref($params[0]) eq "HASH" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 34 34
elsif (ref $params[0] eq 'ARRAY') { } 0 34 34
185 0 @params = %{ $params[0] };
186 } elsif ( ref($params[0]) eq "ARRAY" ) {
187 0 @params = @{ $params[0] };
188 }
189 }
190  
191 45 ▶ if ( !blessed($params[0]) and !blessed($params[1]) and @params % 2 == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0) { } 10 35 45
elsif (not ref $params[0] and @params % 2 == 1) { } 35 0 35
elsif (@params == 1 and builtin::blessed($params[0]) and $params[0]->can('name')) { } 0 0 0
elsif (@params == 2 and builtin::blessed($params[1]) and not ref $params[0]) { } 0 0 0
Condition50%3/6
not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0
!ll&&!rl&&r
03510
builtin::blessed($params[0]) || builtin::blessed($params[1])
l!l&&r!l&&!r
0045
MC/DC33%1/3
not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0
builtin::blessed($params[0]) builtin::blessed($params[1]) @params % 2 == 0
Truth table50%2/4
not(A || B) and C
A= builtin::blessed($params[0])B= builtin::blessed($params[1])C= @params % 2 == 0
ABCresult
1XX0
01X0
0000
0011
192 # $wf->state( name => "foo", transitions => [qw/bar gorch/] )
193 10 return $self->create_or_set_state( @params );
194 ▶ } elsif ( !ref($params[0]) and @params % 2 == 1 ) {
Condition33%1/3
not ref $params[0] and @params % 2 == 1
!ll&&!rl&&r
0035
MC/DC0%0/2
not ref $params[0] and @params % 2 == 1
not ref $params[0] @params % 2 == 1
Truth table33%1/3
not(A) and B
A= not ref $params[0]B= @params % 2 == 1
ABresult
0X0
100
111
195 # my $state = $wf->state("new", %attrs); # create new by name, or just get_foo
196 35 return $self->create_or_set_state( name => @params )
197 ▶ } elsif ( @params == 1 and blessed($params[0]) and $params[0]->can("name") ) {
Condition0%0/6
@params == 1 and builtin::blessed($params[0]) and $params[0]->can("name")
!ll&&!rl&&r
000
@params == 1 and builtin::blessed($params[0])
!ll&&!rl&&r
000
MC/DC0%0/3
@params == 1 and builtin::blessed($params[0]) and $params[0]->can("name")
@params == 1 builtin::blessed($params[0]) $params[0]->can("name")
Truth table0%0/4
A and B and C
A= @params == 1B= builtin::blessed($params[0])C= $params[0]->can("name")
ABCresult
0XX0
10X0
1100
1111
198 # $wf->state( $state ); # set by object (if $object->can("name") )
199 0 return $self->add_state( $params[0]->name => $params[0] );
200 ▶ } elsif ( @params == 2 and blessed($params[1]) and !ref($params[0]) ) {
Condition0%0/6
@params == 2 and builtin::blessed($params[1]) and not ref $params[0]
!ll&&!rl&&r
000
@params == 2 and builtin::blessed($params[1])
!ll&&!rl&&r
000
MC/DC0%0/3
@params == 2 and builtin::blessed($params[1]) and not ref $params[0]
@params == 2 builtin::blessed($params[1]) not ref $params[0]
Truth table0%0/4
A and B and not(C)
A= @params == 2B= builtin::blessed($params[1])C= not ref $params[0]
ABCresult
0XX0
10X0
1100
1111
201 # $wf->state( foo => $state ); # set by name
202 0 return $self->add_state( @params );
203 } else {
204 0 ▶ if ( @params == 1 and blessed($params[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1 and builtin::blessed($params[0])) { } 0 0 0
Condition0%0/3
@params == 1 and builtin::blessed($params[0])
!ll&&!rl&&r
000
MC/DC0%0/2
@params == 1 and builtin::blessed($params[0])
@params == 1 builtin::blessed($params[0])
Truth table0%0/3
A and B
A= @params == 1B= builtin::blessed($params[0])
ABresult
0X0
100
111
205 0 croak "The state $params[0] must support the 'name' method.";
206 } else {
207 0 croak "'state' was called with invalid parameters. Please consult the documentation.";
208 }
209 }
210 }
211  
212 39 ▶sub transition {
Subroutine transition: called CC=17 SCAR=45.2
Pod documented
213 39 my ( $self, @params ) = @_;
214  
215 39 ▶ if ( @params == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1) 25 14 39
216 25 ▶ if ( ref($params[0]) eq "HASH" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 6 19 25
elsif (ref $params[0] eq 'ARRAY') { } 0 19 19
217 6 @params = %{ $params[0] };
218 } elsif ( ref($params[0]) eq "ARRAY" ) {
219 0 @params = @{ $params[0] };
220 }
221 }
222  
223 39 ▶ if ( !blessed($params[0]) and !blessed($params[1]) and @params % 2 == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0) { } 19 20 39
elsif (not ref $params[0] and @params % 2 == 1) { } 20 0 20
elsif (@params == 1 and builtin::blessed($params[0]) and $params[0]->can('name')) { } 0 0 0
elsif (@params == 2 and builtin::blessed($params[1]) and not ref $params[0]) { } 0 0 0
Condition50%3/6
not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0
!ll&&!rl&&r
02019
builtin::blessed($params[0]) || builtin::blessed($params[1])
l!l&&r!l&&!r
0039
MC/DC33%1/3
not builtin::blessed($params[0]) || builtin::blessed($params[1]) and @params % 2 == 0
builtin::blessed($params[0]) builtin::blessed($params[1]) @params % 2 == 0
Truth table50%2/4
not(A || B) and C
A= builtin::blessed($params[0])B= builtin::blessed($params[1])C= @params % 2 == 0
ABCresult
1XX0
01X0
0000
0011
224 # $wf->state( name => "foo", transitions => [qw/bar gorch/] )
225 19 return $self->create_or_set_transition( @params );
226 ▶ } elsif ( !ref($params[0]) and @params % 2 == 1 ) {
Condition33%1/3
not ref $params[0] and @params % 2 == 1
!ll&&!rl&&r
0020
MC/DC0%0/2
not ref $params[0] and @params % 2 == 1
not ref $params[0] @params % 2 == 1
Truth table33%1/3
not(A) and B
A= not ref $params[0]B= @params % 2 == 1
ABresult
0X0
100
111
227 # my $state = $wf->state("new", %attrs); # create new by name, or just get_foo
228 20 return $self->create_or_set_transition( name => @params )
229 ▶ } elsif ( @params == 1 and blessed($params[0]) and $params[0]->can("name") ) {
Condition0%0/6
@params == 1 and builtin::blessed($params[0]) and $params[0]->can("name")
!ll&&!rl&&r
000
@params == 1 and builtin::blessed($params[0])
!ll&&!rl&&r
000
MC/DC0%0/3
@params == 1 and builtin::blessed($params[0]) and $params[0]->can("name")
@params == 1 builtin::blessed($params[0]) $params[0]->can("name")
Truth table0%0/4
A and B and C
A= @params == 1B= builtin::blessed($params[0])C= $params[0]->can("name")
ABCresult
0XX0
10X0
1100
1111
230 # $wf->state( $state ); # set by object (if $object->can("name") )
231 0 return $self->add_transition( $params[0]->name => $params[0] );
232 ▶ } elsif ( @params == 2 and blessed($params[1]) and !ref($params[0]) ) {
Condition0%0/6
@params == 2 and builtin::blessed($params[1]) and not ref $params[0]
!ll&&!rl&&r
000
@params == 2 and builtin::blessed($params[1])
!ll&&!rl&&r
000
MC/DC0%0/3
@params == 2 and builtin::blessed($params[1]) and not ref $params[0]
@params == 2 builtin::blessed($params[1]) not ref $params[0]
Truth table0%0/4
A and B and not(C)
A= @params == 2B= builtin::blessed($params[1])C= not ref $params[0]
ABCresult
0XX0
10X0
1100
1111
233 # $wf->state( foo => $state ); # set by name
234 0 return $self->add_transition( @params );
235 } else {
236 0 ▶ if ( @params == 1 and blessed($params[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1 and builtin::blessed($params[0])) { } 0 0 0
Condition0%0/3
@params == 1 and builtin::blessed($params[0])
!ll&&!rl&&r
000
MC/DC0%0/2
@params == 1 and builtin::blessed($params[0])
@params == 1 builtin::blessed($params[0])
Truth table0%0/3
A and B
A= @params == 1B= builtin::blessed($params[0])
ABresult
0X0
100
111
237 0 croak "The transition $params[0] must support the 'name' method.";
238 } else {
239 0 croak "'transition' was called with invalid parameters. Please consult the documentation.";
240 }
241 }
242 }
243  
244 84 ▶sub expand_attrs {
Subroutine expand_attrs: called CC=3 SCAR=11.0
Pod documented
245 84 my ($self, $attrs ) = @_;
246  
247 84 foreach my $key ( keys %$attrs ) {
248 134 ▶ if ( my ( $type ) = ( $key =~ /(transition|state)/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($type) = $key =~ /(transition|state)/) 32 102 134
249 32 my $method = "autovivify_${type}s";
250 32 $attrs->{$key} = $self->$method( $attrs->{$key} );
251 }
252 }
253 }
254  
255 5 __PACKAGE__;
256  
257 __END__
↑ Summary Workflow/Transition/Validate/Simple.pm »