File Coverage

blib/lib/Machine/State/State.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # ABSTRACT: State Machine State Class
2             package Machine::State::State;
3              
4 2     2   84806 use Bubblegum;
  2         546270  
  2         12  
5 2     2   1901987 use Function::Parameters;
  2         12763  
  2         19  
6 2     2   2898 use Machine::State::Failure::Transition::Unknown;
  0            
  0            
7             use Machine::State::Transition;
8             use Moose;
9             use Try::Tiny;
10              
11             our $VERSION = '0.04'; # VERSION
12              
13             has 'name' => (
14             is => 'ro',
15             isa => 'Str',
16             required => 1
17             );
18              
19             has 'next' => (
20             is => 'rw',
21             isa => 'Str',
22             required => 0
23             );
24              
25             has 'transitions' => (
26             is => 'ro',
27             isa => 'HashRef',
28             default => sub {{}}
29             );
30              
31             method add_transition {
32             my $trans = pop;
33             my $name = shift;
34              
35             if ($trans->isa('Machine::State::Transition')) {
36             $name //= $trans->name;
37             $self->transitions->set($name => $trans);
38             return $trans;
39             }
40              
41             # transition not found
42             Machine::State::Failure::Transition::Unknown->throw(
43             transition_name => $name,
44             );
45             }
46              
47             method remove_transition {
48             my $name = shift;
49              
50             if ($self->transitions->get($name->asa_string)) {
51             return $self->transitions->delete($name);
52             }
53              
54             # transition not found
55             Machine::State::Failure::Transition::Unknown->throw(
56             transition_name => $name,
57             );
58             }
59              
60             1;
61              
62             __END__
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =head1 NAME
69              
70             Machine::State::State - State Machine State Class
71              
72             =head1 VERSION
73              
74             version 0.04
75              
76             =head1 SYNOPSIS
77              
78             use Machine::State::State;
79              
80             my $state = Machine::State::State->new(
81             name => 'sleep',
82             next => 'resume'
83             );
84              
85             =head1 DESCRIPTION
86              
87             Machine::State::State represents a state and it's transitions.
88              
89             =head1 ATTRIBUTES
90              
91             =head2 name
92              
93             my $name = $state->name;
94             $name = $state->name('inspired');
95              
96             The name of the state. The value can be any scalar value.
97              
98             =head2 next
99              
100             my $transition_name = $state->next;
101             $transition_name = $state->next('create_art');
102              
103             The name of the next transition. The value can be any scalar value. This value
104             is used in automating the transition from one state to the next.
105              
106             =head2 transitions
107              
108             my $transitions = $state->transitions;
109              
110             The transitions attribute contains the collection of transitions the state can
111             apply. The C<add_transition> and C<remove_transition> methods should be used to
112             configure state transitions.
113              
114             =head1 METHODS
115              
116             =head2 add_transition
117              
118             $trans = $state->add_transition(Machine::State::Transition->new(...));
119             $state->add_transition(name => Machine::State::Transition->new(...));
120              
121             The add_transition method registers a new transition in the transitions
122             collection. The method requires a L<Machine::State::Transition> object.
123              
124             =head2 remove_transition
125              
126             $trans = $state->remove_transition('transition_name');
127              
128             The remove_transition method removes a pre-defined transition from the
129             transitions collection. The method requires a transition name.
130              
131             =encoding utf8
132              
133             =head1 AUTHOR
134              
135             Al Newkirk <anewkirk@ana.io>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2014 by Al Newkirk.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut