File Coverage

blib/lib/State/Machine.pm
Criterion Covered Total %
statement 36 43 83.7
branch 3 8 37.5
condition 2 6 33.3
subroutine 10 12 83.3
pod 3 3 100.0
total 54 72 75.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Simple State Machine Implementation
2             package State::Machine;
3              
4 1     1   26282 use Bubblegum::Class;
  1         179157  
  1         6  
5 1     1   699779 use Function::Parameters;
  1         3474  
  1         11  
6 1     1   1277 use State::Machine::Failure::Transition::Execution;
  1         4  
  1         46  
7 1     1   871 use State::Machine::Failure::Transition::Missing;
  1         4  
  1         38  
8 1     1   935 use State::Machine::Failure::Transition::Unknown;
  1         4  
  1         62  
9 1     1   12 use Try::Tiny;
  1         1  
  1         101  
10              
11 1     1   7 use Bubblegum::Constraints -typesof;
  1         3  
  1         17  
12              
13             our $VERSION = '0.07'; # VERSION
14              
15             has 'state' => (
16             is => 'rw',
17             isa => typeof_object,
18             required => 1
19             );
20              
21             has 'topic' => (
22             is => 'ro',
23             isa => typeof_string,
24             required => 1
25             );
26              
27 2     2 1 8101 method apply {
  2         2  
28 2         35 my $state = $self->state;
29 2         649 my $next = $self->next;
30              
31             # cannot transition
32 2 50       24 State::Machine::Failure::Transition::Missing->throw
33             unless $next->isa_string;
34              
35             # find transition
36 2 50       18 if (my $trans = $state->transitions->get($next)) {
37             try {
38             # attempt transition
39 2     2   92 $self->state($trans->execute($state, @_));
40             }
41             catch {
42             # transition execution failure
43 0     0   0 State::Machine::Failure::Transition::Execution->throw(
44             captured => $_,
45             transition_name => $next,
46             transition_object => $trans,
47             );
48             }
49 2         94 }
50             else {
51             # transition unknown
52 0         0 State::Machine::Failure::Transition::Unknown->throw(
53             transition_name => $next
54             );
55             }
56              
57 2         826 return $self->state;
58             };
59              
60 2     2 1 4 method next {
  2         4  
61 2         39 my $state = $self->state;
62 2   33     53 my $next = shift // $state->next;
63              
64 2 50 33     29 if ($state && !$next) {
65             # deduce transition unless defined
66 0 0       0 if ($state->transitions->keys->count == 1) {
67 0         0 $next = $state->transitions->keys->get(0);
68             }
69             }
70              
71 2         7 return $next;
72             }
73              
74 0     0 1   method status {
  0            
75 0           return $self->state->name;
76             }
77              
78             1;
79              
80             __END__