File Coverage

blib/lib/Class/Workflow/Transition/Simple.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::Transition::Simple;
4 1     1   1922 use Moose;
  0            
  0            
5              
6             use overload '""' => "stringify", fallback => 1;
7              
8             # FIXME with Class::Workflow::Transition should not be necessary
9             with qw/
10             Class::Workflow::Transition
11             Class::Workflow::Transition::Deterministic
12             Class::Workflow::Transition::Strict
13             Class::Workflow::Transition::Validate::Simple
14             /;
15              
16             has name => (
17             isa => "Str",
18             is => "rw",
19             );
20              
21             sub stringify {
22             my $self = shift;
23             if ( defined( my $name = $self->name ) ) {
24             return $name;
25             }
26             return overload::StrVal($_[0]);
27             }
28              
29             has misc => (
30             isa => "HashRef",
31             is => "rw",
32             default => sub { {} },
33             auto_deref => 1,
34             );
35              
36             has body => (
37             isa => "CodeRef",
38             is => "rw",
39             default => sub { sub { return () } },
40             );
41              
42             has set_fields => (
43             isa => "HashRef",
44             is => "rw",
45             default => sub { {} },
46             );
47              
48             # if set_fields is set it overrides body_sets_fields
49             has body_sets_fields => (
50             isa => "Bool",
51             is => "rw",
52             default => 0,
53             );
54              
55             sub apply_body {
56             my ( $self, $instance, @args ) = @_;
57             my $body = $self->body;
58              
59             # if we have a predefined set of fields
60             unless ( $self->body_sets_fields ) {
61             return (
62             $self->set_fields,
63             $self->$body( $instance, @args ),
64             );
65             } else {
66             # otherwise let the body control everything
67             return $self->$body( $instance, @args );
68             }
69             }
70              
71             __PACKAGE__;
72              
73             __END__
74              
75             =pod
76              
77             =head1 NAME
78              
79             Class::Workflow::Transition::Simple - A useful class (or base class) for
80             writing transitions.
81              
82             =head1 SYNOPSIS
83              
84             use Class::Workflow::Transition::Simple;
85              
86             my $t = Class::Workflow::Transition::Simple->new(
87             name => "feed",
88             to_state => $not_hungry, # Class::Workflow::Transition::State
89             body_sets_fields => 1,
90             body => sub {
91             my ( $self, $instance, @args ) = @_;
92              
93             my $remain = $global_food_warehouse->reduce_quantity;
94              
95             return (
96             remaining_food => $remain,
97             );
98             },
99             );
100              
101             =head1 DESCRIPTION
102              
103             =head1 FIELDS
104              
105             =over 4
106              
107             =item name
108              
109             This is just a string. It can be used to identify the transition in a parent
110             object like C<Class::Workflow> if any.
111              
112             =item to_state
113              
114             This is the state the transition will transfer to. This comes from
115             C<Class::Workflow::Transition::Deterministic>.
116              
117             =item body
118              
119             This is an optional sub (it defaults to C<<sub { }>>) which will be called
120             during apply, after all validation has passed.
121              
122             The body is invoked as a method on the transition.
123              
124             See C<body_sets_fields> for the semantics of the return value.
125              
126             =item body_sets_fields
127              
128             When true, then the body is expected to return a hash of fields to override in
129             the instance. See L<Class::Workflow::Transition::Deterministic> for details.
130              
131             This field is present to avoid writing code like this:
132              
133             return ( {}, @return_values );
134              
135             When you don't want to set fields in the instance.
136              
137             Defaults to false (just write return @return_value, set to true to set fields).
138              
139             See also C<set_fields>.
140              
141             =item set_fields
142              
143             This field is a hash ref that will be used as the list of fields to set on the
144             instance when C<body_sets_fields> is false.
145              
146             If your transition does not need to dynamically set fields you should probably
147             use this.
148              
149             Defaults to C<{}>.
150              
151             =item validate
152              
153             =item validators
154              
155             =item clear_validators
156              
157             =item add_validators
158              
159             These methods come from L<Class::Workflow::Transition::Validate::Simple>.
160              
161             =back
162              
163             =head1 ROLES
164              
165             This class consumes the following roles:
166              
167             =over 4
168              
169             =item *
170              
171             L<Class::Workflow::Transition::Deterministic>
172              
173             =item *
174              
175             L<Class::Workflow::Transition::Strict>
176              
177             =item *
178              
179             L<Class::Workflow::Transition::Validate::Simple>
180              
181             =back
182              
183             =cut
184              
185