File Coverage

blib/lib/Data/FSM/Transition.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             package Data::FSM::Transition;
2              
3 11     11   138859 use strict;
  11         39  
  11         462  
4 11     11   70 use warnings;
  11         20  
  11         643  
5              
6 11     11   1120 use Mo qw(build is);
  11         1523  
  11         67  
7 11     11   10562 use Mo::utils 0.12 qw(check_code check_isa check_length check_required);
  11         36270  
  11         870  
8 11     11   1688 use Mo::utils::Number qw(check_positive_natural);
  11         5038  
  11         2383  
9              
10             our $VERSION = 0.01;
11              
12             has callback => (
13             is => 'ro',
14             );
15              
16             has from => (
17             is => 'ro',
18             );
19              
20             has id => (
21             is => 'ro',
22             );
23              
24             has name => (
25             is => 'ro',
26             );
27              
28             has to => (
29             is => 'ro',
30             );
31              
32             sub BUILD {
33 22     22 0 5143 my $self = shift;
34              
35             # Check 'callback'.
36 22         104 check_code($self, 'callback');
37              
38             # Check 'from'.
39 21         348 check_required($self, 'from');
40 20         201 check_isa($self, 'from', 'Data::FSM::State');
41              
42             # Check 'id'.
43 20         541 check_positive_natural($self, 'id');
44              
45             # Check 'name'.
46 17         202 check_length($self, 'name', 100);
47              
48             # Check 'to'.
49 17         174 check_required($self, 'to');
50 16         120 check_isa($self, 'to', 'Data::FSM::State');
51              
52 16         409 return;
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding utf8
62              
63             =head1 NAME
64              
65             Data::FSM::Transition - Data object for Finite Transition Machine transition.
66              
67             =head1 SYNOPSIS
68              
69             use Data::FSM::Transition;
70              
71             my $obj = Data::FSM::Transition->new(%params);
72             my $callback = $obj->callback;
73             my $from = $obj->from;
74             my $id = $obj->id;
75             my $name = $obj->name;
76             my $to = $obj->to;
77              
78             =head1 METHODS
79              
80             =head2 C<new>
81              
82             my $obj = Data::FSM::Transition->new(%params);
83              
84             Constructor.
85              
86             =over 8
87              
88             =item * C<callback>
89              
90             Transition callback.
91              
92             It's optional.
93              
94             Default value is undef.
95              
96             =item * C<from>
97              
98             L<Data::FSM::State> state object.
99              
100             It's required.
101              
102             Default value is undef.
103              
104             =item * C<id>
105              
106             FSM state id.
107             The id is positive natural number.
108              
109             It's optional.
110              
111             Default value is undef.
112              
113             =item * C<name>
114              
115             FSM state name.
116             The length of name is 100 characters.
117              
118             Default value is undef.
119              
120             =item * C<to>
121              
122             L<Data::FSM::State> state object.
123              
124             It's required.
125              
126             Default value is undef.
127              
128             =back
129              
130             Returns instance of object.
131              
132             =head2 C<callback>
133              
134             my $callback = $obj->callback;
135              
136             Get transition callback.
137              
138             Returns reference to code.
139              
140             =head2 C<from>
141              
142             my $from = $obj->from;
143              
144             Get state object from which transition starts.
145              
146             Returns L<Data::FSM::State> instance.
147              
148             =head2 C<id>
149              
150             my $id = $obj->id;
151              
152             Get FSM state id.
153              
154             Returns positive natural number.
155              
156             =head2 C<initial>
157              
158             my $initial = $obj->initial;
159              
160             Get inital flag..
161              
162             Returns boolean (0/1).
163              
164             =head2 C<name>
165              
166             my $name = $obj->name;
167              
168             Get FSM state name.
169              
170             Returns string.
171              
172             =head2 C<to>
173              
174             my $to = $obj->to;
175              
176             Get state object from which transition ends.
177              
178             Returns L<Data::FSM::State> instance.
179              
180             =head1 ERRORS
181              
182             new():
183             From Mo::Utils::check_code():
184             Parameter '%s' must be a code.
185             Value: %s
186              
187             From Mo::utils::check_code():
188             Parameter '%s' must be a '%s' object.
189             Value: %s
190             Reference: %s
191              
192             From Mo::Utils::check_length():
193             Parameter 'name' has length greater than '100'.
194             Value: %s
195              
196             From Mo::utils::check_required():
197             Parameter '%s' is required.
198              
199             From Mo::utils::Number::check_positive_natural():
200             Parameter 'id' must be a positive natural number.
201             Value: %s
202              
203             =head1 EXAMPLE
204              
205             =for comment filename=create_and_print_fsm_transition.pl
206              
207             use strict;
208             use warnings;
209              
210             use Data::FSM::Transition;
211             use Data::FSM::State;
212              
213             my $locked = Data::FSM::State->new(
214             'name' => 'Locked',
215             );
216             my $unlocked = Data::FSM::State->new(
217             'name' => 'Unlocked',
218             );
219             my $obj = Data::FSM::Transition->new(
220             'callback' => sub {
221             my $self = shift;
222             print 'Id: '.$self->id."\n";
223             },
224             'from' => $locked,
225             'id' => 7,
226             'name' => 'Coin',
227             'to' => $unlocked,
228             );
229              
230             # Print out.
231             print 'Id: '.$obj->id."\n";
232             print 'From: '.$obj->from->name."\n";
233             print 'To: '.$obj->from->name."\n";
234             print 'Name: '.$obj->name."\n";
235              
236             # Output:
237             # Id: 7
238             # From: Locked
239             # To: Locked
240             # Name: Coin
241              
242             =head1 DEPENDENCIES
243              
244             L<Mo>,
245             L<Mo::utils>,
246             L<Mo::utils::Number>.
247              
248             =head1 REPOSITORY
249              
250             L<https://github.com/michal-josef-spacek/Data-FSM>
251              
252             =head1 AUTHOR
253              
254             Michal Josef Špaček L<mailto:skim@cpan.org>
255              
256             L<http://skim.cz>
257              
258             =head1 LICENSE AND COPYRIGHT
259              
260             © 2025-2026 Michal Josef Špaček
261              
262             BSD 2-Clause License
263              
264             =head1 VERSION
265              
266             0.01
267              
268             =cut