File Coverage

lib/Workflow/Base.pm
Criterion Covered Total %
statement 56 58 96.5
branch 17 20 85.0
condition 5 6 83.3
subroutine 11 11 100.0
pod 7 7 100.0
total 96 102 94.1


line stmt bran cond sub pod time code
1             package Workflow::Base;
2              
3 29     29   780 use warnings;
  29         50  
  29         1006  
4 29     29   203 use strict;
  29         68  
  29         1048  
5 29     29   171 use base qw( Class::Accessor );
  29         57  
  29         17844  
6 29     29   60218 use Log::Log4perl;
  29         92703  
  29         363  
7             $Workflow::Base::VERSION = '1.62';
8              
9             sub new {
10 420     420 1 9804 my ( $class, @params ) = @_;
11 420         1275 my $self = bless { PARAMS => {} }, $class;
12              
13 420 100 100     2202 if ( ref $params[0] eq 'HASH' && ref $params[0]->{param} eq 'ARRAY' ) {
14 26         51 foreach my $declared ( @{ $params[0]->{param} } ) {
  26         91  
15 41         151 $params[0]->{ $declared->{name} } = $declared->{value};
16             }
17 26         96 delete $params[0]->{param};
18             }
19 420         1983 $self->init(@params);
20 416         16603 return $self;
21             }
22              
23 18     18 1 40 sub init {return};
24              
25             sub log {
26 2968   66 2968 1 16739 return ( $_[0]->{log} ||= Log::Log4perl->get_logger(ref $_[0]) );
27             }
28              
29             sub param {
30 485     485 1 62904 my ( $self, $name, $value ) = @_;
31 485 100       932 unless ( defined $name ) {
32 62         79 return { %{ $self->{PARAMS} } };
  62         235  
33             }
34              
35             # Allow multiple parameters to be set at once...
36              
37 423 100       754 if ( ref $name eq 'HASH' ) {
38 1         18 foreach my $param_name ( keys %{$name} ) {
  1         5  
39 2         5 $self->{PARAMS}{$param_name} = $name->{$param_name};
40             }
41 1         2 return { %{ $self->{PARAMS} } };
  1         5  
42             }
43              
44 422 100       712 unless ( defined $value ) {
45 249         1289 return $self->{PARAMS}{$name};
46             }
47 173         567 return $self->{PARAMS}{$name} = $value;
48             }
49              
50             sub delete_param {
51 2     2 1 5 my ( $self, $name ) = @_;
52 2 50       6 unless ( defined $name ) {
53 0         0 return;
54             }
55              
56             # Allow multiple parameters to be deleted at once...
57              
58 2 100       15 if ( ref $name eq 'ARRAY' ) {
59 1         3 my %list = ();
60 1         1 foreach my $param_name ( @{$name} ) {
  1         2  
61 1 50       3 next if ( not exists $self->{PARAMS}{$param_name} );
62 1         3 $list{$param_name} = $self->{PARAMS}{$param_name};
63 1         2 delete $self->{PARAMS}{$param_name};
64             }
65 1         5 return {%list};
66             }
67              
68 1 50       4 if ( exists $self->{PARAMS}{$name} ) {
69 1         2 my $value = $self->{PARAMS}{$name};
70 1         2 delete $self->{PARAMS}{$name};
71 1         5 return $value;
72             }
73 0         0 return;
74             }
75              
76             sub clear_params {
77 1     1 1 3 my ($self) = @_;
78 1         7 $self->{PARAMS} = {};
79             }
80              
81             sub normalize_array {
82 303     303 1 2125 my ( $self, $ref_or_item ) = @_;
83 303 100       760 return () unless ($ref_or_item);
84 137 100       392 return ( ref $ref_or_item eq 'ARRAY' ) ? @{$ref_or_item} : ($ref_or_item);
  122         368  
85             }
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =head1 NAME
94              
95             Workflow::Base - Base class with constructor
96              
97             =head1 VERSION
98              
99             This documentation describes version 1.62 of this package
100              
101             =head1 SYNOPSIS
102              
103             package My::App::Foo;
104             use base qw( Workflow::Base );
105              
106             =head1 DESCRIPTION
107              
108             Provide a constructor and some other useful methods for subclasses.
109              
110             =head1 METHODS
111              
112             =head2 Class Methods
113              
114             =head3 new( @params )
115              
116             Just create a new object (blessed hashref) and pass along C<@params>
117             to the C<init()> method, which subclasses can override to initialize
118             themselves.
119              
120             Returns: new object
121              
122             =head2 Object Methods
123              
124             =head3 init( @params )
125              
126             Subclasses may implement to do initialization. The C<@params> are
127             whatever is passed into C<new()>. Nothing need be returned.
128              
129             =head3 log()
130              
131             Returns the logger for the instance, based on the instance class.
132              
133             =head3 param( [ $name, $value ] )
134              
135             Associate arbitrary parameters with this object.
136              
137             If neither C<$name> nor C<$value> given, return a hashref of all
138             parameters set in object:
139              
140             my $params = $object->param();
141             while ( my ( $name, $value ) = each %{ $params } ) {
142             print "$name = $params->{ $name }\n";
143             }
144              
145             If C<$name> given and it is a hash reference, assign all the values of
146             the reference to the object parameters. This is the way to assign
147             multiple parameters at once. Note that these will overwrite any
148             existing parameter values. Return a hashref of all parameters set in
149             object.
150              
151             $object->param({ foo => 'bar',
152             baz => 'blarney' });
153              
154             If C<$name> given and it is not a hash reference, return the value
155             associated with it, C<undef> if C<$name> was not previously set.
156              
157             my $value = $object->param( 'foo' );
158             print "Value of 'foo' is '$value'\n";
159              
160             If C<$name> and C<$value> given, associate C<$name> with C<$value>,
161             overwriting any existing value, and return the new value.
162              
163             $object->param( foo => 'blurney' );
164              
165             =head3 delete_param( [ $name ] )
166              
167             Delete parameters from this object.
168              
169             If C<$name> given and it is an array reference, then delete all
170             parameters from this object. All deleted parameters will be returned
171             as a hash reference together with their values.
172              
173             my $deleted = $object->delete_param(['foo','baz']);
174             foreach my $key (keys %{$deleted})
175             {
176             print $key."::=".$deleted->{$key}."\n";
177             }
178              
179             If C<$name> given and it is not an array reference, delete the
180             parameter and return the value of the parameter.
181              
182             my $value = $object->delete_param( 'foo' );
183             print "Value of 'foo' was '$value'\n";
184              
185             If C<$name> is not defined or C<$name> does not exists the
186             undef is returned.
187              
188             =head3 clear_params()
189              
190             Clears out all parameters associated with this object.
191              
192             =head3 normalize_array( \@array | $item )
193              
194             If given C<\@array> return it dereferenced; if given C<$item>, return
195             it in a list. If given neither return an empty list.
196              
197             =head1 COPYRIGHT
198              
199             Copyright (c) 2003-2023 Chris Winters. All rights reserved.
200              
201             This library is free software; you can redistribute it and/or modify
202             it under the same terms as Perl itself.
203              
204             Please see the F<LICENSE>
205              
206             =head1 AUTHORS
207              
208             Please see L<Workflow>
209              
210             =cut