line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Class::Workflow::YAML; |
4
|
1
|
|
|
1
|
|
45006
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Class::Workflow; |
7
|
|
|
|
|
|
|
use YAML (); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has workflow_key => ( |
10
|
|
|
|
|
|
|
isa => "Str", |
11
|
|
|
|
|
|
|
is => "rw", |
12
|
|
|
|
|
|
|
default => "workflow", |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub load_string { |
16
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
17
|
|
|
|
|
|
|
my $res = $self->localize_yaml_env( _load_string => $data ); |
18
|
|
|
|
|
|
|
my $workflow = $self->empty_workflow; |
19
|
|
|
|
|
|
|
$self->inflate_hash( $workflow, $res ); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub load_file { |
23
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
24
|
|
|
|
|
|
|
my $res = $self->localize_yaml_env( _load_file => $data ); |
25
|
|
|
|
|
|
|
my $workflow = $self->empty_workflow; |
26
|
|
|
|
|
|
|
$self->inflate_hash( $workflow, $res ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _load_string { |
30
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
31
|
|
|
|
|
|
|
YAML::Load( $data ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _load_file { |
35
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
36
|
|
|
|
|
|
|
YAML::LoadFile( $data ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub localize_yaml_env { |
40
|
|
|
|
|
|
|
my ( $self, $method, @args ) = @_; |
41
|
|
|
|
|
|
|
local $YAML::UseCode = 1; |
42
|
|
|
|
|
|
|
$self->$method( @args ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub empty_workflow { |
46
|
|
|
|
|
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
Class::Workflow->new; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub inflate_hash { |
51
|
|
|
|
|
|
|
my ( $self, $workflow, $wrapper ) = @_; |
52
|
|
|
|
|
|
|
my $hash = $wrapper->{ $self->workflow_key }; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
foreach my $key ( keys %$hash ) { |
55
|
|
|
|
|
|
|
if ( my ( $type ) = ( $key =~ /^(state|transition)s$/ ) ) { |
56
|
|
|
|
|
|
|
foreach my $item ( @{ $hash->{$key} } ) { |
57
|
|
|
|
|
|
|
$workflow->$type( ref($item) ? ( ( ref($item) eq "ARRAY" ) ? @$item : %$item ) : $item ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} else { |
60
|
|
|
|
|
|
|
$workflow->$key( $hash->{$key} ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $workflow; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__PACKAGE__; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=pod |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 NAME |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Class::Workflow::YAML - Load workflow definitions from YAML files. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SYNOPSIS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $y = Class::Workflow::YAML->new; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $w = $y->load_file("workflow.yml"); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# an exmaple workflow.yml for the bug flow |
84
|
|
|
|
|
|
|
# mentioned in Class::Workflow |
85
|
|
|
|
|
|
|
--- |
86
|
|
|
|
|
|
|
# data not under the key "workflow" is ignored. |
87
|
|
|
|
|
|
|
# In this example I use 'misc' to predeclare |
88
|
|
|
|
|
|
|
# an alias to some code I'll be using later. |
89
|
|
|
|
|
|
|
misc: |
90
|
|
|
|
|
|
|
set_owner_to_current_user: &setowner !perl/code: | |
91
|
|
|
|
|
|
|
{ |
92
|
|
|
|
|
|
|
my ( $self, $instance, $c ) = @_; |
93
|
|
|
|
|
|
|
# set the owner to the user applying the |
94
|
|
|
|
|
|
|
# transition (see Class::Workflow::Context) |
95
|
|
|
|
|
|
|
return { owner => $c->user }; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
workflow: |
98
|
|
|
|
|
|
|
initial_state: new |
99
|
|
|
|
|
|
|
states: |
100
|
|
|
|
|
|
|
- name: new |
101
|
|
|
|
|
|
|
transitions: |
102
|
|
|
|
|
|
|
# you can store transition |
103
|
|
|
|
|
|
|
# information inline: |
104
|
|
|
|
|
|
|
- name : reject |
105
|
|
|
|
|
|
|
to_state: rejected |
106
|
|
|
|
|
|
|
# or symbolically, defining |
107
|
|
|
|
|
|
|
# in the transitions section |
108
|
|
|
|
|
|
|
- accept |
109
|
|
|
|
|
|
|
- name: open |
110
|
|
|
|
|
|
|
transitions: |
111
|
|
|
|
|
|
|
- name : reassign |
112
|
|
|
|
|
|
|
to_state: unassigned |
113
|
|
|
|
|
|
|
# clear the "owner" field in the instance |
114
|
|
|
|
|
|
|
set_fields: |
115
|
|
|
|
|
|
|
owner: ~ |
116
|
|
|
|
|
|
|
- name : claim_fixed |
117
|
|
|
|
|
|
|
to_state: awaiting_approval |
118
|
|
|
|
|
|
|
- name: awaiting_approval |
119
|
|
|
|
|
|
|
transitions: |
120
|
|
|
|
|
|
|
- name : resolved |
121
|
|
|
|
|
|
|
to_state: closed |
122
|
|
|
|
|
|
|
- name : unresolved |
123
|
|
|
|
|
|
|
to_state: open |
124
|
|
|
|
|
|
|
- name: unassigned |
125
|
|
|
|
|
|
|
transitions: |
126
|
|
|
|
|
|
|
- name : take |
127
|
|
|
|
|
|
|
to_state: open |
128
|
|
|
|
|
|
|
# to dynamically set instance |
129
|
|
|
|
|
|
|
# you do something like this: |
130
|
|
|
|
|
|
|
body_sets_fields: 1 |
131
|
|
|
|
|
|
|
body : *setowner |
132
|
|
|
|
|
|
|
# these two are end states |
133
|
|
|
|
|
|
|
- closed |
134
|
|
|
|
|
|
|
- rejected |
135
|
|
|
|
|
|
|
# we now need to define |
136
|
|
|
|
|
|
|
# the "accept" transition |
137
|
|
|
|
|
|
|
transitions: |
138
|
|
|
|
|
|
|
- name : accept |
139
|
|
|
|
|
|
|
to_state : open |
140
|
|
|
|
|
|
|
body_sets_fields: 1 |
141
|
|
|
|
|
|
|
body : *setowner |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module lets you easily load workflow definitions from YAML files. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
YAML is nice for this because its much more concise than XML, and allows clean |
148
|
|
|
|
|
|
|
embedding of perl code. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 FIELDS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item workflow_key |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=back |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 METHODS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over 4 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item load_file $filename |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item load_string $string |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Load the YAML data, and call C<inflate_hash> on an empty workflow. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item inflate_hash $workflow, $hash |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Define the workflow using the data inside C<< $hash->{$self->workflow_key} >>. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item empty_workflow |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Calls C<< Class::Workflow->new >> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item localize_yaml_env |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
A wrapper method to locally set C<$YAML::Syck::UseCode> to 1. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SEE ALSO |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
L<Class::Workflow>, L<YAML> |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|