File Coverage

blib/lib/Class/Workflow/Instance.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::Instance;
4 1     1   2064 use Moose::Role;
  0            
  0            
5              
6             with 'MooseX::Clone';
7              
8             has prev => ( # the instance this instance was derived from
9             does => "Class::Workflow::Instance",
10             is => "ro",
11             required => 0,
12             );
13              
14             has transition => ( # the transition this instance is a result of
15             does => "Class::Workflow::Transition",
16             is => "ro",
17             required => 0,
18             );
19              
20             has state => ( # the state the instance is currently in
21             does => "Class::Workflow::State",
22             is => "ro",
23             required => 1,
24             );
25              
26             sub derive {
27             my ( $self, @fields ) = @_;
28             $self->clone( @fields, prev => $self );
29             }
30              
31             sub _clone {
32             my ( $self, @args ) = @_;
33             $self->clone(@args);
34             }
35              
36             # FIXME push this feature to MooseX::Clone by using special values ( \$MooseX::Clone::CLEAR or somesuch )
37             around clone => sub {
38             my ( $next, $self, %fields ) = @_;
39              
40             my @clear = grep { not defined $fields{$_} } keys %fields;
41              
42             delete @fields{@clear};
43              
44             my $clone = $self->$next(%fields);
45              
46             my $meta = Class::MOP::get_metaclass_by_name(ref $self);
47              
48             foreach my $field ( @clear ) {
49             $meta->find_attribute_by_name($field)->clear_value($clone);
50             }
51              
52             return $clone;
53             };
54              
55             __PACKAGE__;
56              
57             __END__
58              
59             =pod
60              
61             =head1 NAME
62              
63             Class::Workflow::Instance - An instance in a workflow, with state and history.
64              
65             =head1 SYNOPSIS
66              
67             package MyInstance;
68             use Moose;
69              
70             with qw(Class::Workflow::Instance);
71              
72             my $instance = MyInstance->new( state => $initial_state );
73              
74             my $new_instance = $transition->apply( $instance, @args );
75              
76             # $instance is in $initial state, no transitions applied
77             # $new_instance may be in another state, $transition has been applied
78             # $new_instance->prev == $instance
79              
80             =head1 DESCRIPTION
81              
82             A workflow instance encapsulates the current state and workflow history on
83             behalf of some parent object that needs state management.
84              
85             In L<Class::Workflow> these instances are functionally pure, that is they don't
86             change but instead derive their parent copies, and the reference from a given
87             item is to the most recently derived copy.
88              
89             This eases auditing, reverting, and the writing of transitions.
90              
91             =head1 FIELDS
92              
93             =over 4
94              
95             =item state
96              
97             The state this instance is in. Required.
98              
99             =item prev
100              
101             The L<Class::Workflow::Instance> object this object was derived from. Optional.
102              
103             =item transition
104              
105             The transition that created this instance from C<prev>.
106              
107             =back
108              
109             =head1 METHODS
110              
111             =over 4
112              
113             =item derive %fields
114              
115             Clones the object, setting C<prev> to the current object, and shadowing the
116             fields with new values from the key value pair list in the arguments.
117              
118             =item clone %fields
119              
120             The low level clone operation. If you need to override L<Moose> based cloning,
121             because your instance objects are e.g. L<DBIx::Class> objects (see the
122             F<examples> directory), then you would likely want to override this.
123              
124             See L<MooseX::Clone> for more details.
125              
126             =back
127              
128             =cut
129              
130