File Coverage

blib/lib/Workflow/Context.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Workflow::Context;
2              
3 30     30   1639110 use warnings;
  30         86  
  30         1901  
4 30     30   164 use strict;
  30         73  
  30         808  
5 30     30   347 use v5.14.0;
  30         124  
6 30     30   176 use parent qw( Workflow::Base );
  30         62  
  30         192  
7              
8             $Workflow::Context::VERSION = '2.09';
9              
10              
11             sub init {
12 38     38 1 115 my ( $self, %params) = @_;
13              
14 38         156 for my $key (keys %params) {
15 3         16 $self->param( $key => $params{$key} );
16             }
17             }
18              
19             sub merge {
20 1     1 1 48 my ( $self, $other ) = @_;
21 1         4 my $other_params = $other->param();
22 1         2 while ( my ( $k, $v ) = each %{$other_params} ) {
  2         13  
23 1         3 $self->param( $k, $v );
24             }
25             }
26              
27             1;
28              
29             __END__
30              
31             =pod
32              
33             =head1 NAME
34              
35             Workflow::Context - Data blackboard for Workflows, Actions, Conditions and Validators
36              
37             =head1 VERSION
38              
39             This documentation describes version 2.09 of this package
40              
41             =head1 SYNOPSIS
42              
43             # Create your own context and merge it with one that may already be
44             # in a workflow
45              
46             my $context = Workflow::Context->new();
47             $context->param( foo => 'bar' );
48             $context->param( current_user => User->fetch( 'foo@bar.com' ) );
49             my $wf = FACTORY()->create_workflow( 'w/f', $context );
50              
51             # The above is the same as:
52             $context = Workflow::Context->new(
53             foo => 'bar',
54             current_user => User->fetch( 'foo@bar.com' ),
55             );
56             $wf = FACTORY()->create_workflow( 'w/f', $context );
57              
58              
59             # In a Condition get the 'current_user' back out of the workflow's context
60              
61             sub evaluate {
62             my ( $self, $wf ) = @_;
63             my $current_user = $wf->context->param( 'current_user' );
64             ...
65             }
66              
67             # Set values directly into a workflow's context
68              
69             $wf->context->param( foo => 'bar' );
70             $wf->context->param( news => My::News->fetch_where( 'date = ?', DateTime->now ) );
71              
72             =head1 DESCRIPTION
73              
74             Holds information to pass between your application and a Workflow,
75             including its Actions, Conditions and Validators.
76              
77             =head1 OBJECT METHODS
78              
79             =head2 init( %params )
80              
81             Adds C<%params> to the context at instantiation.
82              
83             =head2 merge( $other_context )
84              
85             Merges the values from C<$other_context> into this object. If there
86             are duplicate keys in this object and C<$other_context>,
87             C<$other_context> wins.
88              
89             =head1 SEE ALSO
90              
91             =over
92              
93             =item * L<Workflow>
94              
95             =back
96              
97             =head1 COPYRIGHT
98              
99             Copyright (c) 2003-2021 Chris Winters. All rights reserved.
100              
101             This library is free software; you can redistribute it and/or modify
102             it under the same terms as Perl itself.
103              
104             Please see the F<LICENSE>
105              
106             =head1 AUTHORS
107              
108             Please see L<Workflow>
109              
110             =cut