File Coverage

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


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